How to Add Event Handler Method to Command Button
Description

Learn how to add Event Handler in command button on the form.
Transcript
So, the next step is to add some event handlers to the buttons on the form. So at the moment, we will just start off for the first button. So, I will select the button and over here in the properties window, w e will have a look at the events on that control. So, clearly it is the click event that we are interested in. Now, do not do this but if you did double click on this event, it would add a method to the code behind class and the name of the method would be generated by Visual Studio. So for example, if our control is called “This Year Button” then the event method would be called “thisyearbutton_click” and that is fine in most divisions but bear in mind that the event handler code for this button and this button is going to be fairly similar. So, I am actually going to use the same handler method for both buttons. So, I do not really want to choose a name that is based one button or the other. I want to choose a more generic name. So, I am going to type in a name for the event method in here. I am going to call the method “thebutton_click”. Okay and I will press enter and Visual Studio has added that method, the event handler to our class.
So, what I want to do in here is I want to get the users name and I want to display a list of all the countries the user has visited this year or last year. So, let us start doing that. So, I am going to append to the ResultLabel, a string.And the string is going to be formatted like this, “Countries visited by” and then we will have a place holder for the person’s name and then we will have a line break. And let us put in the name textbox value, okay. And just to make sure there that you do have plus equals, I really do want this to be accumulative addition to the ResultLabel. I want to explain exactly how the label remembers its previous value and adds to it each time you do a round trip to the server. So, make sure you do have a plus, equals there not just an equals.
Okay, so that has added the name of the person and now we are going to add a list of all the countries that the user has added. Now then, if you have a multiple selection list box and you want to figure out which items have been selected, you have to unfortunately looped through each item and check each item in turn to see if it’s being selected. There was not any easy way to say to the list box, “Please give me back all your selected items.” You have to manually activate through each item yourself. So, we are going to write their loop but you are just going to activate through each list item and the country list. And then for each of those items, we are going to check if that item was selected by the user, okay. So, you might remember this selective property which tells whether or not that item is selected and if it was selected, in other words this is a country that the user has visited that we want to add that to our label.
So, Resultlabel.text and we are going to add to that label. Let us just put a star in there, just for a static purposes and we will close it and then we will have the actual value of this item. So, that would be name of the country and we will also put end quotes, a line break after each item so that they get displayed nicely on the screen. Okay. So, that is the end of the statement and that is the end of our loop and then finally, we will just add another line break at the end of our ResultLabel.
So, let me just summarize briefly what we have done. The button click handler will append to the label, the name of the user and list of all the countries that the user has selected in the list box. Let us run it and see what happens, Control-F5. So, let us enter a name. So, we will go for Joe, and let us say Joe has visited Australia and Germany and we will click “visited this year” and that says, Countries visited by Joe, Australia and Germany. And now let us enter somebody else’s name so we will go for Bernie. And Bernie has visited Canada and France. And click the button. Okay, that is great!
Related Articles
How to Add Event Handler Method to Command Button
Lots of comedians we see are male, which sometimes makes us a little bored. It is Chelsea Handler that makes things different from what we often think of comedians. Not only is she an amazing comedian, but she also surpasses many male ones. Brassy, Beautiful and candid are precise words to describe Chelsea. She has a cutting wit to back up her good looks as a movie stars.
...
In the Linux command line shell, moving around from directory to directory, adding new directories, and deleting old directories is easy once you know the proper commands. Again, with Linux, you are limited only by your brain! If you know all the commands, then you weild great power. But if you know only a few commands, it\'s like you are standing in the airport in Tokyo and want to know where the bathroom is, but only know how to say hi and bye in Japanese....
Remove The Control Commander - How to Remove The Control Commander On your PC....
This article takes a close look at the Ten Commandments, their bearing on history, and especially their relationship to the new covenant. How are Christians today to relate to the Ten Commandments?...
Have you been thinking of finding out more information about sales management association? Then read this article to find out more!...
The MTV Video Music Awards (VMAs) needed a controversial figure for the show\'s 2010 edition to follow up Kanye West\'s bad-boy outburst in 2009, and they found it in comedienne Chelsea Handler....
Chelsea Handler has never been one to hold back when it comes to her signature brand of capricious comedy, especially on her late-night TV show \"Chelsea Lately\"....
When compared to IMAP for handling email requests, POPO3 remains the more popular choice. Although many people are quite familiar about POP3, when running dedicated servers, it is important to know more about it as well as how it typically works....
Once in a while, requirements for work can also have an effect on a person\'s home life and the San Antonio Texas food handler course is just one example. Learning how to properly store, handle, prepare and serve food is required....
Getting your food handlers' card is generally required within thirty days of beginning work in any food service industry. This isn't just fast food--some food scientists and even caterers will need one. But now that many states make it easy to obtain one online and even print out your own copies, it's so simple and straightforward, everyone should get their own!...