控制一个表单多次打开

假设我多次打开同一个表单,但我只想控制其中一个(一个 fe 具有“hello”作为窗口标题(文本) <- 来识别)


我该如何做到这一点?


编辑:这是我想做的一个例子(它有点复杂,我不擅长解释我想要什么)


private void openthesecformfirst_Click(object sender, EventArgs e)

{

    Form2 sec = new Form2();

    sec.Text = "Hi";

    sec.Show();

    //The second form is now opened

}


private void openthesecformsecond_Click(object sender, EventArgs e)

{

    Form2 sec = new Form2();

    sec.Text = "Hello";

    sec.Show();

    //the second form is now opened twice

}


private void changelabelinfirst_Click(object sender, EventArgs e)

{

    //Identified by the title the first opened form2 is supposed to change a label text


    //How do I get this one specifically?

}


private void changelabelinsecond_Click(object sender, EventArgs e)

{

    //Identified by the title the second opened form2 is supposed to change a label text


    //How do I get this one specifically?

}


阿波罗的战车
浏览 114回答 2
2回答

婷婷同学_

对于 OS Windows 中的查找窗口,您可以使用FindWindowExWin32 Api,例如:因为这是原始的不安全代码,您应该从 user32.dll 导入函数:[DllImport("user32.dll", SetLastError = true)] static extern IntPtr&nbsp;FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,&nbsp;string lpszClass, string lpszWindow);[DllImport("user32.dll", SetLastError = true)]public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr&nbsp;childAfter,&nbsp;string className,&nbsp; string windowTitle);导入后,您可以使用这样的功能:&nbsp;var CaptionTextForLooking = "hello"; // or "Hi"&nbsp;var foundWindowPtr =&nbsp;&nbsp;FindWindowEx(IntPtr.Zero,IntPtr.Zero,CaptionTextForLooking&nbsp;&nbsp;,IntPtr.Zero);

绝地无双

您可以使用Application.OpenForms属性。
打开App,查看更多内容
随时随地看视频慕课网APP