Why doesn't FormShown event fire in C #?

I am new to C # and I am using windows forms. I have Form1and Form2. In Form1I use the following code to show Form2:

Form2 frm2 = new Form2();
private void button1_Click(object sender, EventArgs e)
{          
    frm2.Show();
}

I want: I want to perform some actions when Form2displayed every time. I posted messageBoxin the event Form2Shown(for the test), but it only fires once and the next time I show Form2it never fires. I also tried to use it formLoad, but it only starts once and the next time I show Form2it never starts. I know that I can use frm2.ShowDialog () to show the displayed event every time, but for some reason I don't want to.

private void Form2_Shown(object sender, EventArgs e)
{
    MessageBox.Show("Form2 is shown"); // this gets fired only once when form2 is shown.
                                       // when I show form2 again it does not get fired.
}

private void button_Hide_Form2_Click(object sender, EventArgs e)
{

    // this is in form2
    Hide();
}

: - , , Form2? .

+4
4

MSDN

Shown ; , , , , .

MSDN , . , .

.

1 - ,

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    Form2 frm = new Form2();

    private void btnShow_Click(object sender, EventArgs e)
    {
        frm.Show();
    }

    private void btnHide_Click(object sender, EventArgs e)
    {
        frm.Hide();
    }
}

Form2 - msgbox, . , , .

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_VisibleChanged(object sender, EventArgs e)
    {
        if(this.Visible == true)
            MessageBox.Show("Hey");
    }

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
        this.Hide();           
    }
}
+10

, , :

// You can use auto-property in here.
private Form2 frm2 { get; set; }

private void button1_Click(object sender, EventArgs e)
{
    // Create a new instance before it is shown.
    NewForm();
    frm2.Show();
}

// Creates a new instance and hooks the event.
private void NewForm()
{
    frm2 = new Form2();
    // When creating an instance, hook the load event.
    frm2.Load += Form2_Load;
}

// Will be triggered when show is called.
private void Form2_Load(object sender, EventArgs e)
{
    // Do stuff
}
+1

If you need only Form2_Shownin your class Form2, you should override OnActivated()and not use EventHandler:

protected override void OnActivated(EventArgs e)
{
    MessageBox.Show("Form2 is shown");
}

The OnActivated method also allows derived classes to handle the event without attaching a delegate. Overriding this method is the preferred method for handling an event in a derived class.

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.onload(v=vs.110).aspx

+1
source

I have found the answer. we use the Form2_VisibleChanged event:

 private void Form2_VisibleChanged(object sender, EventArgs e)
    {
        if(this.Visible==true)
        {

            MessageBox.Show("Form is visible");
        }
        else
        {
            MessageBox.Show("Form is hiding");

        }


    }
+1
source

Source: https://habr.com/ru/post/1652588/


All Articles