如何通过变量访问窗体控件?VB.NET窗体控件引用问题
解决WinForms动态引用窗体控件的问题
你的核心问题是:targetForm被声明为Form基类类型,编译器无法识别具体窗体上的DeptCombox控件,而且你传入的myControl参数也没被用到。下面给你两种直接可用的解决方案:
方案1:用控件集合查找(推荐)
WinForms的控件集合自带按名称查找的方法,支持递归查找子控件(比如放在Panel、GroupBox里的控件也能找到),代码修改如下:
Public Function DeptList(ByVal myForm As String, ByVal myControl As String) Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly() Dim targetForm As Form = CType(asm.CreateInstance("Jigsaw2." & myForm), Form) SqlTxt = "SELECT * FROM Department WHERE Dept_Status = 'Active';" sql.ExecQuery(SqlTxt) ' 按控件名称查找,True表示递归查找所有子控件 Dim foundControls() As Control = targetForm.Controls.Find(myControl, True) If foundControls.Length > 0 Then ' 将找到的控件转换为ComboBox类型 Dim targetCombo As ComboBox = CType(foundControls(0), ComboBox) Try For Each r As DataRow In sql.DBDT.Rows targetCombo.Items.Add(r("Dept_Name")) Next Catch ex As Exception ' 可选:添加异常提示,方便调试 MessageBox.Show("填充失败:" & ex.Message) End Try Else MessageBox.Show("未找到名为 " & myControl & " 的控件") End If End Function
方案2:用反射获取控件属性
如果你的控件是直接拖到窗体上的(成为窗体的成员属性),可以用反射直接获取这个属性:
Public Function DeptList(ByVal myForm As String, ByVal myControl As String) Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly() ' 用Object类型存储窗体实例,避免基类的类型限制 Dim targetForm As Object = asm.CreateInstance("Jigsaw2." & myForm) SqlTxt = "SELECT * FROM Department WHERE Dept_Status = 'Active';" sql.ExecQuery(SqlTxt) Try ' 通过反射获取控件属性 Dim comboProp As System.Reflection.PropertyInfo = targetForm.GetType().GetProperty(myControl) If comboProp IsNot Nothing Then Dim targetCombo As ComboBox = CType(comboProp.GetValue(targetForm), ComboBox) For Each r As DataRow In sql.DBDT.Rows targetCombo.Items.Add(r("Dept_Name")) Next Else MessageBox.Show("窗体上不存在名为 " & myControl & " 的控件属性") End If Catch ex As Exception MessageBox.Show("填充失败:" & ex.Message) End Try End Function
额外提醒:窗体实例的问题
你当前代码是用CreateInstance创建了一个新的窗体实例,如果你的需求是给已经打开的窗体填充控件,那应该替换创建实例的代码,改为查找已打开的窗体:
' 替换原来的CreateInstance代码 Dim targetForm As Form = Application.OpenForms.OfType(Of Form).FirstOrDefault(Function(f) f.Name = myForm) If targetForm Is Nothing Then MessageBox.Show("未找到已打开的 " & myForm & " 窗体") Return End If
内容的提问来源于stack exchange,提问作者Clamont
相关产品推荐
相关产品推荐

