How to use Button in C# Windows Form ?

Hello everyone, I hope you guys are having fun with your lives. In today's tutorial, we are gonna have a look at How to use Button in C# Windows Form. In our previous tutorials, we have first discussed the Introduction to C# Windows Forms in which we have created our first C# Windows Forms Project. After that we have discussed How to add C# Controls in Windows Form, in which we have added different C# controls in that Windows form.

So, now we are gonna add back end programming in those Controls one by one. In today's tutorial, I am gonna add the code for the Buttons and you will see that it won't be any difficult to use them in your projects. So, let's get started with How to use Button in C# Windows Form.

How to use Button in C# Windows Form ???

  • So, now we have two buttons in our C# Windows Form, so now First of all, let's change the text of first button from its Properties as shown in below figure:
  • So, I have changed the text of Button1 to Click Here and I am gonna change the text of Button2 to Submit and they will now look like something as shown in below figure:
  • Now you can see in the above figure that the Text on these buttons have changed to that which I have added in the Text section of their properties.
  • Moreover, you must have also noticed that now their size has also increased, which I did by dragging the buttons.
  • Now let's change the name of these buttons too so I am gonna change the Name of first button to ClickHere and that of second button to Submit.
  • We can't add spaces in the Name of the Button.
  • The Name Change for first button is shown in below figure:
  • Now let's add the Click event for each of these buttons.
  • Click event is the event which should happened when you press the button.
  • So, now double click the first button and it will open up its code as shown below:
  • Now you can see in the above code we have a function named ClickHere_Click, which is the click event function for first button.
  • ClickHere is the name of the button which we changed above and _Click is the event. In simple words, this event will call on when the ClickHere Button is clicked.
  • Next we have the arguments, which I am not currently gonna discuss here but we will have a look at them later and then we have these curly brackets { }, and that's where our code is gonna pasted for the click event of this button.
  • So, add the below code into these brackets.
MessageBox.Show("Its a Click Here Button");
  • Now when you pressed the Click Here Button then a sall message box will open up as shown in below figure:
  • So, now you have seen, first we have added the Button then we changed its Text and Name and later we added the back end code in it so that it could perform an event on clicking.
  • Now, I have added a small code in the Submit Button Click event and it looks something like this when you click it:
  • So, that's all about how to use button in C# Windows Form.
That's all for today. Will meet you guys in the next tutorial on C#. Till then take care and have fun!!! :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir