从名字中获取对象 [英] get object from name
问题描述
如何通过
传递它的名称来引用表单上的控件对象。类似
TextBox txt = SomeClass.SomeGetObjectMethod
(" MyTextBoxName");
wheras SomeClass and SomeGetObjectMethod就是我要找的b $ b。
提前谢谢。
Aamir
这应该这样做:
TextBox someTextBox =
myForm.GetType()。GetFields(" MyTextBox")。GetValue(m yForm)为TextBox;
-mike
MVP
" Aamir Ghanchi" <一个******* @ discussions.microsoft.com>在消息中写道
news:04 **************************** @ phx.gbl ... < blockquote class =post_quotes>如何通过传递名称来获取对表单上控件对象的引用。像TextBox txt = SomeClass.SomeGetObjectMethod
(MyTextBoxName);
wheras SomeClass和SomeGetObjectMethod是我正在寻找的东西。
提前致谢。
Aamir
对我不起作用。我想它应该是GetField(结尾没有
" s")但是即便如此它还没有工作并给出空引用
例外。
Aamir。
这应该这样做:
TextBox someTextBox =
myForm.GetType()。GetFields(" MyTextBox")。GetValue( m yForm)as TextBox;
Aamir< gh ***** @ bigfoot.com>写道:不适合我。我想它应该是GetField(没有
" s"结束),但即便如此它也不起作用并给出null引用
异常。
请发一个简短但完整的例子。请参阅
http://www.pobox.com/ ~siget / csharp / complete.html 我的意思是
这个。
-
Jon Skeet - < sk *** @ pobox.com>
http:// www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
How can I get reference to a control object on a form by
passing it''s name. something like
TextBox txt = SomeClass.SomeGetObjectMethod
("MyTextBoxName");
wheras SomeClass and SomeGetObjectMethod is what I am
looking for.
Thanks in advance.
Aamir
This should do it:
TextBox someTextBox =
myForm.GetType().GetFields("MyTextBox").GetValue(m yForm) as TextBox;
-mike
MVP
"Aamir Ghanchi" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...How can I get reference to a control object on a form by
passing it''s name. something like
TextBox txt = SomeClass.SomeGetObjectMethod
("MyTextBoxName");
wheras SomeClass and SomeGetObjectMethod is what I am
looking for.
Thanks in advance.
Aamir
Did not work for me. I guess it should be GetField (ending without
"s") but even then its not working and giving null reference
exception.
Aamir.
This should do it:
TextBox someTextBox =
myForm.GetType().GetFields("MyTextBox").GetValue(m yForm) as TextBox;
Aamir <gh*****@bigfoot.com> wrote:Did not work for me. I guess it should be GetField (ending without
"s") but even then its not working and giving null reference
exception.
Please post a short but complete example. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
this.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
这篇关于从名字中获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!