Chapter 1-Introduction To WinForms and Controls
Chapter 1-Introduction To WinForms and Controls
https://sites.google.com/a/quest.edu.pk/dr-irfana-memon/lecture-slides
Course Content (1)
Part 1: Console Based Applications
Introduction to Microsoft Visual C# and Visual Studio
Working with variables, Data types, Operators, and Expressions
Visual C# Programming Language Constructs, Creating Methods
Invoking Methods, Handling Exceptions, Creating overloaded Methods
Using Decision Statements
Winform GUI
Form (Properties, Methods, and Events)
Controls in WinForm
7
Create Project (Windows Forms
app)
On the Create a new project window, choose the Windows
Forms App (.NET Framework) template for C#.
(If you prefer, you can refine your search to quickly get to the
template you want. For example, enter or type Windows Forms
App in the search box. Next, choose C# from the Language list,
and then choose Windows from the Platform list.)
11
About Windows Form Project
• Main window: In this window, you'll do most of your work,
such as working with forms and editing code. The window
shows a form in the Form Editor.
• Solution Explorer window: In this window, you can view
and navigate to all items in your solution.
• If you choose a file, the contents of the Properties window
18
.NET Windows Form Properties
Name Availability Description
Anchor Read/Write Using this property, you can specify
how the control behaves when its
22
Anchor and Dock Properties
• These two properties are especially useful when you are
designing your form.
• The Anchor property is used to specify how the control
23
Anchor and Dock Properties
• The Dock property is related to the Anchor property.
• You can use it to specify that a control should dock to an
edge of its container.
24
.NET Windows Form Events
• When a user clicks a button or presses a button, you as the
programmer of the application, want to be told that this has
happened.
25
.NET Windows Form Events
Name Description
Click Occurs when a control is clicked. In some cases, this
event will also occur when a user presses Enter.
30
Button Control
• A Button is an essential part of an application, or software, or
webpage. It allows the user to interact with the application or
software.
33
Button Control (Properties)
Name Availability Description
35
Button Control (Example)
• Create a dialog with three buttons.
1. Open Visual Studio.NET and create a new C# Windows.
Application.
37
Button Control (Example)
• Create a dialog with three buttons.
39
Button Control (Example)
• Create a dialog with three buttons.
4. What we want to do now is to display a message box
whenever the button is clicked. So we need the coding window.
40
Button Control (Example)
46
Message Box
• There are only three items on the list now, and all Methods
(you can tell they are Methods because they have the purple
cube icon next to them) Double click on Show, and it will be
added to your C# code:
47
Message Box
• As soon as you type the left round bracket after the "w", you'll see
all the different ways that the Show method can be used.
• There are 21 different ways in total.
• At this time, you don't have to pursue through them all! Type the
following, after the left round bracket (Don't forget the double
quotation marks.):
"Message"
52
Other Button Options
• The one that adds buttons to a message box is, you won't
be surprised to hear, MessageBoxButtons.
• Press the enter key on your keyboard when this option is
highlighted. I
• t will be added to the your code.
• Now type a full stop (period) after the final "s" of
53
Other Button Options
• Double click the one for YesNo, and it will be added to your
code.
• Run your program again, and click your button. Your
Message Box will then look like this:
56
Practice on the use of Button
57
Checkbox Control
CheckBox has 2 or 3 states. This control provides a way for an
option to be selected and deselected independently of other
58
Checkbox Control (Example)
• Create a dialog with one button and three checkbox.
1. Open Visual Studio.NET and create a new C# Windows.
Application.
60
Checkbox Control (Example)
• Create a dialog with one button and three checkbox.
62
Checkbox Control (Example)
63
Checkbox Control (Example)
65
Checkbox Control (Example)
• Double click your Select button to open up the code window.
Our code will make use of the Checked property of
Checkboxes. This is either true or false. It will be true if the
user places a check in the box, and false if there is no check.
71
RadioButton Control (Example)
1. In the properties area on the right, notice that the default
Name property is radioButton1, radioButton2,
72
RadioButton Control (Example)
• Run your form and test it out. What you should find is that
73
RadioButton Control (Example)
• Double click your Select Department button to open up the
code window. Our code will make use of
the Checked property of radioButtons. This is either true or
false. It will be true if the user places a check in the box, and
false if there is no check.
76
Label Controls
• One useful control in Windows Forms is the Label control.
• This control serves as an invisible frame where you can place
text.
77
PictureBox Controls
• PictureBox provides a rectangular region for an image. It
supports many image formats.
• It has an adjustable size.
78
LinkLabel Control
• A LinkLabel control is a label control that can display a
hyperlink.
• A LinkLabel control is inherited from the Label class so it has
all the functionality provided by the Windows Forms Label
control.
• LinkLabel control does not participate in user input or
79
TextBox Control
TextBox. This lets users type letters and enter data. It is part
of the Windows Forms platform and is used with C# code. It
80
TextBox Controls (Example)
We can add two more controls to the form, a Button and a
Text Box.
83
Remaining Control
85
DateTimePicker Control
• The DateTimePicker control is used to select date & time, and to
display date and time in the specified format.
• And you know that the easiest way to set properties is from the
87
DateTimePicker Control (Properties)
dateTimePicker1.Value = DateTime.Today;
The Value property contains the current date and time the
control is set to.
88
DateTimePicker Control (Properties)
• DateTime iDate;
• iDate = dateTimePicker1.Value;
• dateTimePicker1.Format = DateTimePickerFormat.Short; 89
Convert string to DateTime
90
DateTimePicker Control (Properties)
Name: Name property represents a unique name of a
DateTimePicker control. It is used to access the control in the code.
Value: The Value property represents the currently set date time
91
value in a control.
MaxDate: The MaxDate used to set the maximum value of date and
time can be selected by a control.
Properties:
•Name
Properties:
94
MonthCalender Control
(Properties)
SelectionRange
SelectionStart
FirstDayOfWeek
FirstDayOfWeek property gives you an option to start week in the
application with your preferred day. By default Sunday is selected as
the start of the week and Saturday is considered as the last day of 95
the week.
MonthCalender Control
(Properties)
ShowTodayCircle
96
MonthCalender Control
(Properties)
MaxDate and MinDate
97
MonthCalender Control
(Properties)
•CalendarDimensions
CalendarDimensions determines the number of months in a single
grid.
Default dimension is set as (1,1) which will only display one month
range in the grid.
Maximum 12 month can be displayed in a single grid.
Maximum dimension you can set is (4,3) which shows 12 months in
98
MonthCalender Control
(Properties)
•TodayDate
BoldedDates
It is used to highlight some dates on the calendar.
99
MonthCalender Control
•ShowWeekNumbers
(Properties)
ShowWeekNumbers property allows you to display week
number on the calendar. By default this property value is set as
false.
Set this value as true if you want to display number of weeks
100
MaskedTextBox Control
• MaskedTextBox accepts text input of a specific format.
• We often require phone numbers to have their area code and
also the correct number of digits.
103
ProgressBar Control (Example)
4. Double click on button, it transfer you code page. Do this
coding:
104
ProgressBar Control (Example)
6. Run, click button and see progress filling.
108
Panel Controls
To create a Panel Control at design-time, you can drag and drop a
Panel Control from the Toolbox to a Form in Visual Studio.
After you dragging and dropping a Panel Control to the Form, the
109
Panel Controls (Properties)
After you place a Panel Control on a form, the next step is to
set its properties.
The easiest way to set properties is from the Properties
Window.
110
Panel Controls (Properties)
• The Panel has most of the common properties
• Important properties of the Panel control are shown in
Table.
Properties
•The GroupBox has most of
the common control
properties, such as Location,
Height, Width, and Size,
Background and
Foreground, Name, Text, 112
and Font.
FlowLayoutPanel
•FlowLayoutPanel control is used to arrange its child controls
in a horizontal or vertical flow direction.
•Or in other words, FlowLayoutPanel is a container which is
used to organize different or same types of controls in it either
horizontally or vertically.
•The FlowLayoutPanel class is used to represent windows flow
Properties:
114
TabControl
The TabControl control enables you to group sets of controls in
tabs, rather like files in a filing cabinet or dividers in a
notebook. For example, you might create property pages for an
application in which each page represents the properties of a
specific component.
116
Wish You Good Luck