1. How to drag a form without borders?
This function can be implemented in VB6 only by using API functions. In VB. NET, You can implement it with your own functions. First, set the formborderstyle attribute of the form to none to remove the border of the form, and then add a button on the form. The code in the form is as follows:
Code:
- Public class form1
- Inherits system. Windows. Forms. Form
- Private mouse_offset as point
- Private sub form=mousedown (byval sender as object, byval e as system. Windows. Forms. mouseeventargs) handles mybase. mousedown
- Mouse_offset = new point (E. X, E. Y)
- End sub
- Private sub form=mousemove (byval sender as system. Object, byval e as system. Windows. Forms. mouseeventargs) handles mybase. mousemove
- 'Drag the form by pressing the left and right buttons.
- If E. Button = mousebuttons. Left or E. Button = mousebuttons. Right then
- Dim mousepos as point = sender. findform (). mouseposition
- 'Get the mouse offset
- Mousepos. offset (-mouse_offset.x,-mouse_offset.y)
- 'Set the form to move with the mouse
- Sender. findform (). Location = mousepos
- End if
- End sub
- Private sub btnexit_click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
- 'Close the form
- Me. Close ()
- End sub
- End Class
Ii. Mutual calls between multiple forms
In VB6, multiple forms can be easily called to each other. For example, in form1, you only need to use a "form2.show" statement to display the form form2. However, in VB. net: before accessing a form, you must instantiate the form. If there are multiple codes in the project to access the same form, you must pass the same instance pointer to the code. Otherwise, the newly created form instance will no longer be the original form.
The following code calls form1 and form2, and form1 is the main form. The title of the button btnshowfrm2 on form1 is "show form2", and the title of the button btnshowfrm1 on form2 is "show form1 ".
1. Code in form1:
Code:
- Public class form1
- Inherits system. Windows. Forms. Form
- 'Create a new instance of form2
- Dim frm2 as new form2 ()
- Public Function instance2 (byval frm as form2)
- Frm2 = FRM
- End Function
- Private sub btnshowfrm2_click (byval sender as system. Object, byval e as system. eventargs) handles btnshowfrm2.click
- 'The following statement ensures that form1 is accessed in form2 and other forms,
- 'Will get the same form instance of form1.
- Frm2.instance (me)
- Frm2.show ()
- Me. Hide ()
- End sub
- End Class
2. Code in form2:
Code:
- Public class form2
- Inherits system. Windows. Forms. Form
- Dim frm1 as form1
- 'Use a new instance property to generate an instance of the form frm1
- Public Function instance (byval frm as form1)
- Frm1 = FRM
- End Function
- Private sub btnshowfrm1_click (byval sender as system. Object, byval e as system. eventargs) handles btnshowfrm1.click
- Me. Hide ()
- Frm1.show ()
- End sub
- Private sub form2_closed (byval sender as object, byval e as system. eventargs) handles mybase. Closed
- 'If form2 is disabled, the button btnshowfrm2 with form1 set is unavailable.
- Frm1.btnshowfrm2. Enabled = false
- Frm1.show ()
- End sub
- End Class
All the above Code is successfully debugged on Windows XP and VB. NET.