C#判断控件是否存在!!跨窗体操作问题!!

睡神在睡觉 2008-07-07 01:19:50
1.用代码创建窗体上所有控件,判断如果存在该控件则不创建,不存在则创建,请问如何实现?

2.如何实现跨窗体关闭窗口。譬如在from1上有butten1,单击butten1在from2上创建个butten2,单击butten2关闭from2,怎么来实现?
...全文
932 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
faipo 2012-05-14
  • 打赏
  • 举报
回复
直接
if (this.Controls[cName]!=null)

开发者孙小聪 2011-03-28
  • 打赏
  • 举报
回复
找到 好的办法了么??
barius 2008-07-09
  • 打赏
  • 举报
回复
在程序里的某个地方创建一个全局静态类,在里面建一个全局静态成员保存一下对于Form1的引用,然后直接用这个引用访问那个Form,等于现在你就拥有了对一切Private成员的访问权。好了,这下你无敌了- -
marvelstack 2008-07-08
  • 打赏
  • 举报
回复
1.创建控件的时候在容器里面保存引用,在下次创建的时候先查找一下,引用,如果有了就不要再创建了。
2.这个问题可以参考这里,
http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
http://blog.csdn.net/zhzuo/archive/2006/05/05/708941.aspx
yongzhesk 2008-07-07
  • 打赏
  • 举报
回复
mark
baihe_591 2008-07-07
  • 打赏
  • 举报
回复
来晚了。
jcins 2008-07-07
  • 打赏
  • 举报
回复
八楼的想法很不错!!
lizhie 2008-07-07
  • 打赏
  • 举报
回复
八楼的思想特别好
我支持你

随缘心 2008-07-07
  • 打赏
  • 举报
回复
学习了,顶一下
sxmonsy 2008-07-07
  • 打赏
  • 举报
回复
二楼的学习了,好厉害呀.
Ador3 2008-07-07
  • 打赏
  • 举报
回复
[Quote=引用楼主 sleep0110 的帖子:]
1.用代码创建窗体上所有控件,判断如果存在该控件则不创建,不存在则创建,请问如何实现?

2.如何实现跨窗体关闭窗口。譬如在from1上有butten1,单击butten1在from2上创建个butten2,单击butten2关闭from2,怎么来实现?
[/Quote]
1.遍历以下你要的控件在不,不在就创建: this.Controls
2.FindWindow找到窗口实例,对其操作!
lovefootball 2008-07-07
  • 打赏
  • 举报
回复
1、你得写个方法查找所有控件,会用到递归
代码大致如下,你可以自行修改下以符合你的要求


private Control FindControl(Control container, string controlName)
{
if (container.Name == controlName)
{
return container;
}
Control findControl = null;
foreach (Control control in container.Controls)
{
Console.WriteLine(control.Name);
if (control.Controls.Count == 0)
{
if (control.Name == controlName)
{
findControl = control;
break;
}
}
else
{
findControl = FindControl(control, controlName);
if (findControl != null)
{
break;
}
}
}
return findControl;
}

2、在Form1中声明一个全局的Form2
private Form2 form2 = null;

button1_Click()
{
if (form2 == null)
{
form2 = new Form2();
Button btn = new Button();
........//创建Button的其他属性
btn.Click += .........//添加Button的Click事件
}
form2.Show();
}

btn_Click()//新添加的Button的事件
{
if (form2 != null) form2.CLose();
}
cbgn 2008-07-07
  • 打赏
  • 举报
回复
判断控件的NAME属性
for(int i =0;i<frm.controls.count;i++)
{
if(frm.controls[i].name=="XX")
{
不创建
}
else
{
创建

}
}
快乐乔巴 2008-07-07
  • 打赏
  • 举报
回复
1.可以创建一个容器,比如GroupBox,panel等,然后你在代码里在容器中创建,判断有没有此控件,容器中有FindControl()方法,外面写个try 查找不到就抛异常的话就是没找到~~
fsy123456accp 2008-07-07
  • 打赏
  • 举报
回复
偶 不知道 来顶一下
yilanwuyu123 2008-07-07
  • 打赏
  • 举报
回复
用代码创建 就加入命名空间 一个一个new出来 设置显示位置、text等属性
cbgn 2008-07-07
  • 打赏
  • 举报
回复
//button_ok
Button b = new Button();
b.Click += new System.EventHandler(btnOK);
b.Left = 0;
b.Top = l.Height;
b.Width = 70;
b.FlatStyle = FlatStyle.Popup ;
b.Text = "确定";
frmSetReport.Controls.Add(b);

ericzhangbo1982111 2008-07-07
  • 打赏
  • 举报
回复
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem aaToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem bbbToolStripMenuItem;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.PictureBox pictureBox1;

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.treeView1 = new System.Windows.Forms.TreeView();
this.listView1 = new System.Windows.Forms.ListView();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.aaToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.bbbToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.textBox1 = new System.Windows.Forms.TextBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.contextMenuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(9, 66);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 36);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseCompatibleTextRendering = true;
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// treeView1
//
this.treeView1.Location = new System.Drawing.Point(178, 0);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(269, 348);
this.treeView1.TabIndex = 1;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
this.treeView1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.treeView1_KeyDown);
this.treeView1.AfterExpand += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterExpand);
//
// listView1
//
this.listView1.ContextMenuStrip = this.contextMenuStrip1;
this.listView1.Location = new System.Drawing.Point(14, 122);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(128, 111);
this.listView1.TabIndex = 2;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;

//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(64, 1);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(72, 101);
this.textBox1.TabIndex = 3;
this.textBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseMove);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(19, 251);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(38, 40);
this.listBox1.TabIndex = 4;
//
// listBox2
//
this.listBox2.FormattingEnabled = true;
this.listBox2.ItemHeight = 12;
this.listBox2.Items.AddRange(new object[] {
"1",
"2",
"3"});
this.listBox2.Location = new System.Drawing.Point(80, 251);
this.listBox2.MultiColumn = true;
this.listBox2.Name = "listBox2";
this.listBox2.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
this.listBox2.Size = new System.Drawing.Size(77, 52);
this.listBox2.TabIndex = 5;
//
// comboBox1
//
this.comboBox1.ForeColor = System.Drawing.Color.Red;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"1234123",
"21312",
"3121"});
this.comboBox1.Location = new System.Drawing.Point(226, 314);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(143, 20);
this.comboBox1.TabIndex = 6;
this.comboBox1.Text = "2323";
//
// pictureBox1
//
this.pictureBox1.ErrorImage = null;
this.pictureBox1.Image = global::WindowsApplication1.Properties.Resources.kittens_016;
this.pictureBox1.Location = new System.Drawing.Point(199, 54);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(221, 210);
this.pictureBox1.TabIndex = 7;
this.pictureBox1.TabStop = false;
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(459, 348);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listView1);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form2_KeyDown);
this.Load += new System.EventHandler(this.Form2_Load);
this.contextMenuStrip1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}






2.如何实现跨窗体关闭窗口。譬如在from1上有butten1,单击butten1在from2上创建个butten2,单击butten2关闭from2,怎么来实现?

form1.cs
Form2 f2=new Form2();
private void button1_Click(object sender, EventArgs e)
{


Button b2=new Button();
b2.Text="b2";
b2.Click+=new EventHandler(b2_Click);
f2.Controls.Add(b2);
}

private void b2_Click(object sender, EventArgs e)
{
b2.Close();
}
utpcb 2008-07-07
  • 打赏
  • 举报
回复
Application.Exit();

110,912

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧