且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从名字中获取对象

更新时间:2023-11-30 23:07:34

这应该这样做:


TextBox someTextBox =

myForm.GetType()。GetFields(" MyTextBox")。GetValue(m yForm)为TextBox;


-mike

MVP


" Aamir Ghanchi" <一个******* @ discussions.microsoft.com>在消息中写道

news:04 **************************** @ phx.gbl ... 如何通过传递名称来获取对表单上控件对象的引用。像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