0% found this document useful (0 votes)
64 views10 pages

Dot Net - Unit 4 - Final

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views10 pages

Dot Net - Unit 4 - Final

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Introducing Windows Forms

Unit 4 - Introducing windows forms

Overview of the System.Windows.Forms namespace:


• The System.Windows.Forms namespace is a part of the .NET Framework that provides a
library of classes and controls for creating graphical user interfaces (GUIs) in Windows
applications using C#.
• Form is the very first entry typically included in a window-based application.
• It hosts number of other controls for performing desired functions.
• At runtime, a form continuously waits for an event to occur, such as clicking of the mouse
or pressing of a key.
• When an event occurs, it creates the corresponding event handling code.
• Some of the key classes and controls in the System.Windows.Forms namespace include:
• Form: The Form class is the base class for all windows in a Windows Forms application. It
provides the basic functionality for creating, displaying, and managing windows, including
size, position, title, and events.
• Control: The Control class is the base class for all controls in a Windows Forms application.
It provides the core functionality and properties that are common to all controls, such as
size, position, text, font, color, and events.
• Button, Label, TextBox, CheckBox, RadioButton, etc: These classes provide the
functionality for creating common controls such as buttons, labels, text boxes, and
checkboxes.
• ListBox, ComboBox, ListView, TreeView: These classes provide the functionality for
creating list-based controls such as list boxes, combo boxes, list views, and tree views.
• PictureBox, ProgressBar, ToolTip, Timer: These classes provide the functionality for
creating specialized controls such as picture boxes, progress bars, tooltips, and timers.
• Dialog: The Dialog class provides a base class for creating dialog boxes.
• Overall, System.Windows.Forms is a powerful and versatile namespace that provides a
wide range of classes and controls for creating professional-quality Windows applications
in C#.
An anatomy of a Form:
• A Form in C# is a window that provides a user interface for an application.
• It is implemented as a System.Windows.Forms.Form class.
• Title Bar: The top bar of the form that displays the form's title and the minimize, maximize,
and close buttons.
• Control Box: The top-right corner of the form that contains the minimize, maximize, and
close buttons.
• Menu Bar: A horizontal bar that contains a list of menus and menu items.
• Toolbar: A strip of buttons that provide quick access to frequently used commands.
• Status Bar: A strip at the bottom of the form that displays status information, such as the
status of the current operation.
• Control: An object that provides a user interface element, such as a button, text box, or list
box.
• Scroll bar: A bar that allows the user to scroll through a list of items or a large amount of
text.
• Border: The outer edge of the form that surrounds the form's client area.

Ravi Naik, BCA Department, SRNMNC - Shivamogga Page 1


Introducing Windows Forms

• 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.

A Simple Form Program:


using System;
using System.Windows.Forms;
namespace SimpleForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello, World!");
}
}
}

Ravi Naik, BCA Department, SRNMNC - Shivamogga Page 2


Introducing Windows Forms

Function with Control Class


• In a Windows Forms application (WinForms) written in C#, a Control Class can be used to
control the flow of execution of the program and handle user input. The class can contain
methods, properties, and events that can be used to control the behavior of the forms,
buttons, and other controls in the application.
• For example, a Control Class in a WinForms application could contain methods for handling
button clicks, controlling the flow of a loop, or determining when to exit the program. The
class could also contain properties that store the current state of the application, such as
whether a form is open or closed.
• Events can also be handled in the Control Class. For example, if a button is clicked, an event
handler in the Control Class can be used to execute the appropriate code.

The Functionality of the Form Class, Component class, control class


• In a Windows Forms application written in C#, the Form Class, Component Class, and
Control Class all play different roles in the overall functionality of the program.
• Form Class: A Form Class represents a window in a Windows Forms application and is
used to create and display the user interface of the program. The Form Class is derived
from the System.Windows.Forms.Form class and contains properties and methods that are
specific to the form, such as the size, position, and visibility of the form.
• The Form Class is responsible for creating and managing the visual elements of the
program, such as buttons, text boxes, and labels. It also handles user input, such as mouse
clicks and keyboard events, and can be used to create dialog boxes, message boxes, and
other types of windows.
• The Form Class also provides a way to manage the layout of the controls on the form, such
as using layout managers to automatically adjust the position and size of controls based on
the size of the form.
• Additionally, the Form Class 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 form, and the Timer object, which can be used to create timed events.
• In summary, The Form Class is the primary class for creating and managing the user
interface of a Windows Forms application and it provides properties, methods and events
for creating, displaying and handling the window, its controls and its resources.
• Component Class: The Component Class is the base class for all components in a Windows
Forms application. It provides a base set of properties, methods, and events that can be
used by other classes in the program. The Component Class is derived from the
System.ComponentModel.Component class.
• The primary functionality of the Component Class is to provide a way to manage the
lifecycle of the components in the application. It includes methods such as Initialize,
Dispose, and a set of events that are raised at different stages of the component's lifecycle,
such as when the component is initialized or when it is being disposed.
• It also provides a way for the components to be identified and accessed in a consistent way,
regardless of their specific type. The Component Class includes properties such as Site and
Container, which can be used to identify and access the component's location within the
application.
• Additionally, the Component Class provides a way to manage the state of the component,

Ravi Naik, BCA Department, SRNMNC - Shivamogga Page 3


Introducing Windows Forms

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.

Categories of System.Windows.Form namespace

• 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

Ravi Naik, BCA Department, SRNMNC - Shivamogga Page 4


Introducing Windows Forms

File and Directory classes.


• The "Security" category contains classes for working with security features, such as
thePrincipal and PrincipalPermission classes.
• The "Web" category contains classes for working with web-related features, such as the
WebBrowser class.

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).

Ravi Naik, BCA Department, SRNMNC - Shivamogga Page 5


Introducing Windows Forms

• Here is an example of using a CheckBox control in a Windows Forms application:


• private void Form1_Load(object sender, EventArgs e)
{
CheckBox checkBox1 = new CheckBox();
CheckBox checkBox2 = new CheckBox();
checkBox1.Text = "Option 1";
checkBox2.Text = "Option 2";
checkBox1.Checked = true;
this.Controls.Add(checkBox1);
this.Controls.Add(checkBox2);
}
• We can also check the status of a CheckBox by using the Checked property, it returns true
if the box is checked and false otherwise.
if (checkBox1.Checked)
{
// our code here
}
3. Radio Buttons:
• The RadioButton control in Windows Forms is a control that allows the user to select one
option from a group of mutually exclusive options.
• It is implemented as a System.Windows.Forms.RadioButton class.
• To use the RadioButton control, we first need to add it to our form by dragging it from the
Toolbox onto the form.
• Once we have added the radio buttons to our form, we can set the properties of each radio
button such as Text, which sets the text displayed on the radio button, and Checked, which
sets the initial state of the radio button (true for checked, false for unchecked).
• Here is an example of using a RadioButton control in a Windows Forms application:
private void Form1_Load(object sender, EventArgs e)
{
RadioButton radioButton1 = new RadioButton();
RadioButton radioButton2 = new RadioButton();
radioButton1.Text = "Option 1";
radioButton2.Text = "Option 2";
radioButton1.Checked = true;
this.Controls.Add(radioButton1);
this.Controls.Add(radioButton2);
}

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

Ravi Naik, BCA Department, SRNMNC - Shivamogga Page 6


Introducing Windows Forms

Toolbox onto the form.


• Once the GroupBox control is added to the form, we can set its properties such as Text, which
sets the caption text displayed on the group box, and Control.ForeColor and
Control.BackColor properties that set the color of the text and background of the group box
respectively.
• Here is an example of using a GroupBox control in a Windows Forms application:
private void Form1_Load(object sender, EventArgs e)
{
GroupBox groupBox1 = new GroupBox();
groupBox1.Text = "Group Box";
groupBox1.Controls.Add(new TextBox());
groupBox1.Controls.Add(new Button());
this.Controls.Add(groupBox1);
}

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)

Ravi Naik, BCA Department, SRNMNC - Shivamogga Page 7


Introducing Windows Forms

{
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:

private void Form1_Load(object sender, EventArgs e)


{
Timer tmr = new Timer();
tmr.Interval = 1000;
tmr.Tick += new EventHandler(tmr_Tick);
tmr.Enabled = true;
}
private void tmr_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
}

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");

Ravi Naik, BCA Department, SRNMNC - Shivamogga Page 8


Introducing Windows Forms

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:

private void Form1_Load(object sender, EventArgs e)


{
HScrollBar hsb = new HScrollBar();
hsb.Maximum = 100;
hsb.Minimum = 0;
hsb.Scroll += new ScrollEventHandler(hsb_Scroll);
this.Controls.Add(hsb);
}

10. Progressbar control


• The ProgressBar control in Windows Forms is a control that allows us to display the
progress of a task in our application. It is implemented as a
System.Windows.Forms.ProgressBar class.
• To use the ProgressBar 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 ProgressBar control is added to the form, we can set its properties such as
Maximum, which sets the maximum value of the progressbar, Minimum, which sets the
minimum value of the progressbar and Value, which sets the current value of the
progressbar.
• Here is an example of using a ProgressBar control in a Windows Forms application:
private void Form1_Load(object sender, EventArgs e)
{
ProgressBar pb = new ProgressBar();
pb.Maximum = 100;
pb.Minimum = 0;
pb.Style = ProgressBarStyle.Continuous;
this.Controls.Add(pb);
}

Ravi Naik, BCA Department, SRNMNC - Shivamogga Page 9


Introducing Windows Forms

11. Tool Tip:


• The ToolTip control in Windows Forms is a control that allows us to display a small pop-
up window with a message when the user hovers over a control on our form. It is
implemented as a System.Windows.Forms.ToolTip class.
• To use the ToolTip control, we first need to add it to our form by dragging it from the
Toolbox onto the form.
• Once the ToolTip control is added to the form, we can set its properties such as
AutomaticDelay, which sets the delay before the tooltip is displayed, InitialDelay, which
sets the delay before the tooltip is displayed for the first time and ShowAlways, which
determines whether the tooltip should be displayed even when the form is not active.
• Here is an example of using a ToolTip control in a Windows Forms application:
private void Form1_Load(object sender, EventArgs e)
{
ToolTip toolTip1 = new ToolTip();
toolTip1.ShowAlways = true;
toolTip1.SetToolTip(button1, "Click to perform action");
toolTip1.SetToolTip(textBox1, "Enter text here");
}
• We can also set the ToolTip control for multiple controls by calling the SetToolTip method
for each control we want to add the tooltip to.

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.

Ravi Naik, BCA Department, SRNMNC - Shivamogga Page 10

You might also like