C#用户控件加载2次而不是一次

时间:2018-04-24 04:29:19

标签: c#

我正在处理一个托盘图标应用程序,按下后会打开一个包含几个面板的表单,在其中一个面板中有用户控件可以通过按钮打开和关闭

    Form1 form = new Form1();
    private void Icon_Click(object sender, EventArgs e)
    {
        form.Left = Cursor.Position.X - (form.Width / 2);
        form.Top = Screen.PrimaryScreen.WorkingArea.Bottom - form.Height;

        Form fc = Application.OpenForms["Form1"];
        try
        {
            if (fc == null)
            {
                form.Left = Cursor.Position.X - (form.Width / 2);
                form.Top = Screen.PrimaryScreen.WorkingArea.Bottom - form.Height;
                form.Show();
            }
            else
            {
                fc.Close();
            }
            form = new Form1();
        }
        catch (Exception)
        {
        }
    }

这是显示和隐藏表单的代码

This is how the application looks so far

正如您在图像中看到的那样,用户控制部分位于其他2个面板的中间,我使用底部面板中的按钮切换用户控件

应用程序使用位于其中一个用户控件中的TcpListener,一旦启动它就会不断地侦听端口,但由于用户控件加载了两次,我得到Only one usage of each socket address is normally permitted异常并且TcpListener因为这个。 我已经从工具箱中将用户控件加载到表单中,并且所有这些控件都从属性窗口设置为visible = false,并且我在Form1()函数中加载表单时显示用户控件。

这是控制用户控件的显示/隐藏和切换按钮背景图像(颜色)的代码

if (lastbutton != null)
{
      lastbutton.BackgroundImage = idleimages[lastbutton];
      lastcontrol.Visible = false;
      lastcontrol.Enabled = false;
}
lastbutton = mycomputerbtn;
lastbutton.BackgroundImage = activeimages[lastbutton];
lastcontrol = myComputerScreen1;
lastcontrol.Enabled = true;
lastcontrol.Visible = true;
lastcontrol.Focus();

我不知道是否应该这样做但是我在用户控件中设置了一个消息框,当我在visual studio designer中打开表单时会显示该消息框

0 个答案:

没有答案
相关问题