none
Can you help me with a problem? RRS feed

  • Question

  • In my simple C# Program, I put 2 forms! One button in form1 open form2! Then Form2 should be able to do various actions! One of the actions is Hide form1, but somehow, with that code, Form1 not even moves. If u click Hide Form1 it should hide form1 or it's a code error?

    The code of the button:

    var Form1 = new Form1();

    Form1.Visible = false;

    Sunday, November 3, 2019 5:22 PM

Answers

  • The very basics

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var f = new Form2(this);
                f.Show();
            }
        }
    }
    

    Form 2

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private readonly Form _form;
            public Form2(Form ownerForm)
            {
                InitializeComponent();
                _form = ownerForm;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                _form.Hide();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                _form.Visible = true;
                BringToFront();
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by joozyT Sunday, November 3, 2019 7:15 PM
    Sunday, November 3, 2019 5:51 PM
    Moderator