读书人

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

发布时间: 2012-05-16 11:12:12 作者: rapoo

C#判断控件是否存在!!跨窗体操作问题!!
1.用代码创建窗体上所有控件,判断如果存在该控件则不创建,不存在则创建,请问如何实现?

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

[解决办法]
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();
}
[解决办法]
1、你得写个方法查找所有控件,会用到递归
代码大致如下,你可以自行修改下以符合你的要求

C# code
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;} 

读书人网 >C#

热点推荐