|
-
Mar 2nd, 2020, 03:06 PM
#1
Thread Starter
Lively Member
VB.net Programming Bound Group of Radio buttons
I have a form with fields bound to a data source. One of the fields is a bit true/false. I am having a problem with creating a simple No/Yes radio buttons. In Access you create a group and assign numbers then check the group for changes. I am assuming you cant do that with VB.Net. Does anyone have a example of how to use grouped radio buttons to update bound data. I have searched for hours and found many examples of how to group them, change the screen color, display a message telling which is picked when the user clicks on a button. All I want to do is update the bound

Code:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING OFF
GO
CREATE TABLE [dbo].[OrderDetail](
[PartDesc] [varchar](50) NOT NULL,
[PartOrdered] [bit] NULL,
[OrderDate] [datetime] NULL,
[ReceivedDate] [datetime] NULL,
CONSTRAINT [PK_PartDesc] PRIMARY KEY CLUSTERED
(
[PartDesc] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[OrderDetail] ADD DEFAULT ((0)) FOR [ReceivedDate]
GO
-
Mar 2nd, 2020, 04:51 PM
#2
Re: VB.net Programming Bound Group of Radio buttons
Where's the code you use binding the controls?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 2nd, 2020, 06:13 PM
#3
Re: VB.net Programming Bound Group of Radio buttons
You can't bind RadioButtons like that. What you could do is create your own user control that contains the two RadioButtons and then expose a single Boolean property that you can bind to. Internally, the property would check a different RadioButton based on whether that property was True or False.
-
Mar 3rd, 2020, 12:57 AM
#4
Re: VB.net Programming Bound Group of Radio buttons
This is an extended GroupBox containing YesNo RadioButtons. The default value is No (False).
The GroupValue Property exposes rbYes (the Yes RadioButton), which you can bind to. As there are two RadioButtons in a container, only one can be Checked at any time, so the GroupValue.Checked Property is always either No (False) or Yes (True).
Code:
Public Class extendedYesNoGroupbox
Inherits GroupBox
Private rbNo As New RadioButton
Private rbYes As New RadioButton
Public Sub New()
Me.Size = New Size(110, 40)
With rbNo
.Text = "No"
.AutoSize = True
.Location = New Point(12, 17)
.Checked = True
End With
Me.Controls.Add(rbNo)
With rbYes
.Text = "Yes"
.AutoSize = True
.Location = New Point(62, 17)
End With
Me.Controls.Add(rbYes)
End Sub
Public ReadOnly Property GroupValue() As RadioButton
Get
Return rbYes
End Get
End Property
Public Overrides Property Text() As String
Get
If MyBase.Text.StartsWith("ExtendedYesNoGroupbox") Then
Return "YesNo"
Else
Return MyBase.Text
End If
End Get
Set(ByVal value As String)
MyBase.Text = value
End Set
End Property
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 3rd, 2020, 01:51 AM
#5
Re: VB.net Programming Bound Group of Radio buttons
 Originally Posted by .paul.
This is an extended GroupBox containing YesNo RadioButtons. The default value is No (False).
The GroupValue Property exposes rbYes (the Yes RadioButton), which you can bind to. As there are two RadioButtons in a container, only one can be Checked at any time, so the GroupValue.Checked Property is always either No (False) or Yes (True).
Code:
Public Class extendedYesNoGroupbox
Inherits GroupBox
Private rbNo As New RadioButton
Private rbYes As New RadioButton
Public Sub New()
Me.Size = New Size(110, 40)
With rbNo
.Text = "No"
.AutoSize = True
.Location = New Point(12, 17)
.Checked = True
End With
Me.Controls.Add(rbNo)
With rbYes
.Text = "Yes"
.AutoSize = True
.Location = New Point(62, 17)
End With
Me.Controls.Add(rbYes)
End Sub
Public ReadOnly Property GroupValue() As RadioButton
Get
Return rbYes
End Get
End Property
Public Overrides Property Text() As String
Get
If MyBase.Text.StartsWith("ExtendedYesNoGroupbox") Then
Return "YesNo"
Else
Return MyBase.Text
End If
End Get
Set(ByVal value As String)
MyBase.Text = value
End Set
End Property
End Class
How would you check the No option via code? There's nothing in there that would do that. You'd have to internally handle the CheckedChanged event of the Yes option and check the other when it was unchecked. That's a bit dodgy though. Rather than exposing either of the RadioButtons, which means that you could theoretically move it or make any other change you wanted to it, it would be better to expose just a Boolean property, given that's all that's actually needed, and have that check the appropriate RadioButton internally.
-
Mar 3rd, 2020, 02:01 AM
#6
Re: VB.net Programming Bound Group of Radio buttons
Have you tried binding an extended groupbox property? Either I was missing something I needed to implement or it’s not possible, at least with the DataBindings.Add method...
Setting the Checked property of rbNo as True by default means there will always be one RadioButton Checked
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 3rd, 2020, 02:09 AM
#7
Re: VB.net Programming Bound Group of Radio buttons
 Originally Posted by .paul.
Setting the Checked property of rbNo as True by default means there will always be one RadioButton Checked
But if you were to check rbYes, you could never check rbNo again.
-
Mar 3rd, 2020, 02:19 AM
#8
Re: VB.net Programming Bound Group of Radio buttons
 Originally Posted by .paul.
Have you tried binding an extended groupbox property? Either I was missing something I needed to implement or it’s not possible, at least with the DataBindings.Add method...
Changed your code to this:
vb.net Code:
Public Class extendedYesNoGroupbox Inherits GroupBox Private rbNo As New RadioButton Private rbYes As New RadioButton Public Sub New() Me.Size = New Size(110, 40) With rbNo .Text = "No" .AutoSize = True .Location = New Point(12, 17) .Checked = True End With Me.Controls.Add(rbNo) With rbYes .Text = "Yes" .AutoSize = True .Location = New Point(62, 17) End With Me.Controls.Add(rbYes) End Sub Public Property IsYes As Boolean Get Return rbYes.Checked End Get Set(value As Boolean) If value Then rbYes.Checked = True Else rbNo.Checked = True End If End Set End Property Public Overrides Property Text() As String Get If MyBase.Text.StartsWith("ExtendedYesNoGroupbox") Then Return "YesNo" Else Return MyBase.Text End If End Get Set(ByVal value As String) MyBase.Text = value End Set End Property End Class
and then did this in a form:
vb.net Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ExtendedYesNoGroupbox1.DataBindings.Add("IsYes", CheckBox1, "Checked") End Sub
Checking and unchecking the CheckBox then checked and unchecked the RadioButtons as expected.
-
Mar 3rd, 2020, 02:46 AM
#9
Re: VB.net Programming Bound Group of Radio buttons
I tried something similar... Didn't work, but IMO the extended GroupBox is a neat idea.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 3rd, 2020, 02:48 AM
#10
Re: VB.net Programming Bound Group of Radio buttons
I forget now, but i think i just got bound up in my databindings
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 3rd, 2020, 03:38 PM
#11
Thread Starter
Lively Member
Re: VB.net Programming Bound Group of Radio buttons
I tried the extended property code but couldn't get it to work. I did manage to get it working using direct Binding. Not as elegant as the user control or extended property idea.
Code:
Private Sub SerRequest_Shown
If Me.Laser_Maint_LogBindingSource.Current("PartOrdered") = "False" Then
rbNo.Checked = True
rbYes.Checked = False
Else
rbNo.Checked = False
rbYes.Checked = True
End If
End Sub
Private Sub Laser_Maint_LogBindingSource_PositionChanged
' ** This will go away as this is a single record called form
If Me.Laser_Maint_LogBindingSource.Current("PartOrdered") = "False" Then
rbNo.Checked = True
rbYes.Checked = False
Else
rbNo.Checked = False
rbYes.Checked = True
End If
End Sub
Private Sub rbNo_CheckedChanged(sender As Object, e As EventArgs) Handles rbNo.CheckedChanged
If rbNo.Checked = True Then
Me.Laser_Maint_LogBindingSource.Current("PartOrdered") = "False"
Else
Me.Laser_Maint_LogBindingSource.Current("PartOrdered") = "True"
End If
End Sub
Last edited by wjburke2; Mar 4th, 2020 at 07:57 AM.
-
Mar 3rd, 2020, 03:42 PM
#12
Re: VB.net Programming Bound Group of Radio buttons
tl;dr ....
For yes/no things like that I try to avoid radio buttons if I can... I don't find them very intuitive. Checkboxes on the other hand are easy to understand and bind very well to bit/boolean fields.
-tg
-
Mar 3rd, 2020, 04:11 PM
#13
Thread Starter
Lively Member
Re: VB.net Programming Bound Group of Radio buttons
You are right I told the user a single checkbox would be better they insisted on yes no buttons. Thank s for all you replies.
-
Mar 3rd, 2020, 05:27 PM
#14
Re: VB.net Programming Bound Group of Radio buttons
 Originally Posted by wjburke2
I am defiantly going to try the extended property, Thanks. I did manage to get it working using direct Binding. Not a elegant as the user control or extended property idea.
Code:
Private Sub SerRequest_Shown
If Me.Laser_Maint_LogBindingSource.Current("PartOrdered") = "False" Then
rbNo.Checked = True
rbYes.Checked = False
Else
rbNo.Checked = False
rbYes.Checked = True
End If
End Sub
Private Sub Laser_Maint_LogBindingSource_PositionChanged
' ** This will go away as this is a single record called form
If Me.Laser_Maint_LogBindingSource.Current("PartOrdered") = "False" Then
rbNo.Checked = True
rbYes.Checked = False
Else
rbNo.Checked = False
rbYes.Checked = True
End If
End Sub
Private Sub rbNo_CheckedChanged(sender As Object, e As EventArgs) Handles rbNo.CheckedChanged
If rbNo.Checked = True Then
Me.Laser_Maint_LogBindingSource.Current("PartOrdered") = "False"
Else
Me.Laser_Maint_LogBindingSource.Current("PartOrdered") = "True"
End If
End Sub
For the record, that's not data binding. The whole point of binding is that two properties change in sync without manual intervention. You have written code to manually keep the properties in sync, which is the exact opposite of data binding.
-
Mar 4th, 2020, 10:38 PM
#15
Re: VB.net Programming Bound Group of Radio buttons
In helping someone else with a binding problem, where they wanted a different one of two groups of controls to be visible when a property was True or False, I realised that this can be handled a different way, allowing both RadioButtons to be bound to the same property. First, some background.
When you use simple data-binding, i.e. when you call .DataBindings.Add rather than setting a DataSource, a Binding object is created to perform the processing of data between the data source and the control. You can create that Binding object yourself and pass it to the Add method or you can call a different overload and have the Binding object created internally and returned to you. Those other Add overloads basically just pass the parameters on to a Binding constructor.
Those Binding objects have two operations: format and parse. Formatting occurs when data is transferred from the data source to the control and parsing occurs when data is transferred from the control to the data source. If the two bound properties are the same data type then, by default, no processing is done during either operation, i.e. the property value is simply copied. Quite commonly though, the control property will be type String and the data source property will be some other type. In that case, formatting involves converting the data to a String and parsing involves generating the data from a String. An example is when you bind a numeric property to the Text property of a control. The default conversions for the data type are used by default but you can also set the FormattingEnabled, FormatString and FormatInfo properties to perform non-default conversions, e.g. if you want a Decimal value displayed as currency then you might set FormattingEnabled to True and FormatString to "C".
If you need more complex processing than this then you can handle the Format and/or Parse events of the Binding and write your own code to perform it. This is what interests us here. The Format event can be used to generate a custom value for the control based on the data source value. Conversely, the Parse even can be used to generate the data source value from the control's custom value. In this case, the control's custom value would be the Boolean inverse of the data source value. Here is an example using the standard Binding class:
vb.net Code:
Public Class Form1
Private WithEvents inverseBinding As Binding
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RadioButton1.DataBindings.Add("Checked", CheckBox1, "Checked")
inverseBinding = RadioButton2.DataBindings.Add("Checked", CheckBox1, "Checked")
End Sub
Private Sub invertBinding(sender As Object, e As ConvertEventArgs) Handles inverseBinding.Format,
inverseBinding.Parse
If TypeOf e.Value Is Boolean Then
e.Value = Not CBool(e.Value)
End If
End Sub
End Class
In this example, I have two RadioButtons with their Checked property bound to the Checked property of a CheckedBox. The first RadioButton is bound as normal, so it will match the CheckBox. For the second RadioButton, we catch the generated Binding and handle its Format and Parse events. In both cases, the incoming Boolean value is inverted, so that RadioButton will always be the opposite of the CheckBox.
If this is something you might want to do often, you can actually define your own custom class to handle the processing, which saves you handling any events. Like all events, Format and Parse have corresponding methods to raise them that can be overridden in a derived class. Here is such a derived class that does what we need:
vb.net Code:
Public Class InverseBooleanBinding
Inherits Binding
Public Sub New(propertyName As String, dataSource As Object, dataMember As String)
MyBase.New(propertyName, dataSource, dataMember)
End Sub
Public Sub New(propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean)
MyBase.New(propertyName, dataSource, dataMember, formattingEnabled)
End Sub
Public Sub New(propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean, dataSourceUpdateMode As DataSourceUpdateMode)
MyBase.New(propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode)
End Sub
Public Sub New(propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean, dataSourceUpdateMode As DataSourceUpdateMode, nullValue As Object)
MyBase.New(propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode, nullValue)
End Sub
Public Sub New(propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean, dataSourceUpdateMode As DataSourceUpdateMode, nullValue As Object, formatString As String)
MyBase.New(propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode, nullValue, formatString)
End Sub
Public Sub New(propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean, dataSourceUpdateMode As DataSourceUpdateMode, nullValue As Object, formatString As String, formatInfo As IFormatProvider)
MyBase.New(propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode, nullValue, formatString, formatInfo)
End Sub
Protected Overrides Sub OnFormat(cevent As ConvertEventArgs)
'Invert Boolean values.
If TypeOf cevent.Value Is Boolean Then
cevent.Value = Not CBool(cevent.Value)
End If
MyBase.OnFormat(cevent)
End Sub
Protected Overrides Sub OnParse(cevent As ConvertEventArgs)
'Invert Boolean values.
If TypeOf cevent.Value Is Boolean Then
cevent.Value = Not CBool(cevent.Value)
End If
MyBase.OnParse(cevent)
End Sub
End Class
I have mirrored every constructor of the base Binding class in this code but you would only need to do so for the ones you would expect to use. I'd say that the last two, at least, could probably be omitted. The interesting part is the OnFormat and OnParse methods. As you can see, they do basically the same thing as we did in the event handlers in the first example. You can now use the InverseBooleanBinding class in place of a standard Binding and here is an example of just that:
vb.net Code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RadioButton1.DataBindings.Add("Checked", CheckBox1, "Checked")
RadioButton2.DataBindings.Add(New InverseBooleanBinding("Checked", CheckBox1, "Checked"))
End Sub
End Class
As you can see, we need to create the InverseBooleanBinding ourselves and pass that into Add, rather than allowing it to create a standard Binding itself. Other than that though, the code is exactly the same as any standard data-binding. There's no need for any additional handling of events or processing of data.
-
Mar 4th, 2020, 10:47 PM
#16
Re: VB.net Programming Bound Group of Radio buttons
@jm Thanks for sharing. I didn’t know that was possible handling bindings that way...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 4th, 2020, 11:25 PM
#17
Re: VB.net Programming Bound Group of Radio buttons
 Originally Posted by .paul.
@jm Thanks for sharing. I didn’t know that was possible handling bindings that way...
I have known about the formatting and parsing for a long time but it never occurred to me to use them like this. I have told a number of people over the years that it is not possible to bind two RadioButtons to the same property. I can't recall whether I needed to do this sort of thing myself in the first place but I have a feeling I did. Better late than never, I guess.
-
Mar 4th, 2020, 11:32 PM
#18
Re: VB.net Programming Bound Group of Radio buttons
I’ve been kinda busy with Java. There are some interesting contrasting features there. Some things are easy in .Net and difficult in Java and vice versa. But, I’m glad I’ve learned both. Programming is about using the right tool for a job...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 4th, 2020, 11:49 PM
#19
Re: VB.net Programming Bound Group of Radio buttons
... but I still prefer vb to c# or Java
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 5th, 2020, 08:58 AM
#20
Thread Starter
Lively Member
Re: VB.net Programming Bound Group of Radio buttons
You are correct, its to bad Microsoft has diminished support for VB.net/winforms application development. The last few bugs I have summited have came back as "They did not the have staff to fix issues with VB, as they are busy with more current development languages ". I assume that's WPF, Java, c#... My guess is they have piled so many other languages into VS development software when they make changes for one language it effects all the rest but they don't have the staff to test them all. The one I summited was about the values in DateTimePicker screen buffer not updated to the program buffer during validate event. Another had to do with editing data in a Datagridview the response I got was there are several third party Datagridview addin's I should look into one of them. To bad VB is one of the more straight forward languages, unlike C# which seems much more cryptic. I do like the idea of Objects and heaps if the language wasn't so backasswards it would be great. I have actually integrated Object like code into my VB projects and it works nicely. Oh well I only have 5 years till retirement not going to worry about it to much.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|