无法从另一个窗体更改用户控件中组合框的值
Can not change the value of a combobox in an user control from another form
我在 Autocad 中有一个用户控件,可以根据特定标准绘制图形。
从这个用户控件中,可以打开另一个用于填写特定数据的表单。
当我尝试使用表单中的数据自动更改用户控件中的组合框时,出现错误:
Reference to a non-shared member requires an object reference.
现在我在网上查了这个,但我似乎无法应用给出的解决方案。
我的情况如下:
Partial Public Class Partial Public Class User_Control
Private Form1 As New Form_Something
private sub button1_click(sender As Object, e As EventArgs) Handles button1.Click
Form1.show()
End sub
End Class
此用户控件有一个组合框,我们将其命名为 Combobox1。现在,当我更改 Form_Something 上的值时,Combobox1 也必须更改。
所以我在 Form1 中得到了以下代码:
Partial public class Form_Something
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles BT_Apply.Click
User_Control.Combobox1.text = "Apples"
End sub
End class
这给出了提到的错误。
有人可以帮我解决这个问题吗,因为我想用多个 comboboxes/labels 等
来解决这个问题
为了更直观的给大家看,我这里改了UserControl1
的BorderStyle
。您可以根据需要对其进行更改或评论。
我已经对代码的一些关键部分进行了注释,如果您仍然感到困惑,请告诉我。
如果我的 post 有助于解决您的问题,请点击那个 post 的 "Mark as Answer"。通过将 post 标记为已回答或有帮助,您可以帮助其他人更快地找到答案。
结果如下:
我的代码如下:
1.Form1
代码:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs)
MyUserControl.ComboBox1.Text = "Apples"
End Sub
End Class
2.UserControl1
代码:
Public Class UserControl1
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Text = "Apple"
Me.BorderStyle = BorderStyle.Fixed3D
End Sub
Dim myForm As Form
Dim btn As Button
Dim txtbox As TextBox
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
myForm = New Form With {.Text = "myForm"}
btn = New Button With {.Text = "Save", .Height = 30, .Width = 60, .Top = 100, .Left = 150}
txtbox = New TextBox With {.Text = "", .Height = 20, .Width = 100, .Top = 100, .Left = 20}
myForm.Controls.Add(btn)
myForm.Controls.Add(txtbox)
myForm.Show() 'Open dialog in modeless window
AddHandler btn.Click, AddressOf Btn_Click
End Sub
Sub Btn_Click(sender As System.Object, e As System.EventArgs)
Me.ComboBox1.Text = txtbox.Text 'This step is important!
myForm.Close()
End Sub
End Class
我在 Autocad 中有一个用户控件,可以根据特定标准绘制图形。 从这个用户控件中,可以打开另一个用于填写特定数据的表单。 当我尝试使用表单中的数据自动更改用户控件中的组合框时,出现错误:
Reference to a non-shared member requires an object reference.
现在我在网上查了这个,但我似乎无法应用给出的解决方案。 我的情况如下:
Partial Public Class Partial Public Class User_Control
Private Form1 As New Form_Something
private sub button1_click(sender As Object, e As EventArgs) Handles button1.Click
Form1.show()
End sub
End Class
此用户控件有一个组合框,我们将其命名为 Combobox1。现在,当我更改 Form_Something 上的值时,Combobox1 也必须更改。 所以我在 Form1 中得到了以下代码:
Partial public class Form_Something
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles BT_Apply.Click
User_Control.Combobox1.text = "Apples"
End sub
End class
这给出了提到的错误。 有人可以帮我解决这个问题吗,因为我想用多个 comboboxes/labels 等
来解决这个问题为了更直观的给大家看,我这里改了UserControl1
的BorderStyle
。您可以根据需要对其进行更改或评论。
我已经对代码的一些关键部分进行了注释,如果您仍然感到困惑,请告诉我。
如果我的 post 有助于解决您的问题,请点击那个 post 的 "Mark as Answer"。通过将 post 标记为已回答或有帮助,您可以帮助其他人更快地找到答案。
结果如下:
我的代码如下:
1.Form1
代码:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs)
MyUserControl.ComboBox1.Text = "Apples"
End Sub
End Class
2.UserControl1
代码:
Public Class UserControl1
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Text = "Apple"
Me.BorderStyle = BorderStyle.Fixed3D
End Sub
Dim myForm As Form
Dim btn As Button
Dim txtbox As TextBox
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
myForm = New Form With {.Text = "myForm"}
btn = New Button With {.Text = "Save", .Height = 30, .Width = 60, .Top = 100, .Left = 150}
txtbox = New TextBox With {.Text = "", .Height = 20, .Width = 100, .Top = 100, .Left = 20}
myForm.Controls.Add(btn)
myForm.Controls.Add(txtbox)
myForm.Show() 'Open dialog in modeless window
AddHandler btn.Click, AddressOf Btn_Click
End Sub
Sub Btn_Click(sender As System.Object, e As System.EventArgs)
Me.ComboBox1.Text = txtbox.Text 'This step is important!
myForm.Close()
End Sub
End Class