CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2002
    Posts
    56

    Radio Buttons...????

    Hi all,
    I am developing an app. in Vb .net that has a form with a no. of radio buttons. Much like a multiple choice Questionaire(4 choices for each question).
    Once the user selects the options, and clicks on save, I need to write them in An excel sheet.
    Vb6 had control arrays which made this simple, but Vb .net ..... I don't know how to do this.
    Pl. help.
    raj2far

  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210
    VB.Net has an equivelent of control arrays but you must set up the events that you wish to handle multiple controls yourself.
    In the Sample below, I created a new project and added 4 option buttons. To handle all the option buttons in the same events, I altered the Button1_CheckChanged Sub to include buttons 2,3 and 4.
    I doubled clicked RadioButton1 and was presented the following
    Code:
    Private Sub RadioButton1_CheckedChanged(Byval sender as System.Object,_
                  Byval e as System.EventArgs) _
                  Handles RadioButton1.CheckedChanged
    '
    I changed that statement to read 
    '
    Private Sub RadioButton1_CheckedChanged(Byval sender as System.Object,_
                  Byval e as System.EventArgs) _
                  Handles RadioButton1.CheckedChanged,  _
                               RadioButton2_CheckedChanged, _
                               RadioButton3_CheckedChanged, _
                               RadioButton4_CheckedChanged
    There may be a better way, but the big book I am reading on VB.net says this is the only way to manage "control arrays" in VB.Net

  3. #3
    Join Date
    Jul 2002
    Location
    Baltimore, MD
    Posts
    11
    That's what my book says also.....

    However, I am running into an issue where I have 8 list boxes with the same information (The user can choose 8 out of 30 items to graph). There are 8 labels to the left of these list boxes that I set colors to.

    In .NET it seems like I have to do
    lblItem1.BackColor =
    lblItem2.BackColor =
    ...
    lblItem8.BackColor =

    as opposed to either using a control array in vb6 or even

    For intI = 1 to 8
    me.controls("lblItem" & intI).BackColor
    next

    So my question is: Is there a way to do the control looping? It seems like the controls are based upon integers as opposed to putting the name in.

  4. #4
    Join Date
    Apr 2002
    Posts
    56

    hi....

    WEll, I've found out a way to do this...
    along with the handles on clicks, we can loop the controls by their tab index. This is much like doing...
    arr(0) = "something"
    arr(1) = something else"
    ....
    ....
    ...
    arr(20) = "more"

    here's how to do it.
    radiobutton1.tabindex =1
    radiobutton2.tabindex = 2
    ....

    Now we can incorporate them by using a "for each" loop.
    somewhat like this.....

    ctrl = New Object()
    control = New System.Windows.Forms.RadioButton()
    Dim ctl As System.Windows.Forms.RadioButton
    ctl = New System.Windows.Forms.RadioButton()
    For Each ctrl In Me.Panel1.Controls
    For Each control In ctrl.controls
    If control.GetType Is ctl.GetType Then
    If control.checked = True Then
    Call showdetails(control)
    'MsgBox(control.name)
    End If
    End If
    ' MsgBox(control.name)
    Next control
    Next ctrl

    Now here I have a panel which have some 10 groupboxes.
    Each groupbox has one label and four radiobuttons.
    then, in the loop I call a function 'showdetails' which is somewhat like this....

    Public Sub showdetails(ByVal sender As System.Object)
    'Dim buttonControl As RadioButton = buttonControl
    Select Case (sender.TabIndex)
    Case 1
    ' MessageBox.Show(buttonControl.TabIndex)
    array(0) = 1
    Case 2
    ' MessageBox.Show(buttonControl.TabIndex)
    array(0) = 2
    Case 3
    ' MessageBox.Show(buttonControl.TabIndex)
    array(0) = 3
    Case 4
    array(0) = 4
    Case 5
    array(1) = 1
    Case 6
    array(1) = 2
    Case 7
    array(1) = 3
    Case 8
    ....
    ..
    end select

    I hope this clears every one's doubts. It did clear mine..
    Thnks for the help.
    rajtofar




    Originally posted by sxcostanzo
    That's what my book says also.....

    However, I am running into an issue where I have 8 list boxes with the same information (The user can choose 8 out of 30 items to graph). There are 8 labels to the left of these list boxes that I set colors to.

    In .NET it seems like I have to do
    lblItem1.BackColor =
    lblItem2.BackColor =
    ...
    lblItem8.BackColor =

    as opposed to either using a control array in vb6 or even

    For intI = 1 to 8
    me.controls("lblItem" & intI).BackColor
    next

    So my question is: Is there a way to do the control looping? It seems like the controls are based upon integers as opposed to putting the name in.

  5. #5
    Join Date
    Jul 2002
    Location
    Baltimore, MD
    Posts
    11
    The only problem with using the tab index is that it also is the ORDER in which the user goes through the screen. I guess it would work if those were the only controls the user went through, however if they are halfway down your form, wouldn't that be a problem?

  6. #6
    Join Date
    Apr 2002
    Posts
    56
    Hi...
    I guess its the same as "you win some you loose some".
    Is there any other way out??
    I couldn't find anything else.

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

Featured