C# WinForm编程,同时启动多个窗体后,点击一个 button 按钮,将该按钮对应的一个窗体显示在最外面,而不是通过点击该窗体!!! winformc#编程 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 打开窗体时创建 有标示的button,关闭窗体时,remove相对应标记的button。点击按钮时,同样让窗体显示就可以了 class WindowNotifier{ public static readonly WindowNotifier instance = new WindowNotifier(); public void Open() { OnOpen(this, new EventArgs()); } public void Close() { OnClose(this, new EventArgs()); } public event EventHandler OnOpen; public event EventHandler OnClose;}编写一个窗体基类:class FormBase{ public FormBase() { this.Load += (a, b) => WindowNotifier.instance.Open(); this.UnLoad += (a, b) => WindowNotifier.instance.Close(); } }其他窗体从它继承class Form1 : FormBase{ void Form_Load() { WindowNotifier.instance.OnOpen += RefreashButtons(); WindowNotifier.instance.OnClose += RefreashButtons(); } void RefreashButtons(sender, e) { foreach (var item in this.Controls.OfType<Button>().ToList()) this.Controls.Remove(item); foreach (Form item in Application.OpenedForms) { Button btn = new Button(); btn.Tag = item; btn.Text = item.Text; btn.Location = ... btn.Size = ... btn.Click += (a, b) => ((a as Button).Tag as Form).Close(); this.Controls.Add(btn); } }} 嗯,主要是同时启动多个窗体后,对应动态生成多个button 按钮(一个窗体对应一个button 按钮),点击一个 button 按钮,将该按钮对应的一个窗体显示在最外面,而不是通过点击该窗体,主要是怎样做,我是初学者,没有思路啊。 我用过tabcontrol的,不能满足啊, 使用MagicDocking效果会更好 这个是用toolStrip 做的,你自己修改下,用button,button动态创建在flowLayoutPanel 中就不用手动控制位置了,还有用dev的xtraTabbedMdiManager 这个控件不用写代码就能达到你的要求 private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { for (int i = 0; i < this.MdiChildren.Length; i++) { if (this.MdiChildren[i] == e.ClickedItem.Tag) { this.MdiChildren[i].Activate(); } } } void BaseForm_ChlidFormClosedEvent(Form form) { for (int i = 0; i < toolStrip1.Items.Count; i++) { if (toolStrip1.Items[i].Tag == form) { if (StrName.Count != 0) { for (int j = 0; j < StrName.Count; j++) { if (StrName[j] == toolStrip1.Items[i].Tag) StrName.Remove(StrName[j]); } } this.toolStrip1.Items.Remove(toolStrip1.Items[i]); } } } 不需要生成按钮,在.net应用程序中,每打开一个窗体,.net就会把该窗体存入一个窗体集合(Appliction.OpenForms)中,要想让窗体出现在最顶端,只需调用Appliction.OpenForms[窗体Name].Activate()即可。楼主做的应该是一个窗口管理器,最简单的思路如下:遍历Application.OpenForms,把每个窗体的标题与Name存入listView中,用户选中点激活后,就利用用户选中的项信息调用Activate()即可。不但有激活功能,在MdiParent.Layout()方法中还提供了层叠,水平平铺,垂直平铺等窗体排列功能(只能在MDI中使用)。 UIL采购管理.frm_CaiGouGuanLi myfrm_CaiGouGuanLi = new UIL采购管理.frm_CaiGouGuanLi(ref MyPanel); panel2.Controls.Clear(); myfrm_CaiGouGuanLi.TopLevel = false; myfrm_CaiGouGuanLi.Parent = panel2; myfrm_CaiGouGuanLi.Show();我用panel 作为父容器, UIL采购管理.frm_CaiGouGuanLi myfrm_CaiGouGuanLi = new UIL采购管理.frm_CaiGouGuanLi(ref MyPanel); panel2.Controls.Clear(); myfrm_CaiGouGuanLi.TopLevel = false; myfrm_CaiGouGuanLi.Parent = panel2; myfrm_CaiGouGuanLi.Show(); C/S求安全身份认证方案…… Socket问题 socket拔掉网线如何获取 疑问:为什么我写的程序要等执行完了窗口才有反应? c#出现的错误? 关于DirectX简单按钮移动的动画问题。 codesmith如何连接oracle的数据库??? 求C#读写Excel文档的方法。最好有实例,谢谢! 请问:怎样将datagrid的数据源设置为一维数组?? [],菜鸟问题 C# www 上传图片 如何把一个4字节的浮点数转换成十进制的数
点击按钮时,同样让窗体显示就可以了
{
public static readonly WindowNotifier instance = new WindowNotifier();
public void Open() { OnOpen(this, new EventArgs()); }
public void Close() { OnClose(this, new EventArgs()); }
public event EventHandler OnOpen;
public event EventHandler OnClose;
}编写一个窗体基类:
class FormBase
{
public FormBase() {
this.Load += (a, b) => WindowNotifier.instance.Open();
this.UnLoad += (a, b) => WindowNotifier.instance.Close();
}
}其他窗体从它继承
class Form1 : FormBase
{
void Form_Load()
{
WindowNotifier.instance.OnOpen += RefreashButtons();
WindowNotifier.instance.OnClose += RefreashButtons();
}
void RefreashButtons(sender, e)
{
foreach (var item in this.Controls.OfType<Button>().ToList()) this.Controls.Remove(item);
foreach (Form item in Application.OpenedForms)
{
Button btn = new Button();
btn.Tag = item;
btn.Text = item.Text;
btn.Location = ...
btn.Size = ...
btn.Click += (a, b) => ((a as Button).Tag as Form).Close();
this.Controls.Add(btn);
}
}
}
{
for (int i = 0; i < this.MdiChildren.Length; i++)
{
if (this.MdiChildren[i] == e.ClickedItem.Tag)
{
this.MdiChildren[i].Activate();
}
}
} void BaseForm_ChlidFormClosedEvent(Form form)
{ for (int i = 0; i < toolStrip1.Items.Count; i++)
{
if (toolStrip1.Items[i].Tag == form)
{
if (StrName.Count != 0)
{
for (int j = 0; j < StrName.Count; j++)
{
if (StrName[j] == toolStrip1.Items[i].Tag)
StrName.Remove(StrName[j]);
}
}
this.toolStrip1.Items.Remove(toolStrip1.Items[i]);
}
} }
UIL采购管理.frm_CaiGouGuanLi myfrm_CaiGouGuanLi = new UIL采购管理.frm_CaiGouGuanLi(ref MyPanel);
panel2.Controls.Clear();
myfrm_CaiGouGuanLi.TopLevel = false;
myfrm_CaiGouGuanLi.Parent = panel2;
myfrm_CaiGouGuanLi.Show();我用panel 作为父容器,
panel2.Controls.Clear();
myfrm_CaiGouGuanLi.TopLevel = false;
myfrm_CaiGouGuanLi.Parent = panel2;
myfrm_CaiGouGuanLi.Show();