Results 1 to 20 of 20

Thread: How to code Radio Buttons to change value in DropDown Combobox?

  1. #1

    Thread Starter
    New Member MattJoey's Avatar
    Join Date
    Oct 2012
    Location
    Canada
    Posts
    6

    How to code Radio Buttons to change value in DropDown Combobox?

    This has been mind boggling me for awhile, and I cannot seem to get around it. Hopefully someone here will be helpful enough to answer my question.
    I would like to know what/how to code a radio button that when selected it changes the Collection values in the combobox.
    Pretty much I have two radio buttons inside a groupbox with a combobox below it. One radio button is "Province" and the other "Territory". What I would like to know is how would I go about when the user selects the Province radio button that in the combobox it shows the provinces of Canada, and when they select the Territory radio button that the combobox will update and show the three territories of Canada.

    If no one understands I will post screenshots of what I mean.

    Thank You.

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

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    can you post a list of your provinces + territories?

  3. #3

    Thread Starter
    New Member MattJoey's Avatar
    Join Date
    Oct 2012
    Location
    Canada
    Posts
    6

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    Provinces(A-Z)
    Alberta
    British Columbia
    Manitoba
    New Brunswick
    Newfoundland & Labrador
    Nova Scotia
    Ontario
    Prince Edward Island
    Quebec
    Saskatchewan

    Territories
    Northwest Territories
    Nunavut
    Yukon

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

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    try this:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Dim choices()() As String = {New String() {"Alberta", "British Columbia", "Manitoba", "New Brunswick", "Newfoundland & Labrador", "Nova Scotia", "Ontario", "Prince Edward Island", "Quebec", "Saskatchewan"}, New String() {"Northwest Territories", "Nunavut", "Yukon"}}
    4.  
    5.     Private Sub RadioButtons_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
    6.         Dim rbs() As RadioButton = {RadioButton1, RadioButton2}
    7.         ComboBox1.DataSource = choices(Array.IndexOf(rbs, sender))
    8.     End Sub
    9.  
    10.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    11.         RadioButton1.Checked = True
    12.     End Sub
    13.  
    14. End Class

  5. #5

    Thread Starter
    New Member MattJoey's Avatar
    Join Date
    Oct 2012
    Location
    Canada
    Posts
    6

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    Alright, and where would this code go in, the combobox, form, or both the radio buttons?

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

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    show me your form code + i'll show you how to integrate my code

  7. #7

    Thread Starter
    New Member MattJoey's Avatar
    Join Date
    Oct 2012
    Location
    Canada
    Posts
    6

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    Name:  Capture.PNG
Views: 3095
Size:  3.0 KBName:  Capture2.PNG
Views: 3635
Size:  35.8 KB

    There, I believe that's what you asked.
    I only have two buttons coded how they should be.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    ok. you can paste the code before your first button_click handler.
    don't forget to change the names RadioButton1, RadioButton2 to your rb names

    edit: + the combobox...
    Last edited by .paul.; Oct 23rd, 2012 at 11:02 PM.

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

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    in your code, intResponse is wrong. a msgbox returns a value of type msgboxresult

  10. #10

    Thread Starter
    New Member MattJoey's Avatar
    Join Date
    Oct 2012
    Location
    Canada
    Posts
    6

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    Thank you very much, Paul. +1 rep for you.

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    Another very simple way to do this would be to use two combo boxes and toggle which is visible when the radio button is ticked.

  12. #12

    Thread Starter
    New Member MattJoey's Avatar
    Join Date
    Oct 2012
    Location
    Canada
    Posts
    6

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    Quote Originally Posted by DataMiser View Post
    Another very simple way to do this would be to use two combo boxes and toggle which is visible when the radio button is ticked.
    And how would that work? I am thinking of doing another assignment in the future and will use the Combobox like this and I think I will use both.

  13. #13
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    You should be able to figure that out if you give if a tiny bit of thought. You have 2 lists and 2 combos you want one list visible based on which radio button is clicked. Think about it.

  14. #14
    New Member
    Join Date
    Oct 2012
    Posts
    13

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    Quote Originally Posted by MattJoey View Post
    And how would that work? I am thinking of doing another assignment in the future and will use the Combobox like this and I think I will use both.
    Heres how you do it, just have the combo boxes directly over each other

    Code:
    Private Sub Provinces_Radio_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Provinces_Radio.CheckedChanged        Provinces_Combo.Visible = True
            Territories_Combo.Visible = False
        End Sub
    
    
        Private Sub Territories_Radio_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Territories_Radio.CheckedChanged
            Territories_Combo.Visible = True
            Provinces_Combo.Visible = False
        End Sub

  15. #15
    New Member
    Join Date
    Oct 2012
    Posts
    13

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    Why....did it do that? :/

    and why cant I edit?

    Code:
        Private Sub Provinces_Radio_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Provinces_Radio.CheckedChanged
            Provinces_Combo.Visible = True
            Territories_Combo.Visible = False
        End Sub
    
        Private Sub Territories_Radio_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Territories_Radio.CheckedChanged
            Territories_Combo.Visible = True
            Provinces_Combo.Visible = False
        End Sub

  16. #16
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    Why did it do what?

    You can't edit until your post count is a bit higher.

  17. #17
    New Member
    Join Date
    Oct 2012
    Posts
    13

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    Quote Originally Posted by DataMiser View Post
    Why did it do what?

    You can't edit until your post count is a bit higher.
    On my screen on the first reply I made the first and seconds CODE lines are on the same line.

    and thanks for the info about the edit.

  18. #18
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    The code window will word wrap when the lines are to long to fit.

  19. #19
    New Member
    Join Date
    Sep 2013
    Posts
    7

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    I am having also the same problem. I need to save what is in the radio button like save male or female? I am using streamwriter to save the details of the form.

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: How to code Radio Buttons to change value in DropDown Combobox?

    Quote Originally Posted by krazy4search View Post
    I am having also the same problem. I need to save what is in the radio button like save male or female? I am using streamwriter to save the details of the form.
    Please start your own thread for your question.
    This thread is 11 months old

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