![]() ![]() | ||
The testing department is on the phone again—does your program really need 120 buttons in the main form? After all, that's exactly what menus were designed for: to hide controls not needed, getting them out of the user's way. (In fact, that's usually a good way to determine if a control item should be in a menu or on the main form: you use menus to make options available to the user at all times, while keeping them out of the way.)
However, let's say you really don't want to put your control items into menus—you can still use buttons if you hide the ones that don't apply at a particular time, showing them when appropriate. Hiding and showing controls in a form as needed can produce dramatic effects at times.
Showing and hiding controls and forms is easy: just use the control's or form's Visible property. Setting this property to True displays the control or form; setting it to False hides it. Here's an example where we make a button disappear (probably much to the user's surprise) when the user clicks it:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Button1.Visible = False End Sub
You can also use the Show and Hide methods of controls and forms to show and hide them. For example, when the user clicks Button1, we can hide Form2 and show Form3 this way:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Form2.Hide() Form3.Show() End Sub
![]() ![]() | ||