Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

VS2012 : C# Windows Forms guide

JavaScript Is Best Script
Joined
Dec 13, 2010
Messages
631
Reaction score
131
Today i'll be introducing to you guys Windows Forms programming as well as how to add and use some of the controls available.

Prerequisites :
Code:
Knowledge of C# (does not have to include Windows Forms programming knowledge)
Visual Studio 2012

So what is "Windows Forms"? Basically WinForms (short form of Windows Forms) is, according to the holy Wikipedia, "the name given to the graphical application programming interface (API) included as a part of Microsoft .NET Framework, providing access to native Microsoft Windows interface elements by wrapping the extant Windows API in managed code."
As you see by the definition above, a Windows Form is build on .NET Framework. Which means that one can program the same product using any language as long as he or she is programming on top of .NET. Some languages include Visual Basic, Managed C++ and C#. I'll be using C# today because im best at it :D

GUI programming are usually event-driven, and Windows Forms is of no exception. What does event-driven means, and how does it differ from normal code? According to the all-holy Wikipedia, it is defined as follows: "In computer programming, event-driven programming (EDP) or event-based programming is a programming paradigm in which the flow of the program is determined by events—e.g., sensor outputs or user actions (mouse clicks, key presses) or messages from other programs or threads."
Now how does it differ from normal code? You could think of it as this : normal code is something like you executing the following actions : Stand up -> Walk 5 meters forward -> Kiss your wife.
On the other hand, event-driven is something like this : Wife kisses you -> Get a shock and jump up -> Kiss your wife back.

Alright now let's get to the programming part.

Step 1) Create a Windows Forms project:
1.1) Launch Visual Studio 2012
1.2) On the top left hand corner, click FILE -> New Project
1.3) Browse in the new window to Templates -> Visual C# -> Windows
1.4) Select "Windows Forms Application" and hit "OK"

Step 2) Adding controls to the form:
Now that you've created the project, you'll see a Form Designer popping up with a Form in the middle section. That's where you're gonna add you're controls and stuffs.
2.1) Make sure you have the toolbox windows out. If you haven't, go to VIEW -> Toolbox
2.2) From your toolbox drag the control you wish to add into the form in the section in the middle. We'll use a button for this example.

Step 3) Hooking up events
Now that you've added the control to the form, you'll need to hook up the event (literally) so that the button knows whenever it has been clicked, and will from there execute the task the coder has assigned it to do.
3.1) To hook up the event, simply double click on the button you have just added in to the form.
3.2) You will be brought into the code view of the form you were designing, into a method named something like "private void button1_Click(object sender, EventArgs e)". This is where all the logic takes place. It's called the event handler of the click event of button1 actually.

Step 4) Adding logic to control events
Well now that you've hooked up the event, just programming as per normal!
4.1) The event handler will be called whenever the button gets pressed. So you add what you wanna do when your button gets pressed into this event handler. Let's say we wanna set the text of the button to "Potato" whenever it gets pressed. We simply do this:
Code:
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Text = "Potato";
        }
Pretty simple innit? That's right.

Step 5) Debugging
Debug as per normal, hit F5 and a form will appear. Click the button you've added and it's text should change to "Potato". Hit F5 again to stop debugging as per normal.

Step 6) Extending to other events
Let's say you wanna do something whenever your mouse hovers above the button. You'll have to hook up this event to the button.
6.1) To do so, in the constructor method of the form, below "InitializeComponent();", add the following text : "button1.MouseHover += button1_MouseHover;"
6.2) Create a method named button1_MouseHover accepting parameters matching that of the MouseHover delegate.
6.3) Inside button1_MouseHover, do the same as you did for the button clicked event code.

Well, that's all :D
 
Back
Top