CHAPTER 2: BASIC WINDOWS
FORMS PROGRAMMING
APPLIED PROGRAMMING IN ENGINEERING
2.3 COMMON CONTROLS:
⚬ What is Controls:
⚬ In Windows Forms (WinForms), controls are UI components that allow
users to interact with an application.
⚬ Windows Forms provides a variety of built-in controls that allow developers
to build graphical user interfaces (GUIs) for desktop applications.
⚬ These controls can be categorized based on their functionality:
⚬ Basic controls: Button, Textbox, Label, PictureBox.
⚬ Input controls: CheckBox/RadioButton, ListBox/ComboBox.
⚬ Data controls: ListView, TreeView, DataGridView.
⚬ Container controls: Panel, GroupBox, TabControl.
⚬ Advanced controls: Menu, MessageBox, Dialog, Timer …
2. COMMON CONTROLS:
⚬ What is Controls:
2. COMMON CONTROLS:
• 2.1 Basic controls:
⚬ These are fundamental UI elements used to display text, receive input, or trigger
actions.
⚬ Some basic controls in Windows Form:
⚬ Label: Displays static text (e.g., instructions, titles).
⚬ Button: Executes an action when clicked.
⚬ Textbox: Allows users to enter or edit text.
⚬ PictureBox: Displays images (JPG, PNG, BMP, GIF).
2. COMMON CONTROLS:
• 2.1 Basic controls:
⚬ Label:
⚬ The Label control is used to display static text that
provides information or instructions to users.
⚬ Key Properties of Label:
⚬ Text: The content displayed in the label.
⚬ Font: Defines the font style and size.
⚬ ForeColor: Sets the text color.
⚬ AutoSize: Adjusts the label size automatically to fit
the text.
2. COMMON CONTROLS:
• 2.1 Basic controls:
⚬ Button:
⚬ The Button control triggers an action when clicked.
⚬ Key Properties of Button:
⚬ Text: The content displayed in the label.
⚬ Enabled: Determines if the button is clickable.
⚬ BackColor: Sets the background color.
⚬ Key Events of Button:
⚬ Click: Executes an action when the button is clicked.
2. COMMON CONTROLS:
• 2.1 Basic controls:
⚬ Textbox:
⚬ The TextBox control allows users to enter and edit text.
⚬ Key Properties of TextBox:
⚬ Text: The content displayed in the label.
⚬ Multiline: Allows multiple lines of text.
⚬ Readonly: Makes the textbox non-editable.
⚬ PasswordChar: Masks input for password field.
⚬ Key Events of TextBox:
⚬ TextChanged: Executes when input/delete text.
2. COMMON CONTROLS:
• 2.1 Basic controls:
⚬ PictureBox:
⚬ The PictureBox control is used to display images.
⚬ Key Properties of PictureBox:
⚬ Image: The image displayed in the control.
⚬ SizeMode: Adjusts image scaling (StretchImage,
Zoom, AutoSize).
⚬ ReadOnly: Makes the textbox non-editable.
⚬ BorderStyle: Adds a border around the image.
2. COMMON CONTROLS:
• 2.1 Basic controls:
⚬ Example:
⚬ Design a GUI as follows.
⚬ Program Functionality:
⚬ The user enters a height value (using the English system) in feet into
the corresponding Entry field.
⚬ When the Convert button is pressed:
⚬ Convert the value to inches and centimeters and display the results.
⚬ Conversion formulas:1 foot (feet) = 30.48 cm = 12 inches.
⚬ If the user does not enter a value, use a default value of 0.
⚬ If the entered value is less than 0, display the message "Please
enter a valid value" in a Label (placed below the button).
2. COMMON CONTROLS:
• 2.2 Input controls:
⚬ Input controls are UI elements in a Windows Forms application that allow users to
enter or modify data.
⚬ These controls capture user input in various formats, such as text, numbers,
selections, or dates..
⚬ Some Input controls in Windows Form:
⚬ CheckBox: Enables selection of true/false (checked/unchecked).
⚬ RadioButton: Allows selection of one option from multiple choices.
⚬ ListBox: Displays a list of items, allowing single or multiple selections.
⚬ ComboBox: Provides a dropdown list with selectable items.
⚬ RichTextBox: Similar to TextBox but supports formatted text, colors, and images.
2. COMMON CONTROLS:
• 2.2 Input controls :
⚬ CheckBox:
⚬ A CheckBox that allows users to make binary choices (Checked or
Unchecked). It is typically used for enabling/disabling options.
⚬ Key Properties of CheckBox:
⚬ Text: Sets the label text next to the checkbox..
⚬ Checked: Indicates whether the checkbox is selected (true) or not (false).
⚬ CheckState: Returns the state: Checked, Unchecked, or Indeterminate.
⚬ Key Events of CheckBox:
⚬ CheckedChanged: Triggered when the Checked property value changes.
2. COMMON CONTROLS:
• 2.2 Input controls :
⚬ RadioButton:
⚬ A RadioButton that allows users to select one option from a group of
mutually exclusive options. Unlike a CheckBox, where multiple selections
are allowed, only one RadioButton in a group can be selected at a time.
⚬ Key Properties of RadioButton:
⚬ Text: Sets the label text next to the radiobutton.
⚬ Checked: Indicates whether the radiobutton is selected or not.
⚬ Key Events of RadioButton:
⚬ CheckedChanged: Triggered when the Checked property value changes.
2. COMMON CONTROLS:
• 2.2 Input controls :
⚬ ListBox:
⚬ A ListBox displays a list of items from which the user can select one
or multiple options. It is commonly used for selecting from a list of
choices, such as file names, categories, or available options.
⚬ Key Properties of ListBox:
⚬ Items: Collection of items in the ListBox..
⚬ SelectedItem: The currently selected item.
⚬ SelectedIndex: The index of the selected item.
⚬ SelectionMode : Determines whether single or multiple selection
is allowed (One, MultiSimple, MultiExtended).
2. COMMON CONTROLS:
• 2.2 Input controls :
⚬ ListBox:
⚬ Key Events of ListBox:
⚬ SelectedIndexChanged: Occurs when the selected item changes..
2. COMMON CONTROLS:
• 2.2 Input controls :
⚬ ComboBox:
⚬ A ComboBox provides a drop-down list of selectable items. It allows users
to choose one option from a list while also allowing text input (if enabled).
⚬ Key Properties of ComboBox:
⚬ Items: Collection of items in the ComboBox.
⚬ SelectedItem: The currently selected item.
⚬ SelectedIndex: The index of the selected item.
⚬ Text: The text displayed in the ComboBox.
⚬ DropDownStyle: Controls the style (DropDown, DropDownList, Simple).
2. COMMON CONTROLS:
• 2.2 Input controls :
⚬ ComboBox:
⚬ Key Events of ComboBox:
⚬ SelectedIndexChanged: Occurs when the selected item changes..
2. COMMON CONTROLS:
• 2.2 Input controls:
⚬ Example:
⚬ Write a program to build program with the following GUI:
⚬ Program Functionality:
⚬ User enters values to the Entries (Name to Entry1 and Score to Entry2)
⚬ Click Button Add: adding values to Listboxs (Name to Listbox1 and Score to Listbox2).
⚬ If the user has not entered value to Entries (Entry1 or Entry2 is empty ): do not add values to Listbox.
⚬ If the user enters value outside the range [0,10] ): display message ‘Please enter the valid value’
⚬ Click Button Delete: if one of element of Listbox1 is selected: delete this element and the
corresponding element in Litsbox2. If not select element of Listbox1: do nothing.
APPLIED PROGRAMMING IN ENGINEERING
THANK YOU