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?
Printable View
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 procedureQuote:
Originally Posted by vlntine82
vb Code:
Public Class Form1 Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click ShowHidePanels End Sub Private Sub ShowHidePanels Select Case True Case rdoPnl1.Checked : Panel1.Visible = True Panel2.Visible = False Panel3.Visible = False Case rdoPnl2.Checked : Panel2.Visible = True Panel3.Visible = False Panel1.Visible = False Case rdoPnl3.Checked : Panel3.Visible = True Panel1.Visible = False Panel2.Visible = False Case Else Panel1.Visible = True Panel2.Visible = True Panel3.Visible = True End Select End Sub End Class
Here is the form generation code if you would like to recreate the form and give it a go:Quote:
Originally Posted by vlntine82
vb Code:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Partial Class Form1 Inherits System.Windows.Forms.Form 'Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) Try If disposing AndAlso components IsNot Nothing Then components.Dispose() End If Finally MyBase.Dispose(disposing) End Try End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.rdoPnl1 = New System.Windows.Forms.RadioButton Me.rdoPnl2 = New System.Windows.Forms.RadioButton Me.rdoPnl3 = New System.Windows.Forms.RadioButton Me.btnGo = New System.Windows.Forms.Button Me.GroupBox1 = New System.Windows.Forms.GroupBox Me.Panel1 = New System.Windows.Forms.Panel Me.Panel2 = New System.Windows.Forms.Panel Me.Panel3 = New System.Windows.Forms.Panel Me.Panel4 = New System.Windows.Forms.Panel Me.GroupBox1.SuspendLayout() Me.Panel4.SuspendLayout() Me.SuspendLayout() ' 'rdoPnl1 ' Me.rdoPnl1.AutoSize = True Me.rdoPnl1.Location = New System.Drawing.Point(6, 19) Me.rdoPnl1.Name = "rdoPnl1" Me.rdoPnl1.Size = New System.Drawing.Size(61, 17) Me.rdoPnl1.TabIndex = 0 Me.rdoPnl1.TabStop = True Me.rdoPnl1.Text = "Panel 1" Me.rdoPnl1.UseVisualStyleBackColor = True ' 'rdoPnl2 ' Me.rdoPnl2.AutoSize = True Me.rdoPnl2.Location = New System.Drawing.Point(6, 42) Me.rdoPnl2.Name = "rdoPnl2" Me.rdoPnl2.Size = New System.Drawing.Size(61, 17) Me.rdoPnl2.TabIndex = 1 Me.rdoPnl2.TabStop = True Me.rdoPnl2.Text = "Panel 2" Me.rdoPnl2.UseVisualStyleBackColor = True ' 'rdoPnl3 ' Me.rdoPnl3.AutoSize = True Me.rdoPnl3.Location = New System.Drawing.Point(6, 65) Me.rdoPnl3.Name = "rdoPnl3" Me.rdoPnl3.Size = New System.Drawing.Size(61, 17) Me.rdoPnl3.TabIndex = 2 Me.rdoPnl3.TabStop = True Me.rdoPnl3.Text = "Panel 3" Me.rdoPnl3.UseVisualStyleBackColor = True ' 'btnGo ' Me.btnGo.Location = New System.Drawing.Point(82, 42) Me.btnGo.Name = "btnGo" Me.btnGo.Size = New System.Drawing.Size(75, 23) Me.btnGo.TabIndex = 3 Me.btnGo.Text = "Go" Me.btnGo.UseVisualStyleBackColor = True ' 'GroupBox1 ' Me.GroupBox1.Controls.Add(Me.rdoPnl1) Me.GroupBox1.Controls.Add(Me.btnGo) Me.GroupBox1.Controls.Add(Me.rdoPnl2) Me.GroupBox1.Controls.Add(Me.rdoPnl3) Me.GroupBox1.Location = New System.Drawing.Point(12, 12) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(200, 91) Me.GroupBox1.TabIndex = 4 Me.GroupBox1.TabStop = False Me.GroupBox1.Text = "Select Panel to Show" ' 'Panel1 ' Me.Panel1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles) Me.Panel1.BackColor = System.Drawing.Color.DarkRed Me.Panel1.Location = New System.Drawing.Point(63, 3) Me.Panel1.Name = "Panel1" Me.Panel1.Size = New System.Drawing.Size(54, 78) Me.Panel1.TabIndex = 5 ' 'Panel2 ' Me.Panel2.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles) Me.Panel2.BackColor = System.Drawing.Color.Olive Me.Panel2.Location = New System.Drawing.Point(123, 3) Me.Panel2.Name = "Panel2" Me.Panel2.Size = New System.Drawing.Size(54, 78) Me.Panel2.TabIndex = 6 ' 'Panel3 ' Me.Panel3.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles) Me.Panel3.BackColor = System.Drawing.Color.SlateGray Me.Panel3.Location = New System.Drawing.Point(3, 3) Me.Panel3.Name = "Panel3" Me.Panel3.Size = New System.Drawing.Size(54, 78) Me.Panel3.TabIndex = 7 ' 'Panel4 ' Me.Panel4.Controls.Add(Me.Panel3) Me.Panel4.Controls.Add(Me.Panel1) Me.Panel4.Controls.Add(Me.Panel2) Me.Panel4.Dock = System.Windows.Forms.DockStyle.Bottom Me.Panel4.Location = New System.Drawing.Point(0, 121) Me.Panel4.Name = "Panel4" Me.Panel4.Size = New System.Drawing.Size(221, 84) Me.Panel4.TabIndex = 8 ' 'Form1 ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(221, 205) Me.Controls.Add(Me.Panel4) Me.Controls.Add(Me.GroupBox1) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog Me.MaximizeBox = False Me.MinimizeBox = False Me.Name = "Form1" Me.Text = "Form1" Me.GroupBox1.ResumeLayout(False) Me.GroupBox1.PerformLayout() Me.Panel4.ResumeLayout(False) Me.ResumeLayout(False) End Sub Friend WithEvents rdoPnl1 As System.Windows.Forms.RadioButton Friend WithEvents rdoPnl2 As System.Windows.Forms.RadioButton Friend WithEvents rdoPnl3 As System.Windows.Forms.RadioButton Friend WithEvents btnGo As System.Windows.Forms.Button Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents Panel1 As System.Windows.Forms.Panel Friend WithEvents Panel2 As System.Windows.Forms.Panel Friend WithEvents Panel3 As System.Windows.Forms.Panel Friend WithEvents Panel4 As System.Windows.Forms.Panel End Class
I would suggest a different tack. I'd suggest a separate event handler for each RadioButton, e.g.To me that's cleaner and clearer. You'd set the Visible property of all Panels to false initially.vb.net Code:
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged Me.Panel1.Visible = Me.RadioButton1.Checked End Sub Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged Me.Panel2.Visible = Me.RadioButton2.Checked End Sub Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged Me.Panel3.Visible = Me.RadioButton3.Checked End Sub