views:

123

answers:

3

I'm writing a standalone application in VB.NET using Visual Studio 2005.

I want to display/hide a bunch of Buttons based on the selected value of a ComboBox. Each selection would have a different set of Buttons to display, and I'd like to have them arranged in a nice grid.

Driving a TabControl with the ComboBox value would be the kind of behavior I want, but I don't want it to look like a TabControl to the user because it might be confusing.

Is there a way to do this?

Basically, I'd like Selection1 of the ComboBox to show Buttons 1-4, Selection2 to show Buttons 5-11, Selection3 to show (maybe) Buttons 1, 3, 5, 6, and 8, etc., have them arranged nicely, and have the GUI show only the ComboBox and the buttons.

Thanks in advance as always!

+2  A: 

Use a Panel control (or multiple if the items aren't grouped right next to each other) and set the visibility accordingly.

(Added) You CAN stack panels on top of each other, so that the buttons all look like they're in the same location. but it becomes a maintenance nightmare and I don't recommend it.,

Hack warning - the following is a hack, but it works.

Another option is to use a tab control, but hide the tab buttons. (You can do this by positioning a panel over the buttons, but you have to be careful of letting the user resize the form.) Then you set the TabIndex based on the drop-down changing.

Edit again - added per comment

If you use the hack, you can add this into the ComboBox's selected index changed event....

(code may be wrong, as I'm not at my dev pc and can't check, but you get the idea)

TabControl1.SelectedIndex = ComboBox1.SelectedIndex
David Stratton
But how will some of the buttons "belong" to multiple choices?
M.A. Hanin
Thanks for the answer David. I'll try this.
John at CashCommons
+1  A: 

Maybe using a FlowLayoutPanel will help you display the buttons. You can use a jagged array to define which buttons belong to which combo-box item.

M.A. Hanin
Thank you. I'll give this a shot, too.
John at CashCommons
+1  A: 

You could put all of your buttons on a panel on your form. Then when the SelectedIndex event of the combobox fires, you can loop through the buttons on the panel and turn them on and off based on their Tag property.

For this example you would set the Tag property of each button equal to the combobox index or indexes that you want it to turn on for. If you want it visible for more than one combo selection just comma seperate the index values in the tag property.

You don't have to key off of the combobox index. You could use the selected text for example. If you did that, just put the texts to show the button for in the tag property and change the code from ComboBox1.SelectedIndex.ToString to ComboBox1.SelectedText.

The buttons will turn on and off where they are placed at design time, but you could add some code here to arrange them dynamically as well so that all the visible buttons are neatly arranged.

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    For Each ctrl As Control In Me.Panel1.Controls
        If TypeOf ctrl Is Button Then
            If Array.IndexOf(Split(ctrl.Tag, ","), ComboBox1.SelectedIndex.ToString) > -1 Then
                ctrl.Visible = True
            Else
                ctrl.Visible = False
            End If
        End If
    Next
End Sub
Jamie