Lesson Objectives
At the end of this lesson, you will know…
- How to create functions to handle button clicks in the UI
Creating functions to handle the user’s input
Here’s a video of me creating the functions that will be run when a player presses a button on the screen.
Step 1: Start Visual Studio Express 2013 for Desktop, and open the solution.
Step 2: Right-click on the SuperAdventure.cs for, in the SuperAdventure project, then select “Open” or “View Designer”.
Step 3: Double-click on each of the buttons on the form – the four direction buttons (North, South, East, and West) and the two “Use” buttons that will be used for combat.
It doesn’t matter what order you do these in.
After you double-click each button, you’ll be taken to the “code” part of the screen and be placed where the new function was created. This is where you can write the code that will run when the user presses the connected button. So, when the user clicks the “North” button, this will be the code to “move” the player to the location to the North.
Something you don’t see when you do this is the code that gets created to connect the buttons to the new functions.
You might have noticed that the code for the UI screen says “public partial class SuperAdventure : Form”. The word “partial” is important here. There is another part of this class that you don’t normally see. This is the part that connects the controls (buttons, comboboxes, etc) in the UI to the code we’ve been looking at.
We haven’t looked at the other part of this class, because we’re not focusing on how to build the UI in these guides. However, it’s something you need to be aware of if you’re creating Windows Form programs.
There are also other things you can handle from the UI. These are called “events”.
In this case, the only event we care about is when the button click. However, there are dozens of other events that can be handled. For example, many controls have an “OnMouseOver” event – when the user moves the mouse over the control. For that event, you might do something such as change the background color of the control – so the user knows where the focus is.
Summary
Now you can create functions to handle the common user actions in the UI – in this case, when the user clicks on a button. Right now, the functions don’t do anything. So our next lesson will be to start writing our functions, to let the player play the game.
Source code for this lesson
These is no source code for this lesson
Next lesson: Lesson 14.1 – Variables
Previous lesson: Lesson 13.1 – Functions, procedures, and methods
All lessons: Learn C# by Building a Simple RPG Index
Hello, I’d like to ask how to remove a function of a button from the main code. I accidentally created additional ones while testing some things (since I am pretty new to programming and was curious) and now, when I try an delete them from the code, I get an error and the program won’t start.
I guess it isn’t too big of a deal, but I want to clean them up for the sake of readability.
Thank you in advance!
Hi Alex!
Visual Studio probably created some “eventhandlers” for the buttons. You can read more about them in lesson 21.3.
You’ll need to go into the SuperAdventure.Designer.cs file and remove the eventhandlers for those buttons.
Let me know if that doesn’t help.