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