PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2015 VB.net Programming Bound Group of Radio buttons-VBForums
Results 1 to 20 of 20

Thread: VB.net Programming Bound Group of Radio buttons

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    73

    Question 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

    Name:  Untitled.png
Views: 163
Size:  7.5 KB

    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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,119

    Re: VB.net Programming Bound Group of Radio buttons

    Where's the code you use binding the controls?

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,676

    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.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,119

    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

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,676

    Re: VB.net Programming Bound Group of Radio buttons

    Quote Originally Posted by .paul. View Post
    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.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,119

    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

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,676

    Re: VB.net Programming Bound Group of Radio buttons

    Quote Originally Posted by .paul. View Post
    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.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,676

    Re: VB.net Programming Bound Group of Radio buttons

    Quote Originally Posted by .paul. View Post
    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:
    1. Public Class extendedYesNoGroupbox
    2.     Inherits GroupBox
    3.  
    4.     Private rbNo As New RadioButton
    5.     Private rbYes As New RadioButton
    6.  
    7.     Public Sub New()
    8.         Me.Size = New Size(110, 40)
    9.         With rbNo
    10.             .Text = "No"
    11.             .AutoSize = True
    12.             .Location = New Point(12, 17)
    13.             .Checked = True
    14.         End With
    15.         Me.Controls.Add(rbNo)
    16.         With rbYes
    17.             .Text = "Yes"
    18.             .AutoSize = True
    19.             .Location = New Point(62, 17)
    20.         End With
    21.         Me.Controls.Add(rbYes)
    22.     End Sub
    23.  
    24.     Public Property IsYes As Boolean
    25.         Get
    26.             Return rbYes.Checked
    27.         End Get
    28.         Set(value As Boolean)
    29.             If value Then
    30.                 rbYes.Checked = True
    31.             Else
    32.                 rbNo.Checked = True
    33.             End If
    34.         End Set
    35.     End Property
    36.  
    37.     Public Overrides Property Text() As String
    38.         Get
    39.             If MyBase.Text.StartsWith("ExtendedYesNoGroupbox") Then
    40.                 Return "YesNo"
    41.             Else
    42.                 Return MyBase.Text
    43.             End If
    44.  
    45.         End Get
    46.         Set(ByVal value As String)
    47.             MyBase.Text = value
    48.         End Set
    49.     End Property
    50.  
    51. End Class
    and then did this in a form:
    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.     ExtendedYesNoGroupbox1.DataBindings.Add("IsYes", CheckBox1, "Checked")
    3. End Sub
    Checking and unchecking the CheckBox then checked and unchecked the RadioButtons as expected.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,119

    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.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,119

    Re: VB.net Programming Bound Group of Radio buttons

    I forget now, but i think i just got bound up in my databindings

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    73

    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.

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,793

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    73

    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.

  14. #14
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,676

    Re: VB.net Programming Bound Group of Radio buttons

    Quote Originally Posted by wjburke2 View Post
    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.

  15. #15
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,676

    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:
    1. Public Class Form1
    2.  
    3.     Private WithEvents inverseBinding As Binding
    4.  
    5.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    6.         RadioButton1.DataBindings.Add("Checked", CheckBox1, "Checked")
    7.         inverseBinding = RadioButton2.DataBindings.Add("Checked", CheckBox1, "Checked")
    8.     End Sub
    9.  
    10.     Private Sub invertBinding(sender As Object, e As ConvertEventArgs) Handles inverseBinding.Format,
    11.                                                                                inverseBinding.Parse
    12.         If TypeOf e.Value Is Boolean Then
    13.             e.Value = Not CBool(e.Value)
    14.         End If
    15.     End Sub
    16.  
    17. 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:
    1. Public Class InverseBooleanBinding
    2.     Inherits Binding
    3.  
    4.     Public Sub New(propertyName As String, dataSource As Object, dataMember As String)
    5.         MyBase.New(propertyName, dataSource, dataMember)
    6.     End Sub
    7.  
    8.     Public Sub New(propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean)
    9.         MyBase.New(propertyName, dataSource, dataMember, formattingEnabled)
    10.     End Sub
    11.  
    12.     Public Sub New(propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean, dataSourceUpdateMode As DataSourceUpdateMode)
    13.         MyBase.New(propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode)
    14.     End Sub
    15.  
    16.     Public Sub New(propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean, dataSourceUpdateMode As DataSourceUpdateMode, nullValue As Object)
    17.         MyBase.New(propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode, nullValue)
    18.     End Sub
    19.  
    20.     Public Sub New(propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean, dataSourceUpdateMode As DataSourceUpdateMode, nullValue As Object, formatString As String)
    21.         MyBase.New(propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode, nullValue, formatString)
    22.     End Sub
    23.  
    24.     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)
    25.         MyBase.New(propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode, nullValue, formatString, formatInfo)
    26.     End Sub
    27.  
    28.     Protected Overrides Sub OnFormat(cevent As ConvertEventArgs)
    29.         'Invert Boolean values.
    30.         If TypeOf cevent.Value Is Boolean Then
    31.             cevent.Value = Not CBool(cevent.Value)
    32.         End If
    33.  
    34.         MyBase.OnFormat(cevent)
    35.     End Sub
    36.  
    37.     Protected Overrides Sub OnParse(cevent As ConvertEventArgs)
    38.         'Invert Boolean values.
    39.         If TypeOf cevent.Value Is Boolean Then
    40.             cevent.Value = Not CBool(cevent.Value)
    41.         End If
    42.  
    43.         MyBase.OnParse(cevent)
    44.     End Sub
    45.  
    46. 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:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         RadioButton1.DataBindings.Add("Checked", CheckBox1, "Checked")
    5.         RadioButton2.DataBindings.Add(New InverseBooleanBinding("Checked", CheckBox1, "Checked"))
    6.     End Sub
    7.  
    8. 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.

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,119

    Re: VB.net Programming Bound Group of Radio buttons

    @jm Thanks for sharing. I didn’t know that was possible handling bindings that way...

  17. #17
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,676

    Re: VB.net Programming Bound Group of Radio buttons

    Quote Originally Posted by .paul. View Post
    @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.

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,119

    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...

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,119

    Re: VB.net Programming Bound Group of Radio buttons

    ... but I still prefer vb to c# or Java

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    73

    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
  •  



Featured


Click Here to Expand Forum to Full Width