简体   繁体   中英

How to bind radio button in VB.net and C#?

I have a problem about binding a form property to a datasource. The solution is given here(Please see): Radio Buttons And Databinding in vb.net

The problem is, I do not know how to follow the instruction- "...and then you can simply bind the CarpetColor property on your form to the data source's CarpetColor." Can you show me how to do it?

Also, for some reason, I cannot use the "INotifyPropertyChanged" interface (cannot be detected by intellisense). Do I have to import any namespace?

Edit:

enum Gender { Male,Female }
        public Gender _Gender
        {
            get { 
                if(radMale.Checked == true)
                    return Gender.Male;
               else
                    return Gender.Female;
            }
            set
            {
                if (value == Gender.Male)
                    radMale.Checked = true;
                else
                    radFemale.Checked = true;
            }
        }

This is a portion of my code. In my case, I am trying to make a simple student information with CRUD methods. I am stucked on how to bind a radio button but I found out that I can implement it using a form's property instead. Binding to any other control such as textboxes and comboboxes is fairly easy. In my case, I want to bind a form property .

So after a long while, I finally figured out how to solve the problem.

Actually, I have done the coding in VB.net since I am more familiar with the VB syntax instead of C#'s. Nonetheless, the logic is pretty similar to each other since they are bounded by the .NET framework.

The form property goes as follows (though enumerations in the original post is also possible.):

Public Property propGender() As String
    Get
        If RadioButton1.Checked = True Then
            Return "M"
        Else
            Return "F"
        End If
    End Get
    Set(ByVal value As String)
        _Sex = value
        If _Sex = "M" Then
            RadioButton1.Checked = True
        Else
            RadioButton2.Checked = True
        End If
    End Set
End Property

That is trivial, but I only included that code snippet for further clarifications.

The answer on how to data-bind a form property is simply:

Dim genderBinder = New Binding("propGender", Tbl_Stud_InfoBindingSource, "Gender")
Me.DataBindings.Add(genderBinder)
Me.DataBindings.Add(genderBinder)

where Gender is the data member I need. Adjusting the form properties with the use of the radio buttons is where I get confused(even until now, honestly). This problem was solved by implementing the INotifyPropertyChanged interface. I previously thought that this is implicitly included in every project, but easily found out that System.ComponentModel should be imported first. Thanks to MSDN .

Imports System.ComponentModel

Raising the protertyChanged event can be accomplished by:

Protected Sub OnPropertyChanged(ByVal name As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
    OnPropertyChanged("propGender")
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
    OnPropertyChanged("propGender")
End Sub

where RadioButton1 = radiobox for male and RadioButton2 = female (better to follow good naming conventions instead!).

I am not that familiar with C# syntax but think that the equivalent code for raising checkChanged property is:

RadioButton1.CheckedChanged += (s, args) => OnPropertyChanged("propGender");
RadioButton2.CheckedChanged += (s, args) => OnPropertyChanged("propGender");

Finally, in this way, I can now easily manage to bind radio buttons - I know it can be easy to solve this but a common question by beginners like me. In VB6, this can be implemented easily with the use of the popular recordsets. In .NET, the solution is not apparent.

在此处输入图片说明

Edit: I saw some other questions pertaining to the same scenario here in SO. I found two, but they are both using C#, which may be fuzzy for VB users.

Edit: The missing key terminology I used to figure out is simply "Data binding radio button". That's why I want to change the title of this question to make it more definitive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM