Dot Net - Unit 4 - Final
Dot Net - Unit 4 - Final
• Client Area: The inner portion of the form that is used to display controls and other user
interface elements.
• A Form class has various properties and methods that allows us to customize the
appearance, behavior and functionality of our form. Some of the most commonly used
properties include Text, which sets the text displayed in the title bar, BackColor and
ForeColor, which sets the background and text color of the form, and Size, which sets the
size of the form.
• We can also handle the events of the form, such as the Load event, which is raised when the
form is first displayed, and the Closing event, which is raised when the form is closing.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// our code here
}
private void Form1_Closing(object sender, FormClosingEventArgs e)
{
// our code here
}
}
• We can also add controls to the form in order to create the desired interface for our
application, such as buttons, text boxes, labels, etc., we can add them by dragging from the
toolbox or programmatically.
such as whether it is enabled or disabled, and a way to handle errors that may occur during
the component's lifecycle.
• In summary, The Component Class provides a base set of functionalities for all components
in a Windows Forms application. It includes methods for managing the lifecycle of the
component, properties for identifying and accessing the component, and events for
handling errors and managing the state of the component.
• Control Class: A Control Class is used to control the behavior of the forms, buttons, and
other controls in a Windows Forms application. It contains methods, properties, and events
that can be used to control the flow of the program and handle user input. It is derived from
the System.Windows.Forms.Control class.
• The Control Class provides a way to handle user input, such as mouse clicks and keyboard
events. It also provides properties and methods for managing the layout and appearance
of the controls, such as setting the size, position, and background color of the control.
• It also provides a way to handle the events of the control, such as the Click event of a button,
or the TextChanged event of a TextBox. These events can be handled in the control class to
execute the appropriate code in response to the user's actions.
• The Control Class also provides a way to access and manipulate the underlying resources
of the program, such as the Graphics object, which can be used to draw shapes and text on
the control.
• Additionally, the Control Class provides a way to manage the state of the control, such as
whether it is enabled or disabled, and provides a way to handle errors that may occur
during the control's lifecycle.
• In summary, The Control Class is used to control the behavior of the forms, buttons, and
other controls in a Windows Forms application. It provides properties, methods and events
for managing the layout, appearance, events, resources and state of the control, and for
handling errors.
• The "Controls" category contains classes that represent various controls, such as buttons,
text boxes, and labels, that can be used to build a user interface.
• The "Forms" category contains classes that represent windows, dialog boxes, and other
types of forms that can be used to build a user interface.
• The "Layout" category contains classes that are used to manage the layout of controls on a
form, such as the TableLayoutPanel and FlowLayoutPanel classes.
• The "Data" category contains classes for working with data, such as the BindingSource
class, which can be used to bind controls to data sources, and the DataGridView class, which
can be used to display data in a tabular format.
• The "Drawing" category contains classes for drawing shapes and images on a form, such
as the Pen and Brush classes.
• The "ComponentModel" category contains classes for implementing various design-time
features, such as the Component class and the DesignMode property.
• The "Text" category contains classes for working with text, such as the Font and
StringFormat classes.
• The "IO" category contains classes for working with input/output operations, such as the
Programming with windows forms controls - Working with the Basic Controls
The System.Windows.Forms namespace defines various basic controls
1. Buttons:
• The Button control in Windows Forms is a control that allows the user to initiate an action
by clicking on it. It is implemented as a System.Windows.Forms.Button class.
• To use the Button control, we first need to add it to our form by dragging it from the
Toolbox onto the form.
• Once we have added the button to our form, we can set the properties of the button such
as Text, which sets the text displayed on the button, and Image, which sets an image to be
displayed on the button.
• Here is an example of using a Button control in a Windows Forms application:
private void Form1_Load(object sender, EventArgs e)
{
Button button1 = new Button();
button1.Text = "Click Me!";
button1.Image = Image.FromFile("image.jpg");
this.Controls.Add(button1);
}
• We can also handle the button click event by attaching an event handler to the Click event.
The event handler method will be called when the user clicks on the button.
private void button1_Click(object sender, EventArgs e)
{
// our code here
}
• Additionally, the button has Enabled property, it allows to enable or disable the button, by
default it's set to true, when it's set to false, the button will be disabled and the user can't
interact with it.
button1.Enabled = false;
2. Checkboxes:
• Checkboxes allow the user to make multiple selections from anumber of options.
• The CheckBox control in Windows Forms is a control that allows the user to select one or
more options from a list of options. It is implemented as a
System.Windows.Forms.CheckBox class.
• To use the CheckBox control, we first need to add it to our form by dragging it from the
Toolbox onto the form.
• Once we have added the checkboxes to our form, we can set the properties of each
checkbox such as Text, which sets the text displayed on the checkbox, and Checked, which
sets the initial state of the checkbox (true for checked, false for unchecked).
4. Group Box
• In windows form, group box is a container which contains multiple controls on it and the
controls are related to each other.
• The GroupBox control in Windows Forms is a container control that allows us to group a set
of controls together and visually separate them from other controls on the form. It is
implemented as a System.Windows.Forms.GroupBox class.
• To use the GroupBox control, we first need to add it to our form by dragging it from the
5. List Boxes:
• The ListBox control in Windows Forms is a control that allows us to display a list of items
that the user can select. It is implemented as a System.Windows.Forms.ListBox class.
• To use the ListBox control, we first need to add it to our form by dragging it from the
Toolbox onto the form.
• Once the ListBox control is added to the form, we can set its properties such as
SelectionMode, which sets the selection mode (e.g. single, multiple).
• Here is an example of using a ListBox control in a Windows Forms application:
private void Form1_Load(object sender, EventArgs e)
{
ListBox listBox1 = new ListBox();
listBox1.Items.Add("Item 1");
listBox1.Items.Add("Item 2");
listBox1.Items.Add("Item 3");
listBox1.SelectionMode = SelectionMode.MultiExtended;
this.Controls.Add(listBox1);
}
6. Calendar control:
• The Calendar control in Windows Forms is a control that allows us to display a calendar and
select a date. It is implemented as a System.Windows.Forms.MonthCalendar class.
• To use the Calendar control, we first need to add it to our form by dragging it from the
Toolbox onto the form.
• Once the Calendar control is added to the form, we can set its properties such as
FirstDayOfWeek, which sets the first day of the week, ShowToday, which determines
whether the "Today" button is displayed, ShowTodayCircle, which determines whether the
circle around the current date is displayed, and MaxSelectionCount, which sets the maximum
number of days that can be selected.
• Here is an example of using a Calendar control in a Windows Forms application:
private void Form1_Load(object sender, EventArgs e)
{
MonthCalendar calendar1 = new MonthCalendar();
calendar1.FirstDayOfWeek = Day.Monday;
calendar1.ShowToday = true;
calendar1.ShowTodayCircle = true;
calendar1.MaxSelectionCount = 3;
this.Controls.Add(calendar1);
}
7. Timer control
• The Timer control in Windows Forms is a control that allows us to execute a specific code
block at a specified interval of time repeatedly.
• It is implemented as a System.Windows.Forms.Timer class.
• To use the Timer control, we first need to add it to our form by dragging it from the Toolbox
onto the form, or by creating it programmatically.
• Once the Timer control is added to the form, we can set its properties such as Interval, which
determines the time interval between each tick of the timer, and Enabled, which starts or
stops the timer.
• Here is an example of using a Timer control in a Windows Forms application:
8. Picture control
• The PictureBox control in Windows Forms is a control that allows us to display images on
a form. It is implemented as a System.Windows.Forms.PictureBox class.
• To use the PictureBox control, we first need to add it to our form by dragging it from the
Toolbox onto the form, or by creating it programmatically.
• Once the PictureBox control is added to the form, we can set its properties such as Image,
which sets the image that will be displayed in the control, and SizeMode, which determines
how the image will be displayed within the control.
• Here is an example of using a PictureBox control in a Windows Forms application:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox picBox = new PictureBox();
picBox.Image = Image.FromFile("C:\\example.jpg");
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picBox);
}
9. Scrollbar control
• The ScrollBar control in Windows Forms is a control that allows us to add scroll
functionality to our forms. It is implemented as a System.Windows.Forms.HScrollBar or
System.Windows.Forms.VScrollBar class, depending on whether we want to create a
horizontal or a vertical scrollbar.
• To use the ScrollBar control, we first need to add it to our form by dragging it from the
Toolbox onto the form, or by creating it programmatically.
• Once the ScrollBar control is added to the form, we can set its properties such as Maximum,
which sets the maximum value of the scrollbar, Minimum, which sets the minimum value
of the scrollbar and Value, which sets the current value of the scrollbar.
• Here is an example of using a ScrollBar control in a Windows Forms application:
Developing an UI
Follow the below steps to develop an UI in windows form C# :
• Create a new Windows Forms project in Visual Studio: Start by creating a new project in
Visual Studio, and selecting the Windows Forms App template. This will create a basic
template for your UI, with a default form already set up.
• Design the layout of your UI: Use the designer view in Visual Studio to design the layout of
our UI. You can add controls such as buttons, labels, text boxes, and list boxes to the form,
and arrange them.
• Add functionality to your controls: Use the Properties window in Visual Studio to set
properties for your controls, such as their text, size, and color. Also add event handlers for
the controls, such as the Click event for a button, to handle user input and execute the
appropriate code.
• Run the UI: Using start debug option in Visual Studio we can Run the built UI.
In summary, developing an UI in windows form C# consist of creating a new project, designing
the layout, adding functionality to controls, creating and using custom classes, testing and
debugging, and deploying the application to the target platform.