Two tips for using VB. NET forms

Source: Internet
Author: User

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:
  1. Public class form1
  2. Inherits system. Windows. Forms. Form
  3. Private mouse_offset as point
  4. Private sub form=mousedown (byval sender as object, byval e as system. Windows. Forms. mouseeventargs) handles mybase. mousedown
  5. Mouse_offset = new point (E. X, E. Y)
  6. End sub
  7. Private sub form=mousemove (byval sender as system. Object, byval e as system. Windows. Forms. mouseeventargs) handles mybase. mousemove
  8. 'Drag the form by pressing the left and right buttons.
  9. If E. Button = mousebuttons. Left or E. Button = mousebuttons. Right then
  10. Dim mousepos as point = sender. findform (). mouseposition
  11. 'Get the mouse offset
  12. Mousepos. offset (-mouse_offset.x,-mouse_offset.y)
  13. 'Set the form to move with the mouse
  14. Sender. findform (). Location = mousepos
  15. End if
  16. End sub
  17. Private sub btnexit_click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
  18. 'Close the form
  19. Me. Close ()
  20. End sub
  21. 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:
  1. Public class form1
  2. Inherits system. Windows. Forms. Form
  3. 'Create a new instance of form2
  4. Dim frm2 as new form2 ()
  5. Public Function instance2 (byval frm as form2)
  6. Frm2 = FRM
  7. End Function
  8. Private sub btnshowfrm2_click (byval sender as system. Object, byval e as system. eventargs) handles btnshowfrm2.click
  9. 'The following statement ensures that form1 is accessed in form2 and other forms,
  10. 'Will get the same form instance of form1.
  11. Frm2.instance (me)
  12. Frm2.show ()
  13. Me. Hide ()
  14. End sub
  15. End Class

2. Code in form2:

Code:
  1. Public class form2
  2. Inherits system. Windows. Forms. Form
  3. Dim frm1 as form1
  4. 'Use a new instance property to generate an instance of the form frm1
  5. Public Function instance (byval frm as form1)
  6. Frm1 = FRM
  7. End Function
  8. Private sub btnshowfrm1_click (byval sender as system. Object, byval e as system. eventargs) handles btnshowfrm1.click
  9. Me. Hide ()
  10. Frm1.show ()
  11. End sub
  12. Private sub form2_closed (byval sender as object, byval e as system. eventargs) handles mybase. Closed
  13. 'If form2 is disabled, the button btnshowfrm2 with form1 set is unavailable.
  14. Frm1.btnshowfrm2. Enabled = false
  15. Frm1.show ()
  16. End sub
  17. End Class

All the above Code is successfully debugged on Windows XP and VB. NET.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.