C#:BringToFront(),TopMost = True不工作

时间:2017-10-10 17:24:06

标签: c# winforms bringtofront

我正在使用Visual Studio 2008在C#中创建Windows表单应用程序。 我对Button有一个MainForm控件,会在AnotherForm事件上打开Click

我想做的是:

  1. 允许AnotherForm一次只能打开一次,
  2. 如果用户在已打开时尝试打开另一个AnotherForm,则显示错误消息, AND 将已打开的AnotherForm置于前面。
  3. 我可以使用AnotherForm字段将static打开计数限制为1。但是,我很难实现要求#2。它显示了一条错误消息,但它没有将已经打开的AnotherForm带到前面。

    这是我的代码:

    **MainForm**
    private void BtnToOpenAnotherFornn_Click(object sender, EventArgs e)
    {
        AnotherForm af = new AnotherForm();
        if (af.GetNumForms() < 1)
            af.Show();
        else
        {
            MessageBox.Show("AnotherForm is already open.");
            //af.TopMost = true;  //Not working
            //af.BringToFront();   //Not working
        }        
    }
    
    **AnotherForm**
    private static int NumForms = 0;
    public int GetForms(){
        return NumForms;
    }
    

    有人可以告诉我如何在AnotherForm声明中将else带到前面吗?

0 个答案:

没有答案