Results 1 to 13 of 13

Thread: radiobutton

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2015
    Posts
    48

    radiobutton

    hello all
    Can anyone please tell me how to uncheck all the radiobuttons by default in vb window form?
    I know the codes through javascript but not through vb. Please help
    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: radiobutton

    You don't need any code to uncheck them by default. You simply set the Checked property to False in the designer, which it already is by default anyway. Basically, what you're asking for is the default behavior.

    Do you perhaps not actually mean by default but rather unchecking them explicitly after one may have been checked? If so then that could be done like this, assuming that all the RadioButtons have been added to the form directly:
    Code:
    For Each rb In Me.Controls.OfType(Of RadioButton)()
        rb.Checked = False
    Next

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2015
    Posts
    48

    Re: radiobutton

    Quote Originally Posted by jmcilhinney View Post
    You don't need any code to uncheck them by default. You simply set the Checked property to False in the designer, which it already is by default anyway. Basically, what you're asking for is the default behavior.

    Do you perhaps not actually mean by default but rather unchecking them explicitly after one may have been checked? If so then that could be done like this, assuming that all the RadioButtons have been added to the form directly:
    Code:
    For Each rb In Me.Controls.OfType(Of RadioButton)()
        rb.Checked = False
    Next





    Thank you so much
    I will try this and get back to you.


    Thanks again

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2015
    Posts
    48

    Re: radiobutton

    Quote Originally Posted by Abhi25 View Post
    Thank you so much
    I will try this and get back to you.


    Thanks again
    hi
    This method doesnt work.
    i still have a default checked button.
    Is there any other way?

    please help

    Thanks

  5. #5
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: radiobutton

    Quote Originally Posted by Abhi25 View Post
    hi
    This method doesnt work.
    i still have a default checked button.
    Is there any other way?

    please help

    Thanks
    Are the RadioButtons all directly on the form? Are they in GroupBoxes or Panels? Are some on the form and some in GroupBoxes/Panels?
    We know nothing of where they are or how your form is set up.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All Threads • Colors ComboBox • Fading & Gradient Form • MoveItemListBox/MoveItemListView • MultilineListBox • MenuButton • ToolStripCheckBox • Start with Windows

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2015
    Posts
    48

    Re: radiobutton

    Quote Originally Posted by JuggaloBrotha View Post
    Are the RadioButtons all directly on the form? Are they in GroupBoxes or Panels? Are some on the form and some in GroupBoxes/Panels?
    We know nothing of where they are or how your form is set up.
    radiobuttons are inside a groupbox.
    if i use this code it doesnt enter the loop:

    For Each rb As RadioButton In Me.GroupBox1.Controls.OfType(Of RadioButton)()
    rb.CheckedByDefault = False
    Next

    Where am i going wrong?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: radiobutton

    If you mean that a RadioButton is checked when the form is first displayed then there are only two reasons that that would happen:

    1. The Checked property of that RadioButton is set to True in the designer.
    2. That RadioButton is the first control in the Tab order for the form.

    With regards to #2, a RadioButton becomes checked when it receives focus and the first control in the Tab order gets focus when a form is first displayed. If that's your issue, the solution is generally to change the Tab order of the controls to avoid a RadioButton being first.

    If that's not possible for some reason, the code I have already provided is what you would use to uncheck the RadioButton. The important thing is where you put the code. Putting it in the Load event handler is useless because that is executed before the form is displayed. You need to execute the code after the form is displayed, so you'd put it in the Shown event handler.

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2015
    Posts
    48

    Re: radiobutton

    Quote Originally Posted by jmcilhinney View Post
    If you mean that a RadioButton is checked when the form is first displayed then there are only two reasons that that would happen:

    1. The Checked property of that RadioButton is set to True in the designer.
    2. That RadioButton is the first control in the Tab order for the form.

    With regards to #2, a RadioButton becomes checked when it receives focus and the first control in the Tab order gets focus when a form is first displayed. If that's your issue, the solution is generally to change the Tab order of the controls to avoid a RadioButton being first.

    If that's not possible for some reason, the code I have already provided is what you would use to uncheck the RadioButton. The important thing is where you put the code. Putting it in the Load event handler is useless because that is executed before the form is displayed. You need to execute the code after the form is displayed, so you'd put it in the Shown event handler.

    Hi
    Can you please tell me exactly how to use this code?
    Shown event handler means like where?
    I have still not succeeded inunchecking it.
    Please help.

    Thanks

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: radiobutton

    Handle the Shown event of the form and put the code there. If you don't know how to handle an event then that's probably something you should look into. It's one of the most fundamental things you can do in VB and that's why pretty much every beginners tutorial covers it. If you don't understand the fundamentals then you should take the time to work through such a tutorial. There's a link to one in my signature below.

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2015
    Posts
    48

    Re: radiobutton

    Quote Originally Posted by jmcilhinney View Post
    Handle the Shown event of the form and put the code there. If you don't know how to handle an event then that's probably something you should look into. It's one of the most fundamental things you can do in VB and that's why pretty much every beginners tutorial covers it. If you don't understand the fundamentals then you should take the time to work through such a tutorial. There's a link to one in my signature below.


    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As EventArgs) _
    Handles MyBase.Shown
    Try
    For Each rb As RadioButton In Me.GroupBox1.Controls.OfType(Of RadioButton)()
    rb.CheckedByDefault = False
    Next

    Catch ex As Exception

    End Try
    End Sub

    This is where I am with no output.
    Where is it wrong now?

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: radiobutton

    Is there a red squiggly line under rb.CheckedByDefault = False? If not, is this a VB forms application? The line should read rb.Checked = False
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12

    Thread Starter
    Member
    Join Date
    Jun 2015
    Posts
    48

    Re: radiobutton

    Quote Originally Posted by dbasnett View Post
    Is there a red squiggly line under rb.CheckedByDefault = False? If not, is this a VB forms application? The line should read rb.Checked = False

    The case is neither. It is vb.net forms application.
    The compiler is not entering the FOR loop. And rb.Checked is not coming. Even if i write that its automatically taking CheckedByDefault.
    Last edited by Abhi25; Aug 13th, 2015 at 07:01 AM.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: radiobutton

    Presumably you mean that it's a Windows Forms application written in VB.NET and, if that's the case, the standard RadioButton control has no property named CheckedByDefault. If you have such a property then you are not using a standard WinForms RadioButton.

    Also, I specifically stated in my first reply that that code would if the RadioButtons had been added directly to the form. The code uses the form's Controls collection so if the RadioButtons have been added to some other control, e.g. a Panel or GroupBox, then the code will be useless because you would have to use the Controls collection of that container instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width