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
RadioButtonGroup control-VBForums
Results 1 to 1 of 1

Thread: RadioButtonGroup control

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    RadioButtonGroup control

    Since someone posted a question about this on the forum earlier, I decided to give it a shot and create a RadioButton Group control.

    It acts like a simple container (FlowLayoutPanel) containing multiple RadioButtons. The group control however also has CheckedRadioButton and CheckedIndex properties, which return the RadioButton that is checked, and it's index in the controls collection, respectively.

    This allows you to write code using multiple RadioButtons much easier in some cases. For example, suppose you have 4 RadioButtons where the user has to select a skill level with associated number, Beginner (0), Medium (1), Hard (2), or Expert (3).

    If you then want to pass the number of the selected skill level to some function, you would usually have to do something like:
    vb.net Code:
    1. If rbBeginner.Checked Then
    2.    SetLevel(0)
    3. ElseIf rbMedium.Checked Then
    4.    SetLevel(1)
    5. ElseIf rbHard.Checked Then
    6.    SetLevel(2)
    7. ElseIf rbExpert.Checked Then
    8.    SetLevel(3)
    9. End If
    Whereas, with my control, you can simply use:
    vb.net Code:
    1. SetLevel(RadioButtonGroup1.CheckedIndex)
    This works because the index ranges from 0 to 3, just as the skill levels (assuming you placed them in the right order).

    But what if, instead of numbers 0 to 3, you need to pass a string "beginner", "medium", "hard" or "expert" to your function? Or, for simplicity, different numbers, like 1, 4, 9, 15? Well, for that reason, I added a DataValue property to the RadioButtons you add to the group. This value can only be set during run-time though, so you would have to add the radiobuttons dynamically.

    The RadioButtons the group can contain are not regular RadioButtons, but rather custom RadioButtons I called cRadioButton. They behave just like the regular thing though, except for the DataValue property, and the fact that you cannot move them in the designer.
    The constructor of the cRadioButton class takes both a DisplayText and DataValue argument, so this is where you can set the DataValue:
    vb.net Code:
    1. 'When loading the form:
    2.         Dim rb As cRadioButton
    3.  
    4.         rb = New cRadioButton("Beginner", 1)
    5.         RadioButtonGroup1.RadioButtons.Add(rb)
    6.  
    7.         rb = New cRadioButton("Medium", 4)
    8.         RadioButtonGroup1.RadioButtons.Add(rb)
    9.  
    10.         rb = New cRadioButton("Hard", 9)
    11.         RadioButtonGroup1.RadioButtons.Add(rb)
    12.  
    13.         rb = New cRadioButton("Expert", 15)
    14.         RadioButtonGroup1.RadioButtons.Add(rb)
    15.  
    16.  
    17.         '--------------
    18.         'Later:
    19.         SetLevel(CInt(RadioButtonGroup1.CheckedRadioButton.DataValue))

    So, I add four cRadioButtons to a RadioButtonGroup control, and later I use the DataValue of the CheckedRadioButton and pass that as the argument of some kind of SetLevel function.



    Additionally, I have added a Designer class for both the RadioButtonGroup control and the cRadioButton control.
    The Designer for the RadioButtonGroup control allows you to use an Action List menu which gives you access to the RadioButtons collection, and the CheckedIndex property during Design-Time:


    So, you don't (and can't) add RadioButtons to the RadioButtonGroup by dragging them in; rather you just use the Collection Editor window to add them. You can also use that window to remove them, although you can remove them by simply selecting them and deleting.

    Every cRadioButton in the group can be selected individually during Design-Time, and you can change it's properties as usual. You can also use their individual Events.

    However, there is also a CheckedIndexChanged event, which fires whenever the CheckedIndex changes (either by you changing the CheckedIndex / CheckedRadioButton property via code, or by simply checking one during run-time).


    The Designer class for the cRadioButton control merely makes sure that you cannot move the controls. They are positioned in a FlowLayoutPanel, so you should not be able to move them at all usually. But for some reason, the designer did allow moving, but then crashed. So I disabled moving them alltogether.


    I've attached the files you need to this post.


    To use this control, you must first add a reference to System.Design
    . You can do that by right-clicking the project name in the Solution Explorer and choosing Add Reference. Wait for the window to pop up (can take a long time) and scroll down and select System.Design.

    Then, you can right-click the project again and choose Add - Existing Items... Browse to the 3 files and add them.

    Finally, Build your solution, and the control should appear at the top of your Toolbox.



    Enjoy!
    Attached Files Attached Files

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