WinForm 如何获取动态生成控件的属性

fino_wang 2008-01-30 03:08:59
代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
newCB();
newBT();
}

//动态生成ComboBox
private void newCB()
{
ComboBox cb = new ComboBox();
cb.Location = new Point(new Size(50, 50));
cb.Text="aaaaa";
this.Controls.Add(cb);
}

//动态生成Button
private void newBT()
{
Button bt = new Button();
bt.Location = new Point(new Size(200, 50));
bt.Text = "ck";
this.Controls.Add(bt);
bt.Click += new System.EventHandler(this.bt_Click);
}

private void bt_Click(object sender, EventArgs e)
{
//如果我想获取bt.Text的值和cb.Text的值,应该怎样写后面的代码呢?

}
}
}


谢谢大家,请大家帮帮忙!
...全文
345 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fino_wang 2008-01-31
  • 打赏
  • 举报
回复
哦 这样啊 不好意思 以前发过不少帖子,但是都不懂,谢谢hbxhlhx啊!
大家不好意思啊 这回补上
北京的雾霾天 2008-01-30
  • 打赏
  • 举报
回复
麻烦版主结贴,谢谢!
--------------------
如果是版主结,那就是强制结帖子了,楼主自己结吧。点上面的“管理帖子”,然后。。。
fino_wang 2008-01-30
  • 打赏
  • 举报
回复
谢谢楼上所有的朋友,又学到东西了。真是麻烦大家了!实在是黔驴技穷了。
再次谢谢大家!
麻烦版主结贴,谢谢!
  • 打赏
  • 举报
回复
public partial class Form1 : Form
{
private ComboBox cb;
private Button bt;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
newCB();
newBT();
}

//动态生成ComboBox
private void newCB()
{
cb = new ComboBox();
cb.Location = new Point(new Size(50, 50));
cb.Text="aaaaa";
this.Controls.Add(cb);
}

//动态生成Button
private void newBT()
{
bt = new Button();
bt.Location = new Point(new Size(200, 50));
bt.Text = "ck";
this.Controls.Add(bt);
bt.Click += new System.EventHandler(this.bt_Click);
}

private void bt_Click(object sender, EventArgs e)
{
//想获取bt.Text的值和cb.Text的值,直接引用!

}
}
北京的雾霾天 2008-01-30
  • 打赏
  • 举报
回复
在this.Controls里循环查找控件,若是要找的控件则显示出控件的Text就是了
he_8134 2008-01-30
  • 打赏
  • 举报
回复
using   System; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
newBT().Tag=newCB();
}

//动态生成ComboBox
private Control newCB()
{
ComboBox cb = new ComboBox();
cb.Location = new Point(new Size(50, 50));
cb.Text="aaaaa";
this.Controls.Add(cb);
return cb;
}

//动态生成Button
private Control newBT()
{
Button bt = new Button();
bt.Location = new Point(new Size(200, 50));
bt.Text = "ck";
this.Controls.Add(bt);
bt.Click += new System.EventHandler(this.bt_Click);
return bt;
}

private void bt_Click(object sender, EventArgs e)
{
Button bt=sender as Button;
ComboBox cb =bt.Tag as ComboBox;
}
}
}
manonroad 2008-01-30
  • 打赏
  • 举报
回复
重贴一下

//将sender转换成button。
Button btn = sender as Button;
if(btn != null)
{
Text btn_Text = btn.Text;
//对于ComboBox相对复杂一点,也许你需要this.Controls中找。
foreach(Control ctr in this.Controls)
{
if(ctr is ComboBox)
{
ComboxBox cbx = ctr as ComboBox;
MessageBox.Show(cbx.Text);
}
}
manonroad 2008-01-30
  • 打赏
  • 举报
回复
[code=C#]]
//将sender转换成button。
Button btn = sender as Button;
if(btn != null)
{
Text btn_Text = btn.Text;
//对于ComboBox相对复杂一点,也许你需要this.Controls中找。
foreach(Control ctr in this.Controls)
{
if(ctr is ComboBox)
{
ComboxBox cbx = ctr as ComboBox;
MessageBox.Show(cbx.Text);
}
}[/code

110,534

社区成员

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

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

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