Browse » Home
Wednesday, July 27, 2011
How to Set the Z Order Programmatically in Visual Basic
Add Controls to a Form
1. Open Visual Studio and click 'File.' Select 'New Project' to open the New Project window.
2. Click 'Visual Basic' and then double-click 'Windows Forms Application.' Visual Studio will create a new project and display an empty form in the design window.
3. Click 'View' and select 'Toolbox' to display the toolbox and its controls.
4. Double-click the ListBox control. A ListBox named 'ListBox1' will appear on the form.
5. Move to the toolbox again and click the ListBox control. Double-click it and Visual Studio will add a ListBox named 'ListBox2' to the form.
6. Click 'ListBox2' and hold down your left mouse button. Drag it so that it slightly overlaps 'ListBox1.'
7. Return to the toolbox and double-click the 'Button' control. A button named 'Button1' will appear on the form.
8. Return to the toolbox a final time and double-click the 'Button' control again. Visual Studio will add a button named 'Button2' to the form. Click the 'X' at the top of the toolbox to close it.
Add Z Order Logic
9. Double-click 'Button1.' This creates a click event for that button. The following code will appear in the code editor:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickEnd Sub
10. Add this line of code before the 'End Sub' statement:ListBox1.BringToFront()The BringToFront method changes the z index of ListBox1 and moves it in front of other controls.
11. Right-click any area of the code editor and select 'View Designer' to view the form again.
12. Double-click 'Button2.' The code for its click event will appear as follows in the code editor:Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ClickEnd Sub
13. Add this line of code before the 'End Sub' statement:ListBox2.BringToFront()This will bring ListBox2 in front of the other controls.
14. Press 'F5' to run the project. The form will open and show the ListBoxes and buttons. Click the 'Button1' button. 'ListBox1' will overlay 'ListBox2.' Click the 'Button2' button. 'ListBox2' will overlay 'ListBox1.'