积极答复者
通过窗体名称给窗体内控件赋值,请教,谢谢!!!

问题
-
Form2里有Textbox1
Form1里有Button1
下面这句能为Form2里Textbox1赋值(求教的是如何也用窗体的的名称来确定该窗体,即Form2.Controls能否用窗体的名称来获取,如 Form2为字符串)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CallByName(Form2.Controls("Textbox1"), "Text",vbLet, "の名前")
END SUB
答案
全部回复
-
你好,
你可以使用Visual Studio自带工具ildasm.exe来查看你这些代码的中间语言(Immediate language),其中这个方法的代码如下所示:
.method private instance void Button1_Click(object sender, class [mscorlib]System.EventArgs e) cil managed { // Code size 57 (0x39) .maxstack 6 .locals init ([0] object[] VB$t_array$S0) IL_0000: nop IL_0001: call class WindowsApplication1.My.MyProject/MyForms WindowsApplication1.My.MyProject::get_Forms() IL_0006: callvirt instance class WindowsApplication1.Form2 WindowsApplication1.My.MyProject/MyForms::get_Form2() IL_000b: callvirt instance class [System.Windows.Forms]System.Windows.Forms.Control/ControlCollection [System.Windows.Forms]System.Windows.Forms.Control::get_Controls() IL_0010: ldstr "Textbox1" IL_0015: callvirt instance class [System.Windows.Forms]System.Windows.Forms.Control [System.Windows.Forms]System.Windows.Forms.Control/ControlCollection::get_Item(string) IL_001a: ldstr "Text" IL_001f: ldc.i4.4 IL_0020: ldc.i4.1 IL_0021: newarr [mscorlib]System.Object IL_0026: stloc.0 IL_0027: ldloc.0 IL_0028: ldc.i4.0 IL_0029: ldstr bytearray (6E 30 0D 54 4D 52 ) // n0.TMR IL_002e: stelem.ref IL_002f: nop IL_0030: ldloc.0 IL_0031: call object [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Versioned::CallByName(object, string, valuetype [Microsoft.VisualBasic]Microsoft.VisualBasic.CallType, object[]) IL_0036: pop IL_0037: nop IL_0038: ret } // end of method Form1::Button1_Click
会看到编译器在编译代码的时候,调用getForm2方法来确定这个Object。这个文档可以帮助你阅读以上代码:http://msdn.microsoft.com/zh-cn/library/812xyxy2.aspx.
如果我对你的问题理解有误,请告诉我,谢谢。
Best regards,
Shanks Zen
MSDN Community Support | Feedback to us
-
谢谢您!!!我用Controls控件合集可直接给集合内任意(用控件的名称)控件的属性赋值,我想是不是也可用窗体合集也能给实现相同功能,谢谢!!!
这里的Controls是Form类的一个属性,其类型为Control.ControlCollection,返回值是一个包含Form里面所有空间的集合。但是并没有这样一个属性是包含所有窗体的,所有你这种思路用常规方法可能是无法实现的。或许可以用反射之类的可以实现。
Best regards,
Shanks Zen
MSDN Community Support | Feedback to us