The proper way to check if a form is already shown?

前端 未结 2 570
青春惊慌失措
青春惊慌失措 2020-12-10 13:35

I have created a task managment application and I wanted to implement the ability for 2-users to chat about specific task.

In Form1 I have a timer that

相关标签:
2条回答
  • Firstly you are creating a new instance of Form14 every time you have a new message.

    Secondly Show and ShowDialog do two very different things:

    Show just displays the form, whereas ShowDialog displays the form as a modal dialog. This means the user can't do anything else until they dismiss the form.

    You need to have a single instance of the form and you can use the Visible property to determine whether it's shown or not. So you would have:

    private Form14 frm14;
    

    Then in the constructor:

    frm14 = new Form14();
    

    Then in your code:

    if (!frm14.Visible)
    {
        // Add the message
        frm14.Show();
    } else{
        // Top
        frm14.BringToFront();
    }
    
    0 讨论(0)
  • Try making form14 a member of form1. When you get a new message check the Visible property of forom14 to know if it is already showing.

    0 讨论(0)
提交回复
热议问题