Winform中通过一个字符串定位到和字符串相等ID的控件(将字符串转换成相应的控件名称
2010-09-24 00:00
483 查看
方法一:
朋友说的方法倒能够实现,但如果控件很多,而且每次都要动态判断,性能方面就回受到很大影响,这也不时为一个办法.
string
controlName
=
"
控 件名称
"
;
foreach
(control con
in
父容 器.Controls)
{
if (con.Name == controlName)
{
///找到控件了;
///然后将其拆箱,就OK了.
}
}
上面的方法如果在一个窗体里面查找某个控件则父容器就是this如下,如果是其他容器控件则为某容器的ID.
Control.ControlCollection controls
=
this
.Controls;
foreach
(Control myControl
in
controls)
{
if (myControl.Name == "radioButton1")
{
RadioButton radBtn = (RadioButton)myControl;
radBtn.Checked = true;
}
}
方法二:
CSDN朋友帮助:
string
str
=
"
button
"
+
"
1
"
;
//
必须已经有button1控件否则会出错
Button btn1
=
(Button)
this
.Controls.Find(str,
true
)[
];
if
(btn1
==
button1)
MessageBox.Show(btn1.Text);
其实通过上面的转换btn1和button1是同一个控件了,其他容器控件也有同样的Find方法,这样也不错..
其他更好的方法有待进一步研究...
朋友说的方法倒能够实现,但如果控件很多,而且每次都要动态判断,性能方面就回受到很大影响,这也不时为一个办法.
string
controlName
=
"
控 件名称
"
;
foreach
(control con
in
父容 器.Controls)
{
if (con.Name == controlName)
{
///找到控件了;
///然后将其拆箱,就OK了.
}
}
上面的方法如果在一个窗体里面查找某个控件则父容器就是this如下,如果是其他容器控件则为某容器的ID.
Control.ControlCollection controls
=
this
.Controls;
foreach
(Control myControl
in
controls)
{
if (myControl.Name == "radioButton1")
{
RadioButton radBtn = (RadioButton)myControl;
radBtn.Checked = true;
}
}
方法二:
CSDN朋友帮助:
string
str
=
"
button
"
+
"
1
"
;
//
必须已经有button1控件否则会出错
Button btn1
=
(Button)
this
.Controls.Find(str,
true
)[
];
if
(btn1
==
button1)
MessageBox.Show(btn1.Text);
其实通过上面的转换btn1和button1是同一个控件了,其他容器控件也有同样的Find方法,这样也不错..
其他更好的方法有待进一步研究...
相关文章推荐
- Winform中通过一个字符串定位到和字符串相等ID的控件(将字符串转换成相应的控件名称)
- Winform中通过一个字符串定位到和字符串相等ID的控件(将字符串转换成相应的控件名称)
- Winform中通过一个字符串定位到和字符串相等ID的控件(将字符串转换成相应的控件名称)
- Winform中通过一个字符串定位到和字符串相等ID的控件(将字符串转换成相应的控件名称)
- 把一个字符串通过递归转换成相应数字
- 把一个字符串通过递归转换成相应数字
- 通过python调用adb命令实现用元素名称、id、class定位元素
- C#源代码—在windows窗体中添加一个名为lblShow的Lable控件4,创建可变字符串,字符串转换为字符型
- 如何通过字符串指定的窗口名称,获取该窗口内的控件
- 将一个字符串映射为一个Delphi页面控件属性名(通过FindComponent和GetPropInfo找到这个控件指针)
- 通过python调用adb命令实现用元素名称、id、class定位元素
- 用php编写一个函数实现 以下功能:字符串hello_world转换成为HelloWorld, get_field_by_id转换成为GetFieldById
- Android 通过resource名称或者layout中定义的id name 去获取R.java中id,得到控件
- WindowsForm通过字符串名称实例化控件
- android 通过字符串名称 检索资源ID或String
- 在WM_COMMAND中,lParam用来区分是命令消息还是控件通知消息:如果lParam为NULL,则这是个命令消息,否则lParam里面放的必然就是控件的句柄,是一个控件通知消息。对于wParam则是低位放的是控件ID,高位放的是相应的消息事件。
- 练习 3-2 编写一个函数escape(s, t),将字符串t复制到字符串s中,并在复制过程中将换行符、制表符等不可见字符分别转换为\n、\t等相应的可见的转义字符序列。要求使用swich语句。
- 请写一个函数,实现以下功能: 字符串“open_door” 转换成 “OpenDoor”、”make_by_id” 转换成 ”MakeById”
- Android通过崩溃日志中控件id定位崩溃位置的方法
- QStringLiteral(源代码里有一个通过构造函数产生的从const char*到QString的隐式转换,QStringLiteral字符串可以放在代码的任何地方,编译期直接生成utf16字符串,速度很快,体积变大)