Results 1 to 4 of 4

Thread: radio button question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    90

    radio button question

    I have several radio buttons in my application. I need different panels to show based of off what the user selects. How do I create an event to handle if a user changes the radio button selected?

  2. #2
    Lively Member RickyH's Avatar
    Join Date
    Oct 2007
    Posts
    92

    Re: radio button question

    Quote Originally Posted by vlntine82
    I have several radio buttons in my application. I need different panels to show based of off what the user selects. How do I create an event to handle if a user changes the radio button selected?
    try a select case in your form when clicking a button. You can also attach this to the checkchanged event of the radio button and have it call the procedure

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
    4.         ShowHidePanels
    5. End Sub
    6.  
    7. Private Sub ShowHidePanels
    8. Select Case True
    9.             Case rdoPnl1.Checked : Panel1.Visible = True
    10.                 Panel2.Visible = False
    11.                 Panel3.Visible = False
    12.             Case rdoPnl2.Checked : Panel2.Visible = True
    13.                 Panel3.Visible = False
    14.                 Panel1.Visible = False
    15.             Case rdoPnl3.Checked : Panel3.Visible = True
    16.                 Panel1.Visible = False
    17.                 Panel2.Visible = False
    18.             Case Else
    19.                 Panel1.Visible = True
    20.                 Panel2.Visible = True
    21.                 Panel3.Visible = True
    22.         End Select
    23. End Sub
    24. End Class

  3. #3
    Lively Member RickyH's Avatar
    Join Date
    Oct 2007
    Posts
    92

    Re: radio button question

    Quote Originally Posted by vlntine82
    I have several radio buttons in my application. I need different panels to show based of off what the user selects. How do I create an event to handle if a user changes the radio button selected?
    Here is the form generation code if you would like to recreate the form and give it a go:

    vb Code:
    1. <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    2. Partial Class Form1
    3.     Inherits System.Windows.Forms.Form
    4.  
    5.     'Form overrides dispose to clean up the component list.
    6.     <System.Diagnostics.DebuggerNonUserCode()> _
    7.     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    8.         Try
    9.             If disposing AndAlso components IsNot Nothing Then
    10.                 components.Dispose()
    11.             End If
    12.         Finally
    13.             MyBase.Dispose(disposing)
    14.         End Try
    15.     End Sub
    16.  
    17.     'Required by the Windows Form Designer
    18.     Private components As System.ComponentModel.IContainer
    19.  
    20.     'NOTE: The following procedure is required by the Windows Form Designer
    21.     'It can be modified using the Windows Form Designer.  
    22.     'Do not modify it using the code editor.
    23.     <System.Diagnostics.DebuggerStepThrough()> _
    24.     Private Sub InitializeComponent()
    25.         Me.rdoPnl1 = New System.Windows.Forms.RadioButton
    26.         Me.rdoPnl2 = New System.Windows.Forms.RadioButton
    27.         Me.rdoPnl3 = New System.Windows.Forms.RadioButton
    28.         Me.btnGo = New System.Windows.Forms.Button
    29.         Me.GroupBox1 = New System.Windows.Forms.GroupBox
    30.         Me.Panel1 = New System.Windows.Forms.Panel
    31.         Me.Panel2 = New System.Windows.Forms.Panel
    32.         Me.Panel3 = New System.Windows.Forms.Panel
    33.         Me.Panel4 = New System.Windows.Forms.Panel
    34.         Me.GroupBox1.SuspendLayout()
    35.         Me.Panel4.SuspendLayout()
    36.         Me.SuspendLayout()
    37.         '
    38.         'rdoPnl1
    39.         '
    40.         Me.rdoPnl1.AutoSize = True
    41.         Me.rdoPnl1.Location = New System.Drawing.Point(6, 19)
    42.         Me.rdoPnl1.Name = "rdoPnl1"
    43.         Me.rdoPnl1.Size = New System.Drawing.Size(61, 17)
    44.         Me.rdoPnl1.TabIndex = 0
    45.         Me.rdoPnl1.TabStop = True
    46.         Me.rdoPnl1.Text = "Panel 1"
    47.         Me.rdoPnl1.UseVisualStyleBackColor = True
    48.         '
    49.         'rdoPnl2
    50.         '
    51.         Me.rdoPnl2.AutoSize = True
    52.         Me.rdoPnl2.Location = New System.Drawing.Point(6, 42)
    53.         Me.rdoPnl2.Name = "rdoPnl2"
    54.         Me.rdoPnl2.Size = New System.Drawing.Size(61, 17)
    55.         Me.rdoPnl2.TabIndex = 1
    56.         Me.rdoPnl2.TabStop = True
    57.         Me.rdoPnl2.Text = "Panel 2"
    58.         Me.rdoPnl2.UseVisualStyleBackColor = True
    59.         '
    60.         'rdoPnl3
    61.         '
    62.         Me.rdoPnl3.AutoSize = True
    63.         Me.rdoPnl3.Location = New System.Drawing.Point(6, 65)
    64.         Me.rdoPnl3.Name = "rdoPnl3"
    65.         Me.rdoPnl3.Size = New System.Drawing.Size(61, 17)
    66.         Me.rdoPnl3.TabIndex = 2
    67.         Me.rdoPnl3.TabStop = True
    68.         Me.rdoPnl3.Text = "Panel 3"
    69.         Me.rdoPnl3.UseVisualStyleBackColor = True
    70.         '
    71.         'btnGo
    72.         '
    73.         Me.btnGo.Location = New System.Drawing.Point(82, 42)
    74.         Me.btnGo.Name = "btnGo"
    75.         Me.btnGo.Size = New System.Drawing.Size(75, 23)
    76.         Me.btnGo.TabIndex = 3
    77.         Me.btnGo.Text = "Go"
    78.         Me.btnGo.UseVisualStyleBackColor = True
    79.         '
    80.         'GroupBox1
    81.         '
    82.         Me.GroupBox1.Controls.Add(Me.rdoPnl1)
    83.         Me.GroupBox1.Controls.Add(Me.btnGo)
    84.         Me.GroupBox1.Controls.Add(Me.rdoPnl2)
    85.         Me.GroupBox1.Controls.Add(Me.rdoPnl3)
    86.         Me.GroupBox1.Location = New System.Drawing.Point(12, 12)
    87.         Me.GroupBox1.Name = "GroupBox1"
    88.         Me.GroupBox1.Size = New System.Drawing.Size(200, 91)
    89.         Me.GroupBox1.TabIndex = 4
    90.         Me.GroupBox1.TabStop = False
    91.         Me.GroupBox1.Text = "Select Panel to Show"
    92.         '
    93.         'Panel1
    94.         '
    95.         Me.Panel1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
    96.                     Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
    97.         Me.Panel1.BackColor = System.Drawing.Color.DarkRed
    98.         Me.Panel1.Location = New System.Drawing.Point(63, 3)
    99.         Me.Panel1.Name = "Panel1"
    100.         Me.Panel1.Size = New System.Drawing.Size(54, 78)
    101.         Me.Panel1.TabIndex = 5
    102.         '
    103.         'Panel2
    104.         '
    105.         Me.Panel2.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
    106.                     Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
    107.         Me.Panel2.BackColor = System.Drawing.Color.Olive
    108.         Me.Panel2.Location = New System.Drawing.Point(123, 3)
    109.         Me.Panel2.Name = "Panel2"
    110.         Me.Panel2.Size = New System.Drawing.Size(54, 78)
    111.         Me.Panel2.TabIndex = 6
    112.         '
    113.         'Panel3
    114.         '
    115.         Me.Panel3.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
    116.                     Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
    117.         Me.Panel3.BackColor = System.Drawing.Color.SlateGray
    118.         Me.Panel3.Location = New System.Drawing.Point(3, 3)
    119.         Me.Panel3.Name = "Panel3"
    120.         Me.Panel3.Size = New System.Drawing.Size(54, 78)
    121.         Me.Panel3.TabIndex = 7
    122.         '
    123.         'Panel4
    124.         '
    125.         Me.Panel4.Controls.Add(Me.Panel3)
    126.         Me.Panel4.Controls.Add(Me.Panel1)
    127.         Me.Panel4.Controls.Add(Me.Panel2)
    128.         Me.Panel4.Dock = System.Windows.Forms.DockStyle.Bottom
    129.         Me.Panel4.Location = New System.Drawing.Point(0, 121)
    130.         Me.Panel4.Name = "Panel4"
    131.         Me.Panel4.Size = New System.Drawing.Size(221, 84)
    132.         Me.Panel4.TabIndex = 8
    133.         '
    134.         'Form1
    135.         '
    136.         Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    137.         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    138.         Me.ClientSize = New System.Drawing.Size(221, 205)
    139.         Me.Controls.Add(Me.Panel4)
    140.         Me.Controls.Add(Me.GroupBox1)
    141.         Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
    142.         Me.MaximizeBox = False
    143.         Me.MinimizeBox = False
    144.         Me.Name = "Form1"
    145.         Me.Text = "Form1"
    146.         Me.GroupBox1.ResumeLayout(False)
    147.         Me.GroupBox1.PerformLayout()
    148.         Me.Panel4.ResumeLayout(False)
    149.         Me.ResumeLayout(False)
    150.  
    151.     End Sub
    152.     Friend WithEvents rdoPnl1 As System.Windows.Forms.RadioButton
    153.     Friend WithEvents rdoPnl2 As System.Windows.Forms.RadioButton
    154.     Friend WithEvents rdoPnl3 As System.Windows.Forms.RadioButton
    155.     Friend WithEvents btnGo As System.Windows.Forms.Button
    156.     Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    157.     Friend WithEvents Panel1 As System.Windows.Forms.Panel
    158.     Friend WithEvents Panel2 As System.Windows.Forms.Panel
    159.     Friend WithEvents Panel3 As System.Windows.Forms.Panel
    160.     Friend WithEvents Panel4 As System.Windows.Forms.Panel
    161.  
    162. End Class

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: radio button question

    I would suggest a different tack. I'd suggest a separate event handler for each RadioButton, e.g.
    vb.net Code:
    1. Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, _
    2.                                         ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
    3.     Me.Panel1.Visible = Me.RadioButton1.Checked
    4. End Sub
    5.  
    6. Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, _
    7.                                         ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
    8.     Me.Panel2.Visible = Me.RadioButton2.Checked
    9. End Sub
    10.  
    11. Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, _
    12.                                         ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
    13.     Me.Panel3.Visible = Me.RadioButton3.Checked
    14. End Sub
    To me that's cleaner and clearer. You'd set the Visible property of all Panels to false initially.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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