28
2012
03

C# Winfrom中遍历控件

private void Form1_Load(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
if (c.GetType().Name == "Button")
((Button)c).Click += new System.EventHandler(Button_Click);
}

private void Button_Click(object Sender, EventArgs e)
{
MessageBox.Show(Sender.ToString(), e.ToString());//System.Windows.Forms.Button, Text:button1
//MessageBox.Show(((Button)Sender).Text, e.ToString());//button1
//标题为:System.Windows.Forms.MouseEventArgs或者为System.EventArgs
}



///
/// 遍历清空Form窗体上各种控件上的内容
///

/// 包含在控件内的控件集合
/// tabControl1.TabPages[0].Controls
public void ClearControlsData(Control.ControlCollection controls)
{
foreach (Control c in controls )
{
if (c.GetType().Name == "TextBox")
if (((TextBox)c).Visible == true)
((TextBox)c).Clear();
if (c.GetType().Name == "ComboBox")
if (((ComboBox)c).Visible == true)
((ComboBox)c).Text = "";
if (c.GetType().Name == "PictureBox")
if (((PictureBox)c).Visible == true)
((PictureBox)c).Image = null;
}
}
« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。