Answered by:
Writing Code for Radio Buttons, If certain text is in textbox

Question
-
Hi Good People
I have a form with 3 Radio Buttons that I have re-name (RBMR, RBMRS, RBMISS)
I also have a Textbox named (TxtGender)
When I click BindingSourceNavigator. Move next Item Or Previous Item and the Text changes in the textbox (TxtGender) And what ever value is in the Textbox (TxtGender) Could only be (Mr, Mrs, Miss)
I want to change which Radio Button Is checked to the corresponding text in Textbox (TxtGender)
I have tried Select case And other ways But All I get is all three Radio flashing and after a while An error. the code I have tried...
Private Sub TxtGender_TextChanged(sender As Object, e As EventArgs) Handles TxtGender.TextChanged 'I tried this ---------------------------------------------------- Dim Text As String = (TxtGender.Text) Select Case Text Case "Mr" RbMR.Checked = True Case "Mrs" RbMRS.Checked = True Case "Miss" RbMISS.Checked = True Case "" Dim radioButton = RbMR, Dim RadioButton = RbMRS, Dim radioButton = RbMISS, .Checked = False End Select 'I Tried this as well -------------------------------------------- If TxtGender.Text = "Mr".ToString Then RbMR.Checked = CheckState.Checked RbMRS.Checked = CheckState.Unchecked RbMISS.Checked = CheckState.Unchecked ElseIf TxtGender.Text = "Mrs".ToString Then RbMR.Checked = CheckState.Unchecked RbMRS.Checked = CheckState.Checked RbMISS.Checked = CheckState.Unchecked ElseIf TxtGender.Text = "Miss".ToString Then RbMR.Checked = CheckState.Unchecked RbMRS.Checked = CheckState.Unchecked RbMISS.Checked = CheckState.Checked Else RbMR.Checked = CheckState.Unchecked RbMRS.Checked = CheckState.Unchecked RbMISS.Checked = CheckState.Unchecked End If End Sub
Kind Regards
Gary
Gary Simpson
Sunday, November 29, 2020 11:20 PM
Answers
-
To avoid flashing and malfunction, try a possible approach:
Private Changing As Boolean = False Private Sub TxtGender_TextChanged(sender As Object, e As EventArgs) Handles TxtGender.TextChanged If Changing Then Exit Sub Changing = True Select Case TxtGender.Text.ToLower Case "mr" : RbMr.Checked = True Case "mrs" : RbMRS.Checked = True Case "miss" : RbMISS.Checked = True Case Else RbMr.Checked = False RbMRS.Checked = False RbMISS.Checked = False End Select Changing = False End Sub Private Sub RbMr_CheckedChanged(sender As Object, e As EventArgs) Handles RbMr.CheckedChanged If Changing Or Not RbMr.Checked Then Exit Sub Changing = True TxtGender.Text = "Mr" Changing = False End Sub Private Sub RbMRS_CheckedChanged(sender As Object, e As EventArgs) Handles RbMRS.CheckedChanged If Changing Or Not RbMRS.Checked Then Exit Sub Changing = True TxtGender.Text = "Mrs" Changing = False End Sub Private Sub RbMISS_CheckedChanged(sender As Object, e As EventArgs) Handles RbMISS.CheckedChanged If Changing Or Not RbMISS.Checked Then Exit Sub Changing = True TxtGender.Text = "Miss" Changing = False End Sub
- Edited by Viorel_MVP Monday, November 30, 2020 8:19 AM
- Marked as answer by Gary Simpson Monday, November 30, 2020 2:07 PM
Monday, November 30, 2020 8:15 AM -
Hi Karen
thank you for getting back to me,
So This cannot be done with a textbox changed event only, instead of what you are suggesting a Binding Source ?
Best regards
Gary
Gary Simpson
Sure it can but that does not make it the right way to go. Many time in this forum and other forums people tend to go with the least amount of code while sometimes that's fine while other times not.
And with that in mind I created a RadioButtonBinding custom control based on a GroupBox. It's in C# but could be done in VB.NET.
Going back to a TextBox, the number one reason I would not go there is validation concerns and it only works on that form, sure you may only need it for this one form but using a custom control not tied to a specific project and class is better.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
My GitHub code samples
GitHub pageCheck out: the new Microsoft Q&A forums
- Marked as answer by Gary Simpson Monday, November 30, 2020 9:21 PM
Monday, November 30, 2020 5:35 PM
All replies
-
Hello,
I put together a code sample. Note that after taking the screenshot I added a button to get the current person.
Example class
Imports System.ComponentModel Imports System.Runtime.CompilerServices Public Class Person Implements INotifyPropertyChanged Private _sex As String Public Property FirstName() As String Public Property LastName() As String Public Property Sex() As String Get Return _sex End Get Set(ByVal value As String) _sex = value OnPropertyChanged() End Set End Property Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional ByVal propertyName As String = Nothing) PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(propertyName)) End Sub End Class
Form code
Imports System.ComponentModel Public Class Form1 Private _peopleBindingSource As New BindingSource() Private _peopleBindingList As BindingList(Of Person) Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown Dim people = New List(Of Person) From { New Person() With { .FirstName = "Karen", .LastName = "Payne", .Sex = "F" }, New Person() With { .FirstName = "Bill", .LastName = "Smith", .Sex = "M" }, New Person() With { .FirstName = "Mary", .LastName = "Jones", .Sex = "F" }, New Person() With { .FirstName = "Kim", .LastName = "Adams", .Sex = "F" } } _peopleBindingList = New BindingList(Of Person)(people) _peopleBindingSource.DataSource = _peopleBindingList FirstNameTextBox.DataBindings.Add("Text", _peopleBindingSource, "FirstName") LastNameTextBox.DataBindings.Add("Text", _peopleBindingSource, "LastName") Dim maleBinding = New Binding("Checked", _peopleBindingSource, "Sex") AddHandler maleBinding.Format, AddressOf MaleBinding_Format AddHandler maleBinding.Parse, AddressOf MaleBinding_Parse AddHandler maleRadioButton.CheckedChanged, AddressOf Male_CheckedChanged maleRadioButton.DataBindings.Add(maleBinding) PeopleNavigator.BindingSource = _peopleBindingSource End Sub Private Sub Male_CheckedChanged(sender As Object, args As EventArgs) femaleRadioButton.Checked = Not maleRadioButton.Checked End Sub Private Sub MaleBinding_Parse(sender As Object, args As ConvertEventArgs) args.Value = If(DirectCast(args.Value, Boolean), "M", "F") End Sub Private Sub MaleBinding_Format(sender As Object, args As ConvertEventArgs) args.Value = (DirectCast(args.Value, String)) = "M" End Sub ''' <summary> ''' Validation that we have the right sex/gender ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> Private Sub CurrentButton_Click(sender As Object, e As EventArgs) Handles CurrentButton.Click If _peopleBindingSource.Current IsNot Nothing Then Dim person = _peopleBindingList.Item(_peopleBindingSource.Position) MessageBox.Show($"{person.FirstName}, {person.LastName} is a {person.Sex}") End If End Sub End Class
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
My GitHub code samples
GitHub pageCheck out: the new Microsoft Q&A forums
Monday, November 30, 2020 12:16 AM -
Hi
As well as Karen example, here is a quick version that may offer pointers.
This example uses a DataGridView to see the overall BindingNavigator at work.
Controls as per the comments at top of code.
' FORM1 with ' RadioButton1(RBMR), ' RadioButton2 (RBMRS), ' RadioButton2 (RBMISS), ' BindingNavigator1, DataGridView1 ' Option Strict On Option Explicit On Public Class Form1 Dim dt As New DataTable("Asset") Dim BS As New BindingSource Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load With dt .Columns.Add("RBMR", GetType(Boolean)) .Columns.Add("RBMRS", GetType(Boolean)) .Columns.Add("RBMISS", GetType(Boolean)) .Columns.Add("TxtGender", GetType(String)) With .Rows .Add(True, False, False, "MR") .Add(False, False, True, "MISS") .Add(False, True, False, "MRS") .Add(True, False, False, "MR") End With End With BS.DataSource = dt BindingNavigator1.BindingSource = BS DataGridView1.DataSource = BS RadioButton1.DataBindings.Add("Checked", BS, "RBMR") RadioButton2.DataBindings.Add("Checked", BS, "RBMRS") RadioButton3.DataBindings.Add("Checked", BS, "RBMISS") TxtGender.DataBindings.Add("Text", BS, "TxtGender") End Sub End Class
Regards Les, Livingston, Scotland
Monday, November 30, 2020 12:38 AM -
Hi Karen
thank you for getting back to me,
So This cannot be done with a textbox changed event only, instead of what you are suggesting a Binding Source ?
Best regards
Gary
Gary Simpson
Monday, November 30, 2020 12:58 AM -
Hi Les
Thank you for getting back to me,
Why can't this be done with a textbox change event, instead of a binding source.
Both You and Karen have made me think about adding to my SQL Data Base the columns for (Mr, Mrs, Miss) But I do not know if this will help my question about the Radio Buttons.
Best Regards
Gary
Gary Simpson
Monday, November 30, 2020 1:09 AM -
Hi Les
Thank you for getting back to me,
Why can't this be done with a textbox change event, instead of a binding source.
Both You and Karen have made me think about adding to my SQL Data Base the columns for (Mr, Mrs, Miss) But I do not know if this will help my question about the Radio Buttons.
Best Regards
Gary
Gary Simpson
Hi
OK, try this.
The other examples, the effects of changing an item would be reflected throughout - such as change a RadioButton and the BindingNavigator would follw and also the TextBox would follow etc etc etc. Doing it this way doesn't.
BTW: don't you already have a BindingSource for the BindingNavigator which would do everything Karen and I were thinking of.
' FORM1 with ' RadioButton1(RBMR), ' RadioButton2 (RBMRS), ' RadioButton2 (RBMISS), ' TextBox1 (TxtGender) Option Strict On Option Explicit On Public Class Form1 Private Sub TxtGender_TextChanged(sender As Object, e As EventArgs) Handles TxtGender.TextChanged Select Case TxtGender.Text.ToUpper Case "MR" RBMR.Checked = True Case "MRS" RBMRS.Checked = True Case "MISS" RBMISS.Checked = True End Select End Sub End Class
Regards Les, Livingston, Scotland
- Edited by leshay Monday, November 30, 2020 1:24 AM
Monday, November 30, 2020 1:21 AM -
Hi Les
My Code for inserting (Mr, Mrs, Miss) into the textbox (TxtGender) when clicking on a radio Button, three choices.
Private Sub RbMr_CheckedChanged(sender As Object, e As EventArgs) Handles RbMR.CheckedChanged TxtGender.Text = "Mr" End Sub Private Sub RbMRS_CheckedChanged(sender As Object, e As EventArgs) Handles RbMRS.CheckedChanged TxtGender.Text = "Mrs" End Sub Private Sub RbMISS_CheckedChanged(sender As Object, e As EventArgs) Handles RbMISS.CheckedChanged TxtGender.Text = "Miss" End Sub
Which means if I click on RBMR, Then (Mr will be put into textbox (TxtGender) I have tried your last suggestion and I still get the same result It keep flashing between all three radio buttons) The an error comes up.
Regards
Gary
Gary Simpson
Monday, November 30, 2020 1:39 AM -
To avoid flashing and malfunction, try a possible approach:
Private Changing As Boolean = False Private Sub TxtGender_TextChanged(sender As Object, e As EventArgs) Handles TxtGender.TextChanged If Changing Then Exit Sub Changing = True Select Case TxtGender.Text.ToLower Case "mr" : RbMr.Checked = True Case "mrs" : RbMRS.Checked = True Case "miss" : RbMISS.Checked = True Case Else RbMr.Checked = False RbMRS.Checked = False RbMISS.Checked = False End Select Changing = False End Sub Private Sub RbMr_CheckedChanged(sender As Object, e As EventArgs) Handles RbMr.CheckedChanged If Changing Or Not RbMr.Checked Then Exit Sub Changing = True TxtGender.Text = "Mr" Changing = False End Sub Private Sub RbMRS_CheckedChanged(sender As Object, e As EventArgs) Handles RbMRS.CheckedChanged If Changing Or Not RbMRS.Checked Then Exit Sub Changing = True TxtGender.Text = "Mrs" Changing = False End Sub Private Sub RbMISS_CheckedChanged(sender As Object, e As EventArgs) Handles RbMISS.CheckedChanged If Changing Or Not RbMISS.Checked Then Exit Sub Changing = True TxtGender.Text = "Miss" Changing = False End Sub
- Edited by Viorel_MVP Monday, November 30, 2020 8:19 AM
- Marked as answer by Gary Simpson Monday, November 30, 2020 2:07 PM
Monday, November 30, 2020 8:15 AM -
Hi Les
My Code for inserting (Mr, Mrs, Miss) into the textbox (TxtGender) when clicking on a radio Button, three choices.
Private Sub RbMr_CheckedChanged(sender As Object, e As EventArgs) Handles RbMR.CheckedChanged TxtGender.Text = "Mr" End Sub Private Sub RbMRS_CheckedChanged(sender As Object, e As EventArgs) Handles RbMRS.CheckedChanged TxtGender.Text = "Mrs" End Sub Private Sub RbMISS_CheckedChanged(sender As Object, e As EventArgs) Handles RbMISS.CheckedChanged TxtGender.Text = "Miss" End Sub
Which means if I click on RBMR, Then (Mr will be put into textbox (TxtGender) I have tried your last suggestion and I still get the same result It keep flashing between all three radio buttons) The an error comes up.
Regards
Gary
Gary Simpson
Hi
It seems you are now saying that you want the Radio Buttons to change the contents of the TextBox instead of the TextBox changing the Radio Button states!
Very confusing - what exactly do you want to do/happen?
Regards Les, Livingston, Scotland
Monday, November 30, 2020 1:37 PM -
Hi Viorel
Thank you for getting back to me, Your code does stop the error by stopping the flashing between all three radio buttons.
I guess I will stick with your sample code You kindly supplied.
Really I was trying to select the Radio Button to what ever the text was in the textbox (TxtGender).
With the code I had, And when I used the Binding source buttons, One radio button (RBMR) was checked even Though the textbox (TxtGender) value was Mrs or Miss. But with your code None of the buttons are checked. I can live with that
Thank you Viorel your input is Much appreciated.
Best Regards
Gary
Gary Simpson
Monday, November 30, 2020 2:07 PM -
Hi Karen
thank you for getting back to me,
So This cannot be done with a textbox changed event only, instead of what you are suggesting a Binding Source ?
Best regards
Gary
Gary Simpson
Sure it can but that does not make it the right way to go. Many time in this forum and other forums people tend to go with the least amount of code while sometimes that's fine while other times not.
And with that in mind I created a RadioButtonBinding custom control based on a GroupBox. It's in C# but could be done in VB.NET.
Going back to a TextBox, the number one reason I would not go there is validation concerns and it only works on that form, sure you may only need it for this one form but using a custom control not tied to a specific project and class is better.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
My GitHub code samples
GitHub pageCheck out: the new Microsoft Q&A forums
- Marked as answer by Gary Simpson Monday, November 30, 2020 9:21 PM
Monday, November 30, 2020 5:35 PM