Tom
Tom

Reputation: 79

RadioButton will not change panel display when selected

Hello I'm trying to get this radiobutton to change during panel display but it will not. I want it to change back in forth regardless of the order that it is selected but I'm having some difficulties. I'm aware that this is an easy fix but if anyone can help I will really appreciate it.

Public Sub RadioButton()

    If RadioButton1.Checked Then
        Panel2.Visible = True
    End If
    If RadioButton2.Checked Then
        Panel3.Visible = True
    End If
    If RadioButton3.Checked Then
        Panel4.Visible = True
    End If
    If RadioButton4.Checked Then
        Panel5.Visible = True
    End If

End Sub

Upvotes: 1

Views: 863

Answers (3)

David Wilson
David Wilson

Reputation: 4439

It sounds almost as if your panels are layered on top of each other - First thing to check is that none of the panels are children of another None of the panels effectively sub panels of any others? This can happen very easily if you're moving panels around using drag and drop in the form designer.

From the Visual Studio View menu, click on View>Other Windows>Document Outline

This will bring up a Visual Studio tab showing all your controls on the form. Have a look at the Panel controls. If any are below another and are indented - like this :-

enter image description here

Panel5 is a child of Panel2. To correct this, drag Panel5 to just underneath Form1. Or - at the top of the tab. you can see a left,right,up and down arrow. Click the indented panel and then click the left arrow.

Do this for any other indented panels.

Next - you code. It should work fine if your panels aren't on top of each other, but if they're on top of each other, then you may also need to bring the Panel to the front of the pile -

Panel2.BringToFront

Hopefully this should solve the problems.

Upvotes: 1

Visual Vincent
Visual Vincent

Reputation: 18320

Do you even handle the RadioButtons' events?

You get use of the CheckedChanged event for this:

Private Sub RadioButtons_CheckedChanged(sender As Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged, RadioButton4.CheckedChanged
    Panel2.Visible = RadioButton1.Checked
    Panel3.Visible = RadioButton2.Checked
    Panel4.Visible = RadioButton3.Checked
    Panel5.Visible = RadioButton4.Checked
End Sub

Read more: CheckedChanged Event - MSDN

Upvotes: 1

Szymon
Szymon

Reputation: 43023

I'm guessing you want to switch panels' visibility based on which radio button is selected. The problem is that you only make those panels visible but never not visible (i.e. when one becomes visible, others should not be visible). Change your code to:

Panel2.Visible = RadioButton1.Checked
Panel3.Visible = RadioButton2.Checked
Panel4.Visible = RadioButton3.Checked
Panel5.Visible = RadioButton4.Checked

Upvotes: 3

Related Questions