Lesson 14 - Other Windows Forms Controls
In the previous lesson, Basic Windows Forms Controls, we described several basic form controls,
these were Button
, Label
, TextBox
,
NumericUpDown
, ComboBox
, ListBox
, and
CheckBox
. In today's C# .NET tutorial, we're going to continue the
Toolbox tour.
RadioButton
RadioButton
is very similar to CheckBox
. The
difference is that only one RadioButton
inside the current
container can be checked at a time. If you have more of them and check one, the
others will be unchecked. So we can always choose only one from a set of
options. We usually use RadioButton
controls when there are only a
few options available and a ListBox
wouldn't look nice on the
form.
Properties
Checked
(bool
) - If the value istrue
, theRadioButton
is checked.Appearance
- If set toButton
, theRadioButton
will be rendered as an ordinary button (i.e. theButton
). A checkedRadioButton
will look like a pressed button, others will look like unpressed ones.Image
- If we useAppearance = Button
, we can set an icon to the button easily.TextAlign
- Aligns the label text to the check button.ImageAlign
- Aligns the image.TextImageRelation
- Here we can set the relation between the text and the image. The text can be displayed over the picture, but it can also be above it, below it, etc.
Events
CheckedChanged
- Is triggered when theRadioButton
is checked or unchecked.
GroupBox
If some controls on the form are logically related, we can place them in a
container. The container is a control that can contain other controls inside. We
can simply put those controls into it in the designer. One of the containers in
.NET is GroupBox
. Try to create a GroupBox
and put
several controls in it. Then when you move it, you'll see those controls are
moving along with it.
Beside making the form more readable, inserting controls into containers has
other benefits as well. For example, we can hide the container (set
Visible
to false
) and it'll disappear along with its
contents. We can easily access the controls inside the container using its
Controls
collection and a loop. And last but not least, we can put
RadioButton
controls in it, of which only one can be selected at a
time. More RadioButton
controls can be checked at a time if they
are in different containers.
Properties
Text
- The group title.Controls
- The collection of all the components inside this group.
Events
No important events.
Panel
Panel
is another container control. It behaves the same as the
GroupBox
, except that its border isn't displayed by default. This
allows us to group form controls for inner purposes of the application, without
the user noticing it. A typical example is putting e.g. 20 TextBox
controls with different values into a Panel
, then iterating through
its Controls
collection and setting the Text
value to
each. Without the panel, getting to the TextBox
controls would be
more difficult, and perhaps we'd accidentally rewrite all the controls on the
form.
Properties
BorderStyle
- The visual style of the panel's border. We can setNone
,FixedSingle
andFixed3D
.Controls
- The collection of all controls inside the panel.
Events
No important events.
FlowLayoutPanel
FlowLayoutPanel
arranges form controls for us. While in the
normal panel we have to set their positions manually,
FlowLayoutPanel
doesn't use their positions, but displays the
controls as they go instead, from left to right, from top to bottom. This can
often be useful.
Properties
FlowDirection
- We can set in which direction the controls should be displayed. ThePanel
can display them both from right to left, or even vertically in both directions.BorderStyle
- The style of the border. We can setNone
,FixedSingle
, andFixed3D
.
Events
No important events.
PictureBox
PictureBox
is an image, more specifically an image control. It's
useful for improving the application design. It's also a common practice to not
assign the image to it, but draw something instead (e.g. a graph).
Properties
Image
- The image.SizeMode
- The display mode of the image. TheNormal
mode displays the image as it is,StretchImage
stretches it over the entirePictureBox
, regardless of the aspect ratio, theAutoSize
sets thePictureBox
size to the size of the image,CenterImage
centers the image, and finally,Zoom
resizes the image respecting the aspect ratio.
Events
Paint
- This event is triggered when the image is to be redrawn (that is, when the form asks for the image to be redrawn. This happens when the form is created or when another window is moved over the form). Inside the handler method, we usually draw our own graphics on the image's canvas, using the methods of theGraphics
property of thee
handler method parameter. We've already tried it several times during this course.
DateTimePicker
DateTimePicker
allows us to enter date and time with dignity.
It's certainly worth using this control for this purpose, it's easy for the user
and for us as well. We don't have to parse values from a TextBox
,
which spares us from handling input errors, and at the same time the operating
system solves entering the value for us. It provides us a nice calendar to enter
the date and time.
Properties
Format
- Selects the output format. We can choose between the long and short date formats, time format, and even our own format, but we won't deal with this one here.
Here's how the time picker looks like:
ShowUpDown
(bool
) - Specifies whether to display the UpDown (arrows that are particularly useful for time entering) or the DropDown calendar (which is perfect for date entering).Value
- The entered value.
Events
ValueChanged
- Occurs when the user changes the value.
MonthCalendar
The calendar allows us to display days of a month, to select them and render some as bold. We can use it as part of some agenda app.
Properties
BoldedDays
(DateTime[]
) - ADateTime
array of the days that will be rendered as bold. For example, we highlight the days we have an appointment on this way.MonthlyBoldedDays
(DateTime[]
) - Similar as the above, bold days can be specified regardless of the month, so they'll appear bold in every month.AnnuallyBoldedDays
- Again, similar as the above, but the days will appear bold every year, so they year doesn't matter.CalendarDimensions
- The number of months displayed vertically and horizontally.ShowToday
(bool
) - Allows us to turn off the bottom bar with today's date.ShowTodayCircle
(bool
) - Allows us to turn off the circle highlighting of today.ShowWeekNumbers
(bool
) - Displays week numbers.
Events
DateChanged
- Occurs when the date selection (or date range) has changed or when we move to the next / previous month using the arrows.DateSelected
- Occurs when the date selection (or date range) has changed.
That's all for today. In the next lesson, Windows Forms - Dialogs, we'll continue with our review of the .NET controls.