Visual Programming Lectures using Visual Studio 2015 C# Windows Form Application
Lecturer: Saman M. Almufti / Kurdistan Region, Nawroz University
facebook: https://www.facebook.com/saman.malmufti
4. Creating a new Visual
Studio C# Project
• Once Visual Studio is running the
first step is to create a new
project. This will cause the New
Project window to appear
containing a range of different
types of project. Chose Windows
Forms Application
• At the bottom of the window is a
field for providing a project name.
press the OK button to initiate the
creation of the new project.
• Once the new project has been
created the main Visual Studio
window will appear. At the center
of this window will be a new form
in which we will create the user
interface for our sample C#
application.
6. • Every Windows Forms program will use the Form.
• The Form is the most fundamental object used in an
application. By itself, a form does nothing. Its main role is to
host other objects that the user uses to interact with the
computer.
7. Adding Controls
In the design view, you use the mouse to drag controls, which are components
with visual representation such as buttons and text boxes, onto a design surface.
The following illustration shows a combo box that has been dragged from
the Toolbox window onto a form in the Windows Forms Designer.
8. no Property Name Description
1 BackColor Change the background color for the control.
2 Background mage Change the background image displayed in the control.
3 BackgroundImageLayout Change the background image layout .
4 ControlBox Change a value indicating whether a control box is displayed in the caption
bar of the form.
5 Cursor Change the cursor that is displayed when the mouse pointer is over the
control.
6 Font Change the font of the text displayed by the control.
7 ForeColor Change the foreground (font) color of the control. (font)
8 MaximizeBox Change a value indicating whether the Maximize button is displayed in the
caption bar of the form.
9 MinimizeBox Change a value indicating whether the Minimize button is displayed in the
caption bar of the form.
10 Name Change the name of the control.
11 RightToLeft Change a value indicating whether control's elements are aligned to
support locales using right-to-left fonts
12 RightToLeftLayout Change a value indicating whether right-to-left mirror placement is turned
on.
13 StartPosition Change the starting position of the form at run time.
14 Text Change the text associated with this control
15 WindowState Change a value that indicates whether form is minimized, maximized, or
normal.
After you add a control
to the design surface,
you can use
the Properties window
to set its properties,
such as background
color and default text.
9. • A button accepts clicks. In Windows Forms
we use a Button control that accepts click
events and performs other actions in the
user interface.
10. no Property Name Description
1 BackColor Change the background color for the control.
2 Background image Change the background image displayed in the control.
3 BackgroundImageLayout Change the background image layout .
4 Cursor Change the cursor that is displayed when the mouse pointer is over the
control.
5 Font Change the font of the text displayed by the control.
6 ForeColor Change the foreground (font) color of the control. (font)
7 Name Change the name of the control.
8 RightToLeft Change a value indicating whether control's elements are aligned to
support locales using right-to-left fonts
9 Text Change the text associated with this control
11. In this project
• Changing the backcolor of the form (form properties)
• Drag down two buttons from common controls
• hello button (by clicking in button it shows a messagebox (“hello”))
• exit button (by clicking in button the application will be closed)
• Changing the backcolor , forecolor and font of button (from properties)
Code
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("hello ");
}
• Click on Exit button
then the
application will be
closed
12. • lets users type letters and enter data.
Property Name Description
BackColor Gets or sets the background color of the control.
Font Gets or sets the font of the text displayed by the control.
ForeColor Gets or sets the foreground color of the control.
MaxLength Gets or sets the maximum number of characters the user can type or
paste into the text box control.
Multiline Gets or sets a value indicating whether this is a multiline TextBox
control.
Name Gets or sets the name of the control.
PasswordChar Gets or sets the character used to mask characters of a password in a
single-line TextBox control.
Text Gets or sets the current text in the TextBox.
13. In this project
• Drag down a Textbox
• Drag down three buttons from common controls
• hello button (by clicking in this button it shows a messagebox (“hello”))
• exit button (by clicking in this button the application will be closed)
• Hi button (by clicking in this button it will show “hi” in the textbox
• Changing the backcolor , forecolor and font of button (from properties)
• Changing the backcolor , forecolor and font of Textbox(from properties)
15. • The most important characteristic
of a label control is the text it
displays.
• That text is also referred to as its
caption and this is what the user
would read.
• Important property is AutoSize.
16. In this project
• Drag down a three Textbox
• Drag down a three Labels
• Drag down four buttons from common controls
• + button (by clicking in this button textbox3 will show the result of A+B
• - button (by clicking in this button textbox3 will show the result of A-B
• / button (by clicking in this button textbox3 will show the result of A/B
• * button (by clicking in this button textbox3 will show the result of A*B
• Changing the backcolor , forecolor and font of button (from properties)
• Changing the backcolor , forecolor and font of Textbox(from properties)
• Changing the backcolor , forecolor and font of Labels(from properties)
17. int a, b, c;
private void button3_Click(object sender, EventArgs e)
{
a = Convert.ToInt16(textBox1.Text);
b = Convert.ToInt16(textBox2.Text);
c = a / b;
textBox3.Text = Convert.ToString(c);
}
private void button4_Click(object sender, EventArgs e)
{
a = Convert.ToInt16(textBox1.Text);
b = Convert.ToInt16(textBox2.Text);
c = a * b;
textBox3.Text = Convert.ToString(c);
}
18. private void button2_Click(object sender, EventArgs e)
{
a = Convert.ToInt16(textBox1.Text);
b = Convert.ToInt16(textBox2.Text);
c = a - b;
textBox3.Text = Convert.ToString(c);
}
private void button1_Click(object sender, EventArgs e)
{
a = Convert.ToInt16(textBox1.Text);
b = Convert.ToInt16(textBox2.Text);
c = a + b;
textBox3.Text = Convert.ToString(c);
}
19. • PictureBox provides a rectangular region for an image.
• It supports many image formats.
• Allows you to include images on your form.
• Important Properties:
• Image (Browse to file)
• Location
• SizeMode
• Normal, StretchImage, AutoSize, CenterImage, Zoom
• BorderStyle
21. presents an interface for date selection. It allows the
user to select a valid DateTime without much effort
on your part.
22. In this project
• Drag down a button (by clicking in the button a message will
show the date )
• Drag down a dateTimePicker (by double click in the
dateTimePicker you can change the date (year, month and day)
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(dateTimePicker1.Text);
}
23. private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker1.CustomFormat = "ddd,dd-MM-yy";
dateTimePicker1.Format = DateTimePickerFormat.Custom;
}
Any format
Example
dd,mm,yy
ddd-mmm-yyy