C#.net-unit-4
C#.net-unit-4
• Here the solution is like a container which contains the projects and files that may
be required by the program.
• After that following window will display which will be divided into three parts as
follows:
1. Editor Window or Main Window: Here, you will work with forms
and code editing. You can notice the layout of form which is now
blank. You will double click the form then it will open the code for
that.
1
2. Solution Explorer Window: It is used to navigate between all items in
solution. For example, if you will select a file form this window then
particular information will be display in the property window.
3. Properties Window: This window is used to change the different
properties of the selected item in the Solution Explorer. Also, you can
change the properties of components or controls that you will add to
the forms.
• Now to add the controls to your WinForms application go to Toolbox tab
present in the extreme left side of Visual Studio. Here, you can see a list of
controls. To access the most commonly used controls go to Common Controls
present in Toolbox tab.
• Now drag and drop the controls that you needed on created Form. For example, if
you can add TextBox, ListBox, Button etc. as shown below. By clicking on the
particular dropped control you can see and change its properties present in the
right most corner of Visual Studio.
2
In the above image, you can see the TextBox is selected and its properties like
TextAlign, MaxLength etc. are opened in right most corner. You can change its
properties’ values as per the application need. The code of controls will be
automatically added in the background. You can check the Form1.Designer.cs file
present in the Solution Explorer Window.
• To run the program you can use an F5 key or Play button present in the toolbar of
Visual Studio. To stop the program you can use pause button present in the
ToolBar. You can also run the program by going to Debug->Start Debugging
menu in the menubar.
C# Windows Forms is a graphical user interface (GUI) framework that enables developers to
create desktop applications for the Windows operating system. Windows Forms applications
are created using the C# programming language and the .NET framework. They are built by
3
dragging and dropping controls such as buttons, text boxes, labels, and other user interface
elements onto a form.
1. The Windows Forms framework provides a rich set of controls that developers can
use to build applications with. These controls are designed to provide a consistent
and familiar user interface for Windows users. Developers can customize the
appearance and behavior of these controls by setting various properties and
handling events.
2. To create a Windows Forms application in C#, you can use Microsoft Visual
Studio, which is an integrated development environment (IDE) that provides a
visual designer to create and layout the user interface elements. The visual
designer is a drag-and-drop interface for building your UI, and you can easily
configure each control’s properties through a user-friendly interface.
3. In addition to the visual designer, Visual Studio also provides a code editor that
enables developers to write the C# code for the application’s logic. Developers
can handle events and perform tasks such as data validation, data manipulation,
and business logic implementation.
4. Windows Forms applications are versatile and can be used to create various types
of applications such as data entry, management, and reporting applications, as well
as games and multimedia applications.
4
Overview of the system. windows. Forms Namespaces
These namespaces collectively provide the tools and classes necessary for creating,
designing, and interacting with the UI in Windows Forms applications. When you're building
a desktop application using Windows Forms in C#, you'll frequently use types from these
namespaces to design the user interface, handle events, and manage the overall application
flow.
5
These are just a few examples, and there are many more control classes available in both
Windows Forms and WPF. Depending on your application's requirements, you choose the
appropriate controls to build your user interface.
When working with controls, you typically instantiate them, set properties to configure their
appearance and behaviour, and add them to a container (such as a form or panel). Event
handlers are often used to respond to user interactions with these controls.
6
MessageBox.Show("Button Clicked!"); }
6. Properties and Methods:
The form class exposes various properties and methods that allow you to customize its
behavior and appearance. Common properties include Text (for setting the form's title) and
BackColor (for setting the background color).
Example
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent(); // Set
form properties this.Text = "My
Windows Form";
this.BackColor = Color.LightGray;
}}
7. Layout:
You can set the layout of controls on the form using properties like Dock, Anchor, and by
using layout panels (e.g., FlowLayoutPanel, TableLayoutPanel).
8. Menu and Toolbar:
Forms can have menus and toolbars for navigation and interaction. You can add menus and
toolbars through the form designer or programmatically.
9. Closing Event:
You can handle the FormClosing event to perform actions before the form is closed. Example
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
// Perform cleanup or additional actions before closing }
10. Run the Application:
Typically, a Windows Forms application starts by creating an instance of the main form and
running it using the Application.Run method. Example
static class Program
{
namespace SimpleFormExample
{
public partial class MainForm : Form
7
{
private Button myButton;
private TextBox myTextBox;
public MainForm()
{
InitializeComponent();
InitializeUI();
}
// TextBox
myTextBox = new TextBox();
// Set layout
FlowLayoutPanel flowLayoutPanel = new FlowLayoutPanel();
flowLayoutPanel.Controls.Add(myButton);
flowLayoutPanel.Controls.Add(myTextBox);
8
Application.Run(new MainForm());
}
}
}
In this example, we create a simple Windows Form with a button and a textbox. When the
button is clicked, a message box appears with a greeting that includes the text entered in the
textbox.
The Functionality of the Form Class
In C# with Windows Forms, the Form class is a fundamental class that represents a window
or dialog box in a desktop application. The Form class provides a wide range of properties,
methods, and events that enable you to customize and control the behavior and appearance of
your application's user interface. Here are some key functionalities provided by the Form
class:
Properties:
Text:
Allows you to set or get the text displayed in the title bar of the form.
Example
this.Text = "My Form";
WindowState:
Gets or sets a value indicating whether the form is minimized, maximized, or normal.
this.WindowState = FormWindowState.Maximized;
TopMost:
Gets or sets a value indicating whether the form should be displayed as a topmost form.
this.TopMost = true;
Methods:
Show() and ShowDialog():
Displays the form on the screen. ShowDialog is used to display the form as a modal dialog.
this.Show();
9
// or
this.ShowDialog();
Close(): Closes
the form.
this.Close();
Events:
1.Load:
Occurs before the form is closed. Can be used to prompt the user for confirmation
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
// Check if the user wants to save changes before closing, for example }
3.Activated and Deactivate:
Occur when the form is activated (comes to the foreground) or deactivated (loses focus).
private void MainForm_Activated(object sender, EventArgs e) {
// Form is activated
}
CONTROL CLASS
Here's a brief overview of control classes in both Windows Forms and WPF:
10
1. Button:
Represents a standard button.
Example
Button myButton = new Button(); 2.TextBox:
Represents a single-line text box for user input.
Ex:
TextBox myTextBox = new TextBox();
3.Label:
Displays text and allows for simple formatting.
Ex;
Label myLabel = new Label();
4.ComboBox:
Represents a drop-down list.
Ex:
ComboBox myComboBox = new ComboBox();
5.DataGridView:
Displays data in a tabular format.
Ex:
DataGridView myDataGridView = new DataGridView();
6.PictureBox:
Displays an image.
Ex:
PictureBox myPictureBox = new PictureBox();
These are just a few examples, and there are many more control classes available in both
Windows Forms and WPF. Depending on your application's requirements, you choose the
appropriate controls to build your user interface.
When working with controls, you typically instantiate them, set properties to configure their
appearance and behavior, and add them to a container (such as a form or panel). Event
handlers are often used to respond to user interactions with these controls.
11
Programming with windows forms controls: Working with Button types, Check Boxes,
Radio Buttons, Group Boxes, List Boxes, Calendar control, Timer, picture box, group box,
scroll bar, Progress bar, assigning tool tips for controls. Developing an UI.
A Button is an essential part of an application, or software, or webpage. It allows the user to
interact with the application or software. For example, if a user wants to exit from the current
application so, he/she click the exit button which closes the application. It can be used to
perform many actions like to submit, upload, download, etc. according to the requirement of
your program. It can be available with different shape, size, color, etc. and you can reuse
them in different applications. In .NET Framework, Button class is used to represent
windows button control and it is inherited from ButtonBase class. It is defined under
System.Windows.Forms namespace.
Property Description
Using BackColor property you can set the background color of the
button.
BackColor
Using AutoEllipsis property you can set a value which shows that
whether the ellipsis character (…) appears at the right edge of the
control which denotes that the button text extends beyond the
specified length of the button.
AutoEllipsis
Using AutoSize property you can set a value which shows whether
the button resizes based on its contents.
AutoSize
Using Enabled property you can set a value which shows whether
the button can respond to user interaction.
Enabled
12
Using Events property you can get the list of the event handlers that
are applied on the given button.
Events
Font Using Font property you can set the font of the button.
FontHeight Using FontHeight property you can set the height of the font.
Using ForeColor property you can set the foreground color of the
button.
ForeColor
Height Using Height property you can set the height of the button.
Image Using Image property you can set the image on the button.
Margin Using Margin property you can set the margin between controls.
Name Using Name property you can set the name of the button.
Padding Using Padding property you can set the padding within the button.
Using Visible property you can set a value which shows whether the
button and all its child buttons are displayed.
Visible
13
Event Description
This event occur when the user performs double click on the
DoubleClick button.
Leave This event occur when the input focus leaves the control.
MouseClick This event occur when you click the mouse pointer on the button.
This event occur when you double click the mouse pointer on the
MouseDoubleClick button.
MouseHover This event occur when the mouse pointer placed on the button.
MouseLeave This event occur when the mouse pointer leaves the button.
Event Description
CheckedChanged This event occur when the value of the Checked property changes.
CheckStateChanged This event occur when the value of the CheckState property
changes.
14
DoubleClick This event occur when the user double-clicks the CheckBox control.
Leave This event occur when the input focus leaves the control.
MouseClick This event occur when the control is clicked by the mouse.
MouseDoubleClick This event occur when the user double-clicks the CheckBox control.
MouseHover This event occur when the mouse pointer rests on the control.
control.
BorderStyle This property indicates the border style for the control.
LISTBOX
Constructor
Constructor Description
ListBox() This Constructors is used to initialize a new instance of the ListBox class.
Properties
Property Description
This property is used to get or set a value that indicates whether the
AutoSize
control resizes based on its contents.
BackColor This property is used to get or set the background color for the
15
Font This property is used to get or set the font of the text displayed by the
control.
ForeColor This property is used to get or set the foreground color of the control.
Height This property is used to get or set the height of the control.
Location This property is used to get or set the coordinates of the upper-left
corner of the ListBox control relative to the upper-left corner of its
form.
Name This property is used to get or set the name of the control.
TabStop This property is used to get or set a value that shows whether the user
can press the TAB key to provide the focus to the ListBox.
Size This property is used to get or set the height and width of the control.
Text This property is used to get or set the text to be displayed in the
RichTextBox control.
Visible This property is used to get or set a value indicating whether the control
and all its child controls are displayed.
Width This property is used to get or set the width of the control.
16
ColumnWidth This property is used to get or set the width of columns in a
multicolumn ListBox.
HorizontalExtent This property is used to get or set the width by which the horizontal
scroll bar of a ListBox can scroll.
ItemHeight This property is used to get or set the height of an item in the ListBox.
PreferredHeight This property is used to get the combined height of all items in the
ListBox.
SelectedIndex This property is used to get or set the zero-based index of the currently
selected item in a ListBox.
SelectedItem This property is used to get or set the currently selected item in the
ListBox.
SelectedIndices This property is used to get a collection that contains the zero-based
indexes of all currently selected items in the ListBox.
Sorted This property is used to get or set a value indicating whether the items
in the ListBox are sorted alphabetically.
17
TopIndex This property is used to get or set the index of the first visible item in
the ListBox.
18