How to Open Message Dialog Box with button In C#- this a very basic article about a message dialog box. in this article we will learn how to open a dialog box by pressing a button.
After installing the programming system Microsoft Visual C# 2010 Express, the VCSExpress.exe system load module will be most often located in the folder: C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE.
After starting the system, we will see the initial user interface shown in Fig. 1.
To program any task, it is necessary to create a New Project. So click on the file and select the new project. In the left column of the New Project window that opens, there is a list of installed templates (Installed Templates). Select the Visual C# language, and then in the templates area (in the middle column) click on the (Templates) Windows Forms Application and enter the name of the project (Name), in my I enter project1 and click on the OK button.
As a result, you will see the window shown in Fig 3.
This window shows a screen form – Form1, in which programmers have located the various components of the graphical user interface or, in some other way called, controls: fields for entering text TextBox, command buttons Button, lines of text in the form (Labels that cannot be edited user) and so on.
Main Purpose of our Project:
In our first program, we will create a label for displaying, for example, “Programming Digest”. In addition to it, the form will contain a command button and the button caption is “Click me”. Clicking the button will open a dialog box with the message “Hello Fawad khan!”
Adding Button and Label in C#:
Now let’s add the named controls to the form. For this we need will achieve the Toolbox Control Panel. If you are currently a panel of elements that do not see the cops, then you can add it, for example, using the key combination <Ctrl> + <Alt> + <x> or by executing the menu command View | Toolbox. So add the Label and Button into the form by double-clicking on these items from Toolbox. And then arrange them approximately as shown in fig. 4.
Any such object can be created independently, or you can use a ready-made one. In this task, we use ready-made visual objects that can be also just drag and drop from the Toolbox into the window forms.
At the same time, we need to know that each object has properties (Properties). For example, the properties of the button in c# are (Fig. 5): the name of the button (Name) in my case, btn1, the caption (Text) on the button is click me, the location of the (Location) button in the coordinate system of the form we are X, Y, the size of the button (Size), etc. There are many properties, all of them can be seen in the panel Properties (see Fig. 5), which will open when you right-click the mouse within the form and select the Properties command from the context menu. By hovering over all other controls on the form, you can view and their properties: Form1 forms and labels in the form – Label1 labels.
For the Label1 object, select the Text property and set the text: Programming Digest (instead of label1 text).
So that objects have properties, we have already understood. You should also know that objects handled by events. An event, for example, is a click on a button in c#, a click within the form, loading (Load) the form into RAM at the start of the program. Manage this or that event by writing a procedure for working of an event in the program code. To do this, first, you need to get “empty” an event handler (that is, a stub of a program code). In our task, the only actual event we control is the click on the command button In c#. To get an empty handler for this event, in the properties of the button btn1 (see Figure 5) click on the Events lightning icon, and in the list of all possible btn1 events, select the Click event by double-clicking or simply double click on the button the form1.cs tab will be opened. After that, before we will open the tab of the program code Form1.cs (Fig.7).
In the Form1.cs tab, we can see that the Microsoft Visual C# 2010 Express managing environment generated four lines of code. The reader should not be horrified looking at this Nice code – a lot will gradually become clear.
So, in fig. 7 shows the program code describing the “empty” we mentioned event handler btn1_Click. Here between curly braces following with a private void operator, we can write commands to be executed after the user clicks on the command button btn1.
As you can see, we now have two tabs: the Form1.cs code tab and tab of the visual project of the Form1.cs [Design] program (another name for this tabs – form designer). You can switch between them with the mouse or by pressing We use the key combinations <Ctrl> + <Tab>, as is customary for switching between tabs in Windows, as well as the <F7> function key.
Let me remind you that according to the condition of the task, after clicking on the btn1 button, the dialog box that says: “Hello Fawad khan!” To do this, in curly braces, handle Let’s write the events:
example: open message dialog box with a button in c# Programming:
public partial class Form1 : Form
private void btn1_Click(object sender, EventArgs e)
MessageBox.Show("Hello Fawad khan!");
MessageBox.Show(“Hello Fawad khan!”);
This statement calls the Show method (program) of the MessageBox object.
Thus, we wrote a procedure to handle the Click event on the button btn1. Now press the <F5> key and check the program is working.
Congratulations, you have written your first program in Microsoft Visual C# 2010 Express!
My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel “Expertstech”, and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on…