高手帮忙:声明并显示一个窗体后,如何重新捕捉到这个窗体?? 求救阿:各位高手帮帮忙,问题解决,马上结贴例如我在button1 但击事件中显示了一个窗体newfrm如何在button2 的单击事件中修改newfrm的Text 属性>??? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 例子:设有MainForm和BasicInfo两个窗体。修改一下BasicInfo窗口的构造函数,将MainForm的实例作为一个参数传递给BasicInfo,然后BasicInfo关闭时就可以引用原来那个MainForm了。例如:在MainForm中的:private void button2_Click(object sender, System.EventArgs e){ this.Hide(); BasicInfo frm=new BasicInfo(this); frm.ShowDialog();}在BasicInfo.cs中:public class BasicInfo{……private MainForm parentForm;……public BasicInfo(MainForm frm){ parentForm = frm;}……private void button12_Click(object sender, System.EventArgs e){ this.Close(); parentForm.ShowDialog();//这里也可以引用parentForm的其它属性}} 晕,这么简单!Form1是主窗体Form2是被调用的窗体在Form1中设置一个全局变量Form1 form1=null;然后button1事件form2=new Form2();form2.show();button2时间form2.Text="love";一切OK 可是如果我单击了击次button1 ,然后就会打开几个form1 的实例,如何捕捉其中的一个 啊?? form1.Owner = this;void OnButton1Click(...){ if(OwnerForms.Length > 0) return; ...} 谢谢 haiwangstar(八月桂花香) 指点:void OnButton1Click(...){ if(OwnerForms.Length > 0) return; ...}能解释一下OwnerForms 的含义嘛?? 可是如果我单击了击次button1 ,然后就会打开几个form1 的实例,如何捕捉其中的一个 啊??你可以设置一下,如果已经有一个实例了就把button1设置为不可用真是搞不懂你什么是捕捉其中的一个?你这句话让人不明白哦 如果主窗体是MDI父窗体的话:MdiParent属性,子窗体的)假设名称为frmParent。 frmParent.IsMdiContainer=true;创建一个子窗体,假设子窗体name为frmChild frmChild.IsMdiChild=true; frmChild.text="子"主窗体中button1_click的代码:for(int i=0;i<this.MdiChildren.Length;i++) //(MdiChildren属性是子窗体的form[]){ Form frmInstan=MdiChildren[i]; if(frmInstan.Text=="子") frmInstan.Show();}frmChild frmChildInstan = new frmChild();frmParent.MdiParent = this;frmChildInstan .Show();============================================================================如果不是MID:假设两个窗体:from1,from2(避免被多次调用的窗体); from2.text="子"[from1中引用的命名空间]:using System.Runtime.InteropServices; //命名空间提供可用于访问 .NET 中的 COM 对象和本机 API 的类的集合。using System;//指针或句柄的平台特定类型[from1中引用成员函数]:[STAThread][DllImport("User32",EntryPoint="FindWindow")]private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);[DllImport("User32",EntryPoint="SetForegroundWindow")] private static extern bool SetForegroundWindow(IntPtr hWnd);[STAThread][DllImport("User32",EntryPoint="FindWindowEx")] private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter,string lpClassName,string lpWindowName);[from1 中button1_click 代码]:bool bHigherPriorityWindowfrom;IntPtr hWndLogin = FindWindow(null, "子");+" "+IntPtr.Zero.ToString());if(hWndLogin != IntPtr.Zero){ bHigherPriorityWindowfrom = SetForegroundWindow(hWndLogin);//如果存在激活并显示}else{ from2 from2Instan = new from2(); from2Instan .Show();} TO 回复人: quzw(quzw)如果(拥有的子窗口.数量 > 0) 返回; //已经有打开的窗口了,不做操作,直接返回.新的中文编程语言..呵.. 那你需要判断 newfrm 是肤创建?follow if(newfrm == null || !newfrm.IsCreate()){ new form1();}else{ newfrm.focu();} 请教各位一个串口方面的小问题 引用 Microsoft.Office.Interop.Excel 出错 求comboBox1.SelectedItem.ToString()与 comboBox1.Text 区别 问个类型转换的问题,高分送上.谢谢各位~~~~~~~ 又是XML读取写进数据库的问题,为什么我又说"又"呢? 点击链接调用页面的POSTBACK属性 什么叫OWC 面板的KeyEvent事件 删除确认 如何在C#中操作foxpro数据库阿!?怎么连接,和打开呢?进行查询! 要实现这样的功能,用什么方法比较好?在线 如何判断一个字符串当中是否包含某一个字符?
例如:
在MainForm中的:
private void button2_Click(object sender, System.EventArgs e)
{
this.Hide();
BasicInfo frm=new BasicInfo(this);
frm.ShowDialog();
}
在BasicInfo.cs中:
public class BasicInfo
{
……
private MainForm parentForm;
……
public BasicInfo(MainForm frm)
{
parentForm = frm;
}
……
private void button12_Click(object sender, System.EventArgs e)
{
this.Close();
parentForm.ShowDialog();//这里也可以引用parentForm的其它属性}
}
Form1是主窗体
Form2是被调用的窗体
在Form1中设置一个全局变量Form1 form1=null;
然后button1事件form2=new Form2();
form2.show();
button2时间form2.Text="love";
一切OK
{
if(OwnerForms.Length > 0)
return;
...
}
{
if(OwnerForms.Length > 0)
return;
...
}
能解释一下OwnerForms 的含义嘛??
如何捕捉其中的一个 啊??
你可以设置一下,如果已经有一个实例了就把button1设置为不可用
真是搞不懂你
什么是捕捉其中的一个?你这句话让人不明白哦
假设名称为frmParent。
frmParent.IsMdiContainer=true;
创建一个子窗体,假设子窗体name为frmChild
frmChild.IsMdiChild=true;
frmChild.text="子"
主窗体中button1_click的代码:
for(int i=0;i<this.MdiChildren.Length;i++) //(MdiChildren属性是子窗体的form[])
{
Form frmInstan=MdiChildren[i];
if(frmInstan.Text=="子")
frmInstan.Show();
}
frmChild frmChildInstan = new frmChild();
frmParent.MdiParent = this;
frmChildInstan .Show();
============================================================================
如果不是MID:
假设两个窗体:from1,from2(避免被多次调用的窗体);
from2.text="子"
[from1中引用的命名空间]:
using System.Runtime.InteropServices; //命名空间提供可用于访问 .NET 中的 COM 对象
和本机 API 的类的集合。
using System;//指针或句柄的平台特定类型[from1中引用成员函数]:
[STAThread]
[DllImport("User32",EntryPoint="FindWindow")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);[DllImport("User32",EntryPoint="SetForegroundWindow")] private static extern bool SetForegroundWindow(IntPtr hWnd);[STAThread]
[DllImport("User32",EntryPoint="FindWindowEx")] private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter,string lpClassName,string lpWindowName);[from1 中button1_click 代码]:
bool bHigherPriorityWindowfrom;
IntPtr hWndLogin = FindWindow(null, "子");+" "+IntPtr.Zero.ToString());
if(hWndLogin != IntPtr.Zero)
{
bHigherPriorityWindowfrom = SetForegroundWindow(hWndLogin);//如果存在激活并显示
}
else
{
from2 from2Instan = new from2();
from2Instan .Show();
}
返回; //已经有打开的窗口了,不做操作,直接返回.新的中文编程语言..呵..
if(newfrm == null || !newfrm.IsCreate())
{
new form1();
}
else
{
newfrm.focu();
}