Warning: INSERT command denied to user 'dbo292345962'@'74.208.59.105' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:12:\"user warning\";s:8:\"%message\";s:400:\"INSERT command denied to user 'dbo292345962'@'74.208.59.105' for table 'captcha_sessions'\nquery: INSERT into captcha_sessions (uid, sid, ip_address, timestamp, form_id, solution, status, attempts) VALUES (0, '8111c1b2fb38ec28ce757eaf28f96d33', '34.204.175.38', 1569103735, 'comment_form', '48dc1795a35e9e1d9951a16a1957d8b1', 0, 0)\";s:5:\"%file\";s:62:\"/homepages/25/d199835659/htdocs/ID/modules/captcha/captcha.inc\";s:5:\"%line\";i:99;}& in /homepages/25/d199835659/htdocs/ID/includes/database.mysql.inc on line 135
Learning C# GUI Event Handling | Inferno Development

Learning C# GUI Event Handling

C# is an object oriented language that is usually programmed with Visual Studio. Visual Studio makes the designing of GUI extremely easy. C# also makes event handling very simple as well. Anyone can learn how to create simple button driven programs with very little code.

toc_collapse=0;
Contents 

First, you should create a C# project (GUI project) in Visual Studio (Express is free). Then try to open up the design form.

Designing on Visual Studio

Let's get started by opening up Visual Studio's Visual C#, we can quickly design a button with a progress bar. I've also added a tooltip and some menu options (though we won't use the menu options today). Playing around with visual studio can teach you a lot.

C# Button

Next you can go to the Form code.

Developing the Event Handler

Event handling in C# is fairly straight forward.

First you initialize the component on your form (mine's called Form1.cs):

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

This initializes the GUI that you just designed.

Event handling works on functions with the parameters object sender, and EventArgs e.

Event Handling Buttons and Progress Bars

You can add event handler for each form element. We'll generate a tooltip and a progress bar movement each time the button is clicked.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            toolTip1.ToolTipTitle = "You clicked!";
            toolTip1.Show("Hey you clicked button", button1, 10,15,1504);
            toolTip1.UseFading = true;
            toolTip1.UseAnimation = true;
            this.Text = "Clicked Button";

            Button b1 = (Button)sender;
            // you can also grab the sender button
            b1.Text = "I'm Clicked!";

            progressBar1.Visible = true;
            progressBar1.Increment(15);
            if (progressBar1.Value == progressBar1.Maximum)
            {
                progressBar1.Value = 0;
                progressBar1.Visible = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}

When the button is clicked, the progress bar will appear ( .Visible = true), the window title will change ( this.Text ), a tooltip will show up (toolTip1), and the progress bar will increment by 15 each click.

Also button itself can be modified by changing the text, using the sender parameter.

When the progress bar reaches its maximum value, it resets back to zero and disappears.

Al's picture

Hey thanks for this tutorial,

Hey thanks for this tutorial, I never knew GUI creation is so simple in C#.

Bob's picture

Heh, I like the tooltip idea,

Heh, I like the tooltip idea, I should use that in my programs. Thanks for this!

darley's picture

Where's the .Click +=

Where's the .Click += EventHandler stuff?

Tomescu Alin's picture

So how exactly do you "link"

So how exactly do you "link" your button to the event handler if you don't use that .Click += EventHandler stuff? Is it done automatically after a naming convention? Such as if your button is named [button_name] then it automatically has handlers like [button_name]_Click(), and you just need to define these?

————

Get Linux or die tryin'

Baran Ornarli's picture

See, the code I have is a

See, the code I have is a partial class. It means it's only HALF the class!!!

Visual Studio creates a Form1.designer.cs (which you see visually, but it's actually the other half of the code).

That designer.cs has all the .Click += EventHandler code. Read the code with Visual Studio, you'll see (text version).

VS takes care of things and allows you to basically just double click a button and automatically generate code for it! That's what's nice about C# and Microsoft--you can do the same with Java if you purchase the Sun developer studio for a lot of money-- OR with C++ Qt, you can use like Qt Creator or other IDEs.

Jennifer's picture

Nice tutorial, I like your

Nice tutorial, I like your explanation of gui events. Been looking all over bing and google.

Rhiannon Qi's picture

Excellent article, I think

Excellent article, I think I'll send this over to my cousin who's learning GUI in C#.

Pandora's picture

The progress bar was a really

The progress bar was a really cool feature to implement. Thanks.

Joanna Marwaha's picture

wow, it's so easy in Visual

wow, it's so easy in Visual Studio, I really should stop coding it in notepad.

Kate Traher's picture

How much does Visual Studio

How much does Visual Studio cost? What are the disadvantages of VS express?

Christopher's picture

Thank you for explaining

Thank you for explaining this, I was having trouble getting my gui to work.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.