Library tutorials & articles

Windows Forms and Controls

Coding Events - The Buttons

Apart from this strange looking form all you can do is type some text in the textbox but that’s about it. Close your application if it’s open and set the form’s opacity back to 100%, before we get sick of it. Now double click on the cbAccept button and you’re into the code behind the form and specifically the click event of this button. Again this terminology and procedure might be familiar but it’s worth taking note of it all. What you see for this button is the following:

Private Sub cbAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbAccept.Click

End Sub


This event handler is a lot more meaningful than anything you’ve seen before. Forms in .Net are classes and this is a private procedure of the form that handles the .Click event of the cbAccept button. So lets put some code in there for this and the cbCancel button.


Private Sub cbAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbAccept.Click
       MsgBox("Changes Applied!", MsgBoxStyle.OKOnly)
End Sub

Private Sub cbCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbCancel.Click
       Me.Close()
End Sub

a
For the accept button I just put in an old fashioned MsgBox with a title and appearance. Using the VS.Net intellisense I recommend you experiment with function like the MsgBox to see what you can do. For example you could instead of MsgBoxStyle.OKOnly use MsgBoxStyle.OKOnly and MsgBoxStyle.Exclamation. This combines both styles to produce an OK only MsgBox with an exclamation icon on it. As with a lot of VB.Net you’ll find there is a lot in there and for very specific things you often have to figure it out yourself or post to a MessageBoard of course :)

For the Cancel button I used Me.Close to close the application. The Close method closes the reference to the form where Me is the form we’re dealing with. Any other references to the form will have to be closed too. If you call the Close method on the project startup form then all forms of the application will be closed and any references destroyed.

Finally go back to the designer and change 2 more properties on the from. Set AcceptButton to cbButton and CancelButton to cbCancel.

Now start your project again and test it out. To close the application you click the x in the top-right corner of the form, click the Cancel button or press the Esc key on your keyboard. Setting the CancelButton of the form allows this and likewise we can call up the MsgBox by clicking the Accept button or pressing Enter. Of course if the focus is on the Cancel button then Enter will close the form as the selected button will take precedence when pressing the Enter Key. You would use these properties for logins mainly and it’s a lot easier than coding the KeyPress event of a control.

Comments

  1. 01 Aug 2008 at 19:22

     Only 5 years after the last post eh :) Anyway because things may have changed I recommend you look at the microsoft docs on this - http://msdn.microsoft.com/en-us/library/ms229597(VS.80).aspx

  2. 22 Oct 2003 at 16:26

    It may not work with mdi children.

  3. 11 Jul 2003 at 03:26

    I use VB.Net (framework 1.0.3705) on Windows 2000, and I noticed the 'MinimumSize' property doesn't seem to have any effect on my forms (MDIChildren). Does that property really prevent users from resizing a form to a smaller size than the MinimumSize? Any other experiences?


    Thank you,

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Windows Forms and Controls.

Leave a comment

Sign in or Join us (it's free).

Brian O'Connell Microsoft Certified Applications Developer with 10 years experience developing web based applications using asp, asp.net for a Local Authority in Dublin. Clings to a firm belief that a web applicat...

Related podcasts

  • xpert to Expert: Inside Concurrent Basic (CB)

    "Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...

We'd love to hear what you think! Submit ideas or give us feedback