【发布时间】:2017-05-25 23:55:47
【问题描述】:
我正在尝试为Combobox 添加绑定class property。该属性是一个枚举。我设法将项目从enum 加载到Combobox 可能有点不正统,但它的工作原理。 (也欢迎改进建议)然后当表单加载时Combobox 显示活动性别,所有选项都在下拉菜单中。
但是当我将 Combobox 的焦点更改为 button 以测试结果时,它会恢复为男性(在我的测试中,我想将性别更改为女性)。
如何确保将新的性别值传递给我的对象?
这是我的代码示例
Public Class Form1
Private Personobject As Person
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Personobject = New Person
Personobject.Gender = GenderEnum.Male
Dim enumType As Type = GetType(GenderEnum)
Dim names() As String = [Enum].GetNames(enumType)
For Each s As String In names
ComboBox1.Items.Add(s)
Next
ComboBox1.DataBindings.Add("Text", Personobject, "Gender")
End Sub
Private Enum GenderEnum
Male
Female
End Enum
Private Class Person
Public Property Gender As GenderEnum
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Debug.Print(Personobject.Gender.ToString)
End Sub
End Class
【问题讨论】:
标签: vb.net class oop combobox binding