Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 70,206 times

Contents

Related Categories

Windows Forms and Controls - Coding Events - The Buttons

musician

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.

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 application must keep it's 3 most important aspects seperate - presentation (external CSS), structure (XHTML) and behavior (external javascript).

Comments

  • Re: [2433] Windows Forms and Controls

    Posted by musician on 01 Aug 2008

     Only 5 years after the last post eh :) Anyway because things may have changed I recommend you look at the microsoft docs on this - Re: [2433] Windows Forms and Controls

    Posted by san08 on 30 Jul 2008

     

    Hi ,
    I tried to do make the exe of my Windows forms project in the way you had suggested , but to no avail. Here is the code of the bat file that i had made.:-

     %windir%\Mi...

  • Posted by musician on 22 Oct 2003

    It may not work with mdi children.

  • Does the Form 'MinimumSize' property really work?

    Posted by ferrant.be on 11 Jul 2003

    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 resi...