Unit - 5: Windows Forms and Controls in Details

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 107

2160711

Dot Net Technology

Unit - 5
Windows Forms and
Controls in details
Prof. Naimish R. Vadodariya
8866215253
[email protected]
Outline
 Introduction to Windows Forms
 How to Create Windows Forms?
 Windows Forms Properties & Events
 Windows Forms Controls
 Menus
 Dialogs
 Tooltip

Unit: 5 – Windows Forms and Controls in details 2 Darshan Institute of Engineering & Technology
Introduction to Windows Forms
 Windows Forms provides a graphical user interface (GUI) for
building windows client applications.
 This GUI represents a part of Microsoft.NET Framework.
 You can create Windows Forms Applications in any language that
is supported by CLR.
 Prior to Windows Forms, Programmers had to perform the
difficult and inconvenient task of manually writing thousands of
lines of code for designing an application.
 Windows Forms provides an easy and ideal way for creating
applications.

Unit: 5 – Windows Forms and Controls in details 3 Darshan Institute of Engineering & Technology
Windows Forms Cont..
 It acts as container that allow you to add different types of
controls, such as Button, Label and so many others.
 You can also use the Windows Forms for creating message boxes
and dialog boxes, displaying messages, and accepting user data.
 It allows you to design multiple forms to perform various tasks by
using different controls with their properties and methods.
 Namespace : System.Windows.Forms.
 Examples of a Windows Forms Applications
• Notepad
• Calculator

Unit: 5 – Windows Forms and Controls in details 4 Darshan Institute of Engineering & Technology
How to Create Windows Forms ?
 Step : 1
• Open Visual Studio, Go to File menu NewProject.
• After Clicking on Project, new project window will appear.

Unit: 5 – Windows Forms and Controls in details 5 Darshan Institute of Engineering & Technology
Create Windows Forms Cont..
 Select Language, Project Type, Set Location & Give Project Name.

2.2
2.1

2.3
2.4

2.5

Unit: 5 – Windows Forms and Controls in details 6 Darshan Institute of Engineering & Technology
Create Windows Forms Cont..

5
Unit: 5 – Windows Forms and Controls in details 7 Darshan Institute of Engineering & Technology
Windows Forms Properties
Property Description
Name This is the actual name of the form.
Text The text, which will appear at the title bar of the form.
Width This is the width of the form in pixel.
Height This is the height of the form in pixel.
Font This property specify font type, style, size.
BackColor Sets the form background color.
AcceptButton The button that's automatically activated when you press Enter, no matter
which control has the focus at the time. Usually the OK button on a form is
set as AcceptButton for a form.
CancelButton The button that's automatically activated when you hit the Esc key.
Form remains in default size as you show in design, but you can change its
WindowState
state by minimized, maximized & normal.
This property enables you to set the starting position of the form when it
StartPosition
is displayed at run time.

Unit: 5 – Windows Forms and Controls in details 8 Darshan Institute of Engineering & Technology
Windows Forms Events
Events Description
Activated Occurs when the form is activated in code or by the user.
Click Occurs when the form is clicked.
Closed Occurs before the form is closed.
Closing Occurs when the form is closing.
Load Occurs before a form is displayed for the first time.
MouseHover Occurs when the mouse pointer rests on the form.
DoubleClick Occurs when the form control is double-clicked.
KeyDown Occurs when a key is pressed while the form has focus.
Shown Occurs whenever the form is first displayed.

Unit: 5 – Windows Forms and Controls in details 9 Darshan Institute of Engineering & Technology
Windows Forms Basic Controls
 A Windows forms application runs on the desktop computer.
 Normally, GUI is a combination of controls which can used by user.
 Some of the controls are :
Basic Controls List Controls Data Controls
Label ListBox DataGridView
TextBox ComboBox
Button CheckedListBox
RadioButton
CheckBox
PictureBox

Unit: 5 – Windows Forms and Controls in details 10 Darshan Institute of Engineering & Technology
Common Properties of Controls
Property Description
The name of the control. This name can be used to reference the control in
Name
code.
Text Gets or sets the text associated with control.
Visible Specifies whether or not the control is visible at runtime.
Width The width of the control in pixel.
Height The Height of the control in pixel.
BackColor The background color of a control.
ForeColor The foreground color of the control.
Font Gets or sets the font of the text displayed by the control.

Unit: 5 – Windows Forms and Controls in details 11 Darshan Institute of Engineering & Technology
Common Events of Controls
Event Description
Occurs when a control is clicked. In some cases, this event will also occur
Click
when a user presses Enter.
Occurs when a control is double-clicked. Handling the Click event on some
DoubleClick controls, such as the Button control will mean that the DoubleClick event
can never be called.
Occurs when a drag-and-drop operation is completed, in other words,
DragDrop when an object has been dragged over the control, and the user releases
the mouse button.
Occurs when a key becomes pressed while the control has focus. This event
KeyDown
always occurs before KeyPress and KeyUp.
Occurs when a key becomes pressed, while a control has focus. This event
KeyPress
always occurs after KeyDown and before KeyUp.
Occurs when a control receives focus. Do not use this event to perform
GotFocus
validation of controls. Use Validating and Validated instead.
Occurs when a control looses focus. Do not use this event to perform
LostFocus
validation of controls. Use Validating and Validated instead.

Unit: 5 – Windows Forms and Controls in details 12 Darshan Institute of Engineering & Technology
Label Control
 Label Control is generally used to display the text that is not
supposed to be changed by user.
 It is used as instruction to user that which control is used for what
purpose.
 Namespace : System.Windows.Forms.Label

Unit: 5 – Windows Forms and Controls in details 13 Darshan Institute of Engineering & Technology
Properties of Label Control
Property Description
TextAlign Gets or sets how text is aligned in a Label control.
Visible Gets or sets a value indicating whether the control and all its child controls
are displayed.
Autosize Gets or sets a value specifying if the label control should be automatically
resized to display all its contents.

Unit: 5 – Windows Forms and Controls in details 14 Darshan Institute of Engineering & Technology
Methods of Label Control
Method Description
Focus() Sets input focus to the control.
ResetText() Resets the Text property to its default value.
ResetFont() Resets the Font property to its default value.
ResetForeColor() Resets the ForeColor property to its default value.
Show() Displays the control to the user.

Unit: 5 – Windows Forms and Controls in details 15 Darshan Institute of Engineering & Technology
Events of Label Control
Event Description
TextChanged Occurs when the Text property value changes.
Click Occurs when user clicks the label.
Leave Occurs when the input focus leaves the label.
LostFocus Occurs when the control loses focus.

Unit: 5 – Windows Forms and Controls in details 16 Darshan Institute of Engineering & Technology
TextBox Control
 The TextBox control is a windows forms control that lets users
enter text on a windows form at runtime.
 By default, a TextBox control accepts only a single line of text.
 Namespace : System.Windows.Forms.TextBox

Unit: 5 – Windows Forms and Controls in details 17 Darshan Institute of Engineering & Technology
Properties of TextBox Control
Property Description
TextLength Gets the length of text in the TextBox control.
PasswordChar Gets or sets the character used to mask characters of a password in a
single-line TextBox control.
Multiline Gets or sets a value indicating whether this is a multiline TextBox control.
ReadOnly Gets or sets a value indicating whether text in the text box is read-only.
WordWrap Indicates whether a multiline text box control automatically wraps words
to the beginning of the next line when necessary.

Unit: 5 – Windows Forms and Controls in details 18 Darshan Institute of Engineering & Technology
Methods of TextBox Control
Method Description
Clear() Clears all text from the text box control.
Copy() Copies the current selection in the text box to the Clipboard.
Cut() Moves the current selection in the text box to the Clipboard.
DeselectAll() Specifies that the value of the SelectionLength property is zero so that no
characters are selected in the control.
SelectAll() Selects all text in the text box.
Focus() Sets input focus to the control.
ResetText() Resets the Text property to its default value.

Unit: 5 – Windows Forms and Controls in details 19 Darshan Institute of Engineering & Technology
Events of TextBox Control
Event Description
TextChanged Occurs when the text in the textbox changes.
TextAlignChanged Occurs when the TextAlign property value changes.

Unit: 5 – Windows Forms and Controls in details 20 Darshan Institute of Engineering & Technology
Button Control
 A Button Control accepts clicks.
 In Windows Forms we use a Button control, that accepts click and
performs other actions on the user interface.
 Example
• When you press a close button, the form will be close.
 Namespace : System.Windows.Forms.Button

Unit: 5 – Windows Forms and Controls in details 21 Darshan Institute of Engineering & Technology
Properties of Button Control
Property Description
BackColor Gets or sets the background color of the button control.
Image Gets or sets the image that is displayed on a button control.
FlatStyle Gets or sets the flat style appearance of the button control.

Unit: 5 – Windows Forms and Controls in details 22 Darshan Institute of Engineering & Technology
Methods of Button Control
Method Description
Focus() Sets input focus to the control.
ResetBackColor() Resets the BackColor property to its default value.
ResetFont() Resets the Font property to its default value.
ResetText() Resets the Text property to its default value.
Select() Activates the control.
Show() Displays the control to the user.

Unit: 5 – Windows Forms and Controls in details 23 Darshan Institute of Engineering & Technology
Events of Button Control
Event Description
Click Occurs when user clicks the Button.
Validated Occurs when the button control is finished validating.
GotFocus Occurs when the button control receives focus.
TextChanged Occurs when the Text property value changes.
FontChange Occurs when font is changed.

Unit: 5 – Windows Forms and Controls in details 24 Darshan Institute of Engineering & Technology
RadioButton Control
 RadioButton Control enables the user to select a single option
from a group of choices when paired with other RadioButton
controls.
 When a user clicks on a radio button, it becomes checked, and all
other radio buttons with same group become unchecked.
 The RadioButton control can display text, an Image, or both.
 Namespace : System.Windows.Forms.RadioButton

Unit: 5 – Windows Forms and Controls in details 25 Darshan Institute of Engineering & Technology
Properties of RadioButton Control
Property Description
Checked Gets or sets a value indicating whether the radio button is checked.
CheckAlign Gets or sets the location of the check box portion of the radio button
control.
Image Gets or sets the image that is displayed on a radio button control.
Font Gets or sets the font of the text displayed by radio button control.

Unit: 5 – Windows Forms and Controls in details 26 Darshan Institute of Engineering & Technology
Methods of RadioButton Control
Method Description
Focus() Sets input focus to the control.
ResetBackColor() Resets the BackColor property to its default value.
ResetFont() Resets the Font property to its default value.
ResetText() Resets the Text property to its default value.
Select() Activates the control.
Show() Displays the control to the user.

Unit: 5 – Windows Forms and Controls in details 27 Darshan Institute of Engineering & Technology
Events of RadioButton Control
Event Description
CheckedChanged Occurs when the value of the checked property of the radio button
control is changed.
AppearanceChanged Occurs when the value of the Appearance property of the radio
Button control is changed.

Unit: 5 – Windows Forms and Controls in details 28 Darshan Institute of Engineering & Technology
CheckBox Control
 A CheckBox control allows users to select a single or multiple
options from a list of options.
 CheckBox Control accepts either True or False as a value.
 Namespace : System.Windows.Forms.CheckBox

Unit: 5 – Windows Forms and Controls in details 29 Darshan Institute of Engineering & Technology
Properties of CheckBox Control
Property Description
CheckAlign Gets or sets the location of the check box portion of the checkbox.
CheckState Gets or sets the state of the checkbox.
BackColor Gets or sets the background color of the checkbox control.
Image Gets or sets the image that is displayed on a checkbox control.
Font Gets or sets the font of the text displayed by checkbox control.

Unit: 5 – Windows Forms and Controls in details 30 Darshan Institute of Engineering & Technology
Methods of CheckBox Control
Method Description
Focus() Sets input focus to the control.
ResetBackColor() Resets the BackColor property to its default value.
ResetFont() Resets the Font property to its default value.
ResetText() Resets the Text property to its default value.
Select() Activates the control.
Show() Displays the control to the user.

Unit: 5 – Windows Forms and Controls in details 31 Darshan Institute of Engineering & Technology
Events of CheckBox Control
Event Description
CheckedChanged Occurs when the value of the Checked property of the CheckBox
control is changed.
CheckStateChanged Occurs when the value of the CheckState property of the CheckBox
control is changed.
AppearanceChanged Occurs when the value of the Appearance property of the CheckBox
is changed.

Unit: 5 – Windows Forms and Controls in details 32 Darshan Institute of Engineering & Technology
PictureBox Control
 The Windows Forms PictureBox Control is used to display images
in bitmap, GIF, Icon or JPEG formats.
 You can set the Image property to the image you want to display
either at design time or at run time.
 Namespace : System.Windows.Forms.PictureBox

Unit: 5 – Windows Forms and Controls in details 33 Darshan Institute of Engineering & Technology
Properties of PictureBox Control
Property Description
Image Gets or sets the image that is displayed by PictureBox.
ImageLocation Gets or sets the path or URL for the image to display in the PictureBox.
BorderStyle Indicates the border style for the control.
DefaultSize Gets the default size of the control.
Padding Gets or sets padding within the control.

Unit: 5 – Windows Forms and Controls in details 34 Darshan Institute of Engineering & Technology
Methods of PictureBox Control
Method Description
Focus() Sets input focus to the control.
Load() Displays the image specified by the ImageLocation property of the
PictureBox.
ResetFont() Resets the Font property to its default value.
ResetText() Resets the Text property to its default value.
Select() Activates the control.
Show() Displays the control to the user.

Unit: 5 – Windows Forms and Controls in details 35 Darshan Institute of Engineering & Technology
Events of PictureBox Control
Event Description
Click Occurs when the control is clicked.
Resize Occurs when the control is resized.
SizeChanged Occurs when the Size property value changes.

Unit: 5 – Windows Forms and Controls in details 36 Darshan Institute of Engineering & Technology
ListBox Control
 A ListBox control provides a user interface to display a list of items.
 Users can select one or more items from the list.
 If the items exceed a specified limit, a scroll bar automatically
appears to let the user to scroll through the list.
 Namespace : System.Windows.Forms.ListBox

Unit: 5 – Windows Forms and Controls in details 37 Darshan Institute of Engineering & Technology
Properties of ListBox Control
Property Description
Items Gets the items of the ListBox.
SelectedItem Gets or sets the currently selected item in the ListBox.
SelectedIndex Gets or sets the zero-based index of the currently selected item in a
ListBox.
SelectionMode Indicates if the list box is to be single-select, multi-select, or non-
selectable.

Unit: 5 – Windows Forms and Controls in details 38 Darshan Institute of Engineering & Technology
Methods of ListBox Control
Method Description
ClearSelected() Unselects all items in the ListBox.
Sort() Sorts the items in the ListBox.
ResetBackColor() Resets the BackColor property to its default value.
ResetFont() Resets the Font property to its default value.
ResetText() Resets the Text property to its default value.
Select() Activates the control.
Show() Displays the control to the user.

Unit: 5 – Windows Forms and Controls in details 39 Darshan Institute of Engineering & Technology
Events of ListBox Control
Event Description
SelectedIndexChanged Occurs when the SelectedIndex property has changed.
SizeChanged Occurs when the Size property value changes.

Unit: 5 – Windows Forms and Controls in details 40 Darshan Institute of Engineering & Technology
Example - ListBox
private void ListBox_Load(object sender, EventArgs e)
{
// Add Items in ListBox
lstState.Items.Add("Gujarat");
lstState.Items.Add("Maharastra");
lstState.Items.Add("Punjab");
lstState.Items.Add("Delhi");

//Sets currently selected item in the ListBox


lstState.SelectedItem = "Gujarat";
//Sets currently selected Index in the ComboBox
lstState.SelectedIndex = 1;

//Removes Item From List with value "Delhi"


lstState.Items.Remove("Delhi");

//Removes Item From List at Index 2 (Index start from 0)


lstState.Items.RemoveAt(2);

//Accessing All ListBox Items


string value = "";
for (int i = 0; i < lstState.Items.Count; i++)
{
value += lstState.GetItemText(lstState.Items[i]) + "\n";
}
MessageBox.Show(value);
}

Unit: 5 – Windows Forms and Controls in details 41 Darshan Institute of Engineering & Technology
Example – ListBox

lstState.SelectedItem = null;
//Clears All Items From
ListBox

lstState.Items.Clear();

Unit: 5 – Windows Forms and Controls in details 42 Darshan Institute of Engineering & Technology
Example – ListBox

Unit: 5 – Windows Forms and Controls in details 43 Darshan Institute of Engineering & Technology
Example - ListBox
public partial class ListBox_Demo : Form
{
public ListBox_Demo()
{
InitializeComponent();
lstLeft.Sorted = true;
}

private void btnleft_Click(object sender, EventArgs e)


{
if (lstLeft.SelectedIndex != -1)
{
lstRight.Items.Add(lstLeft.SelectedItem);
lstLeft.Items.Remove(lstLeft.SelectedItem);
}
}

private void btnRight_Click(object sender, EventArgs e)


{
if (lstRight.SelectedIndex != -1)
{
lstLeft.Items.Add(lstRight.SelectedItem);
lstRight.Items.Remove(lstRight.SelectedItem);
}
}
}

Unit: 5 – Windows Forms and Controls in details 44 Darshan Institute of Engineering & Technology
ComboBox Control
 A ComboBox control is a combination of a TextBox and a ListBox
control.
 Only one list item is displayed at one time in a ComboBox and
other available items are loaded in a ComboBox list.
 Namespace : System.Windows.Forms.ComboBox

Unit: 5 – Windows Forms and Controls in details 45 Darshan Institute of Engineering & Technology
Properties of ComboBox Control
Property Description
Items Gets the items of the ComboBox.
DisplayMember Gets or sets the property to display for this ListControl.
Enabled Gets or sets a value indicating whether the control can respond to user
interaction.

Unit: 5 – Windows Forms and Controls in details 46 Darshan Institute of Engineering & Technology
Methods of ComboBox Control
Method Description
ClearSelected() Unselects all items in the ComboBox.
Sort() Sorts the items in the ComboBox.
ResetBackColor() Resets the BackColor property to its default value.
ResetFont() Resets the Font property to its default value.
ResetText() Resets the Text property to its default value.
Select() Activates the control.
Show() Displays the control to the user.

Unit: 5 – Windows Forms and Controls in details 47 Darshan Institute of Engineering & Technology
Events of ComboBox Control
Event Description
SelectedIndexChanged Occurs when the SelectedIndex property has changed.
SizeChanged Occurs when the Size property value changes.
GotFocus Occurs when the control receives focus.

Unit: 5 – Windows Forms and Controls in details 48 Darshan Institute of Engineering & Technology
Example - ComboBox
private void Combo_Box_Load(object sender, EventArgs e)
{
// Add Items in ComboBox
cmbState.Items.Add("Gujarat");
cmbState.Items.Add("Maharastra");
cmbState.Items.Add("Punjab");
cmbState.Items.Add("Delhi");

//Sets currently selected Item in the ComboBox


cmbState.SelectedItem = "Gujarat";
//Sets currently selected Index in the ComboBox
cmbState.SelectedIndex = 0;

//Removes Item From List with value "Delhi"


cmbState.Items.Remove("Delhi");

//Removes Item From List at Index 2 (Index start from 0)


cmbState.Items.RemoveAt(2);

//Accessing All ComboBox Items


for (int i = 0; i < cmbState.Items.Count; i++)
{
string value = cmbState.GetItemText(cmbState.Items[i]);
MessageBox.Show(value);
}
}

Unit: 5 – Windows Forms and Controls in details 49 Darshan Institute of Engineering & Technology
Output - ComboBox

cmbState.SelectedItem = null;
//Clears All Items From
ComboBox

cmbState.Items.Clear();

Unit: 5 – Windows Forms and Controls in details 50 Darshan Institute of Engineering & Technology
CheckedListBox Control
 The Windows Forms CheckedListBox control displays a list of
items, like the ListBox control, and also can display a check mark
next to items in the list.
 Users can select one or more items from the list.
 Namespace : System.Windows.Forms.CheckedListBox

Unit: 5 – Windows Forms and Controls in details 51 Darshan Institute of Engineering & Technology
Properties of CheckedListBox Control
Property Description
CheckedItems Collection of checked items in this CheckedListBox.
CheckedIndice Collection of checked indexes in CheckedListBox.
s
CheckOnClick Gets or sets a value indicating whether the check box should be toggled
when an item is selected.
Items Gets the collection of items in this CheckedListBox.
Gets or sets a value indicating whether the items in the CheckedListBox
Sorted
are sorted alphabetically.

Unit: 5 – Windows Forms and Controls in details 52 Darshan Institute of Engineering & Technology
Methods of CheckedListBox Control
Method Description
ClearSelected() Unselects all items in the CheckedListBox.
Sort() Sorts the items in the CheckedListBox.
ResetBackColor() Resets the BackColor property to its default value.
ResetFont() Resets the Font property to its default value.
ResetText() Resets the Text property to its default value.
Select() Activates the control.
Show() Displays the control to the user.

Unit: 5 – Windows Forms and Controls in details 53 Darshan Institute of Engineering & Technology
Events of CheckedListBox Control
Event Description
Click Occurs when the user clicks the CheckedListBox control.
ItemCheck Occurs when the checked state of an item changes.
SelectedIndexChanged Occurs when the SelectedIndex property or the SelectedIndices
collection has changed.
SelectedValueChanged Occurs when the SelectedValue property changes.

Unit: 5 – Windows Forms and Controls in details 54 Darshan Institute of Engineering & Technology
Example - CheckedListBox
private void Form1_Load(object sender, EventArgs e)
{
chklstBox.Items.Add("Gujarat");
chklstBox.Items.Add("Maharastra");
chklstBox.Items.Add("Punjab");
chklstBox.Items.Add("Delhi");
chklstBox.Items.Add("Bihar");
}

private void btnDisplay_Click(object sender, EventArgs e)


{
//With Items
foreach (string s in chklstBox.CheckedItems)
{
lstBox.Items.Add(s);
}

// With Indexes
for (int i = 0; i < chklstBox.CheckedIndices.Count; i++)
{
lstBox.Items.Add(chklstBox.CheckedIndices[i]);
}
}

Unit: 5 – Windows Forms and Controls in details 55 Darshan Institute of Engineering & Technology
Output - CheckedListBox

Values
Indexes

Unit: 5 – Windows Forms and Controls in details 56 Darshan Institute of Engineering & Technology
DataGridView Control
 Displaying data in tabular format like rows and columns with the
help of DataGridView Control.
 The DataGridView Control is designed to displaying tabular data in
windows forms.
 It makes easy to define the basic appearance of cells and the
display formatting of cell values.
 All cells derive from the DataGridViewCell base class.

Unit: 5 – Windows Forms and Controls in details 57 Darshan Institute of Engineering & Technology
Properties of DataGridView Control
Property Description
CellBorderStyle Gets the cell border style for the DataGridView.
ColumnCount Gets or sets the number of columns displayed in the DataGridView.
Columns Gets a collection that contains all the columns in the control.
CurrentRow Gets the row containing the current cell.

Unit: 5 – Windows Forms and Controls in details 58 Darshan Institute of Engineering & Technology
Methods of DataGridView Control
Method Description
AutoResizeRows() Adjusts the heights of all rows to fit the contents of all their cells,
including the header cells.
Refresh() Forces the control to invalidate its client area and immediately redraw
itself and any child controls.
ResetBackColor() Resets the BackColor property to its default value.
ResetFont() Resets the Font property to its default value.
ResetText() Resets the Text property to its default value.
Select() Activates the control.
Show() Displays the control to the user.

Unit: 5 – Windows Forms and Controls in details 59 Darshan Institute of Engineering & Technology
Events of DataGridView Control
Event Description
CellClick Occurs when any part of a cell is clicked.
CellLeave Occurs when a cell loses input focus and is no longer the current cell.
Click Occurs when the control is clicked.
DataError Occurs when an external data-parsing or validation operation throws
an exception, or when an attempt to commit data to a data source
fails.

Unit: 5 – Windows Forms and Controls in details 60 Darshan Institute of Engineering & Technology
Example - DataGridView Control

Unit: 5 – Windows Forms and Controls in details 61 Darshan Institute of Engineering & Technology
Bind DataGridView Control

Unit: 5 – Windows Forms and Controls in details 62 Darshan Institute of Engineering & Technology
Bind DataGridView Control Cont..

Unit: 5 – Windows Forms and Controls in details 63 Darshan Institute of Engineering & Technology
Bind DataGridView Control Cont..

Unit: 5 – Windows Forms and Controls in details 64 Darshan Institute of Engineering & Technology
Bind DataGridView Control Cont..

Unit: 5 – Windows Forms and Controls in details 65 Darshan Institute of Engineering & Technology
Bind DataGridView Control Cont..

Unit: 5 – Windows Forms and Controls in details 66 Darshan Institute of Engineering & Technology
Bind DataGridView Control Cont..

Unit: 5 – Windows Forms and Controls in details 67 Darshan Institute of Engineering & Technology
Bind DataGridView Control Cont..

Unit: 5 – Windows Forms and Controls in details 68 Darshan Institute of Engineering & Technology
Bind DataGridView Control Cont..

Unit: 5 – Windows Forms and Controls in details 69 Darshan Institute of Engineering & Technology
Output

Unit: 5 – Windows Forms and Controls in details 70 Darshan Institute of Engineering & Technology
Menus
 Menus are controls that allows a user to make selections.
 They also hide the selections that are not needed, thereby saving
space in windows applications.
 Menus act like containers for the ToolStripMenuItem objects and
are used to display menu and menu items in a menu bar.
 Any item that resides on a menu is known as a menu item and
represents an individual part of a menu.
 Namespace : System.Windows.Forms.MenuStrip.

Unit: 5 – Windows Forms and Controls in details 71 Darshan Institute of Engineering & Technology
Menus Cont..
 There are main two classes in the standard menu handling.
• MenuStrip : Acts as a container for the menu structure of a form.
• ToolStripMenuItem : Supports the items in a menu system (Including the
menus such as File & Edit)

Unit: 5 – Windows Forms and Controls in details 72 Darshan Institute of Engineering & Technology
ToolStripMenuItem Class
 The ToolStripMenuItem class supports the menus and menu items
in a menu system.
 These menu items are objects that can handle through Click
events in a menu system.
 It has the properties that permits you to configure the
functionality and appearance of a menu item like add shortcut
keys, font, back color etc.
 Namespace : System.Windows.Forms.ToolStripMenuItem.

Unit: 5 – Windows Forms and Controls in details 73 Darshan Institute of Engineering & Technology
Example – Adding Menu Items
 Drag and drop a MenuStrip control, named menuStrip1, from the
toolbox to the form.
 Click the Type Here text to open a text box and enter ‘File’ text in
it. Now you can add some more items to the File menu.
 You can make the menu items perform some actions by creating
event handlers for handling their Click event in the code editor.

Unit: 5 – Windows Forms and Controls in details 74 Darshan Institute of Engineering & Technology
ContextMenuStrip Control
 Context menus are shortcut menus that appear when a user right-
click on the screen.
 Usually, context menus are used to display control-specific
options, such as Cut, Copy, Paste in text boxes.
 You can use the ContextMenuStrip control to create a context
menu and provide users to access frequently used menu
commands.
 Namespace : System.Windows.Forms.ContextMenuStrip.

Unit: 5 – Windows Forms and Controls in details 75 Darshan Institute of Engineering & Technology
ContextMenuStrip Control Cont..
 Similar to main menus, context menu items can be disabled,
hidden, or deleted.
 You can also show the context menus with the help of the Show()
method of ContextMenuStrip control.
 Major difference is that the context menus are not divided into
separate menus, such as File, Edit and Window.

Unit: 5 – Windows Forms and Controls in details 76 Darshan Institute of Engineering & Technology
Introduction to Dialogs
 There are several built-in dialog boxes in Windows Forms.
 The built-in dialog boxes reduce the time and work required for
developing commonly used dialog boxes such as file open, file
save and other dialog boxes.
 Some of the dialog box controls are OpenFileDialog,
SaveFileDialog and FontDialog etc.
 The ShowDialog() Method displays the dialog box at runtime.

Unit: 5 – Windows Forms and Controls in details 77 Darshan Institute of Engineering & Technology
Types of Dialog box Controls
 ColorDialog Control
 FolderBrowserDialog Contol
 FontDialog Control
 OpenFileDialog Control
 SaveFileDialog Control

Unit: 5 – Windows Forms and Controls in details 78 Darshan Institute of Engineering & Technology
ColorDialog Control
 The Color dialog box is use to select or pick a color.
 The user can select or create a particular color from the list, which
is then reported back to the application when the dialog box exits.
 Namespace : System.Windows.Forms.ColorDialog

Unit: 5 – Windows Forms and Controls in details 79 Darshan Institute of Engineering & Technology
Properties of ColorDialog
Event Description
AllowFullOpen Retrieves or sets a value specifying whether or not the user can use
the dialog box to define custom colors.
AnyColor Retrieves or sets a value specifying whether or not the dialog box
displays all available colors in the set of basic colors.
Color Retrieves or sets the color selected by the user for the text.
CustomColors Retrieves or sets the set of custom colors shown in the dialog box.
FullOpen Retrieves or sets a value specifying whether or not the controls
used to create custom colors are visible when the dialog box is
opened.
ShowHelp Retrieves or sets a value specifying whether or not a Help button
appears in the color dialog box.
SolidColorOnly Retrieves or sets a value specifying whether or not the dialog box
restricts users to selecting solid colors only and not dithered colors.

Unit: 5 – Windows Forms and Controls in details 80 Darshan Institute of Engineering & Technology
Example – ColorDialog Cont..
 Create a from with a label control and a button control.
 Drag and drop a ColorDialog control on your form.
 When user click on a button, ColorDialog opens and after selecting
particular color press OK, You will see the change.

Unit: 5 – Windows Forms and Controls in details 81 Darshan Institute of Engineering & Technology
Example – ColorDialog
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Demo_Windows
{
public partial class ColorDialog : Form
{
public ColorDialog()
{
InitializeComponent();
}

private void btnSetColor_Click(object sender, EventArgs e)


{
if (diaCD.ShowDialog() == DialogResult.OK)
{
lblChangeMyColor.BackColor = diaCD.Color;
lblChangeMyColor.Text = "Color Changed!!";
}
}
}
}

Unit: 5 – Windows Forms and Controls in details 82 Darshan Institute of Engineering & Technology
Output – ColorDialog

Unit: 5 – Windows Forms and Controls in details 83 Darshan Institute of Engineering & Technology
FolderBrowserDialog Control
 As name indicates, the FolderBrowserDialog control opens the
browse for folder dialog box which lets the user select a folder.
 The FolderBrowserDialog control is based on FolderBrowserDialog
class.
 Namespace : System.Windows.Forms.FolderBrowserDialog

Unit: 5 – Windows Forms and Controls in details 84 Darshan Institute of Engineering & Technology
Properties of FolderBrowserDialog
Event Description
Description Retrieves or sets the description text displayed above the
TreeView control in the dialog box.
RootFolder Retrieves or sets the root folder where the browsing starts.
SelectedPath Retrieves or sets the path selected by the user.
ShowNewFolderButton Retrieves or sets a value indicating whether or not the New Folder
button appears in the folder dialog box.

Unit: 5 – Windows Forms and Controls in details 85 Darshan Institute of Engineering & Technology
Example – FolderBrowserDialog
 Create a from with a label control, textbox control and a button
control.
 Drag and drop a FolderBrowserDialog control on your form.
 When user click on a button, FolderBrowserDialog opens and after
selecting particular folder path comes in a textbox.

Unit: 5 – Windows Forms and Controls in details 86 Darshan Institute of Engineering & Technology
Example – FolderBrowserDialog
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Demo_Windows
{
public partial class DialogBoxDemo : Form
{
public DialogBoxDemo()
{
InitializeComponent();
}
// Browse Button Click Event
private void btnBrowse_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.RootFolder = Environment.SpecialFolder.MyComputer;
fbd.SelectedPath = "D:\\";
fbd.Description = "Select Path";

if (fbd.ShowDialog() == DialogResult.OK)
{
txtPath.Text = fbd.SelectedPath;
}
}
}
}

Unit: 5 – Windows Forms and Controls in details 87 Darshan Institute of Engineering & Technology
Output – FolderBrowserDialog

Unit: 5 – Windows Forms and Controls in details 88 Darshan Institute of Engineering & Technology
FontDialog Control
 The font dialog box is use to select the font, font style and font
size.
• Namespace : System.Windows.Forms.FontDialog

Unit: 5 – Windows Forms and Controls in details 89 Darshan Institute of Engineering & Technology
Properties of FontDialog
Event Description
Font Retrieves or sets the selected font.
FontMustExist Retrieves or sets a value specifying whether or not the dialog box
specifies an error condition if the user attempts to select a font or
style that does not exist.
MaxSize Retrieves or sets the maximum font size a user can select.
The default value for the font size is 0.
MinSize Retrieves or sets the minimum font size a user can select.
ShowApply Retrieves or sets a value specifying whether or not the dialog box
contains an Apply button.
ShowColor Retrieves or sets a value specifying whether or not the dialog box
displays the color choice.
ShowHelp Retrieves or sets a value specifying whether or not the dialog box
displays a Help button.

Unit: 5 – Windows Forms and Controls in details 90 Darshan Institute of Engineering & Technology
Example - FontDialog
 Create a from with a label control and a button control.
 Drag and drop a FontDialog control on your form.
 When user click on a button, FontDialog opens and after choosing
particular font, font size, and color press OK for see the change.

Unit: 5 – Windows Forms and Controls in details 91 Darshan Institute of Engineering & Technology
Example – FontDialog Cont..
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Demo_Windows
{
public partial class FontDialog : Form
{
public FontDialog()
{
InitializeComponent();
}
private void btnChangeFont_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
lblChangeMyFont.Font = fontDialog1.Font;
lblChangeMyFont.ForeColor = fontDialog1.Color;
lblChangeMyFont.Text = "Font Changed.";
}
}
}
}

Unit: 5 – Windows Forms and Controls in details 92 Darshan Institute of Engineering & Technology
Output – FontDialog

Unit: 5 – Windows Forms and Controls in details 93 Darshan Institute of Engineering & Technology
OpenFileDialog Control
 This control allows user to select a file and open from the
OpenFileDialog box.
 It enables user to check if file exists or not and then opens it.
 Namespace : System.Windows.Forms.OpenFileDialog

Unit: 5 – Windows Forms and Controls in details 94 Darshan Institute of Engineering & Technology
Properties of OpenFileDialog
Event Description
CheckFileExists Retrieves or sets a value indicating whether the dialog box diaplays a
warning if the user specifies a non-existent file.
MultiSelect Retrieves or sets a value specifying whether or not the dialog box
allows multiple file selections.
ReadOnlyChecked Retrieves or sets a value specifying whether or not the read only
checkbox is selected on the dialog box.
SafeFileName Retrieves the file name and extension for the selected file in the
dialog box.
The file name does not include the path.
ShowReadOnly Retrieves or sets a value specifying whether or not the dialog box
displays a read only checkbox.

Unit: 5 – Windows Forms and Controls in details 95 Darshan Institute of Engineering & Technology
Example - OpenFileDialog
 Create a from with a label control and a button control.
 Drag and drop a OpenFileDialog control on your form.
 When user click on a button, OpenFileDialog opens and after
choosing a file, path will be set to the label control.

Unit: 5 – Windows Forms and Controls in details 96 Darshan Institute of Engineering & Technology
Example – OpenFileDialog Cont..
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Demo_Windows
{
public partial class OpenFileDialog : Form
{
public OpenFileDialog()
{
InitializeComponent();
}
private void btnSetFileName_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "Select Files";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;

openFileDialog1.DefaultExt = "jpg";
openFileDialog1.Filter = "Image files (*.jpg)|*.jpg|All files (*.*)|*.*";

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
lblFileName.Text = openFileDialog1.FileName;
}
}
}
}

Unit: 5 – Windows Forms and Controls in details 97 Darshan Institute of Engineering & Technology
Output – OpenFileDialog

Unit: 5 – Windows Forms and Controls in details 98 Darshan Institute of Engineering & Technology
SaveFileDialog Control
 The SaveFileDialog control supports the Save As dialog box that
allows the user to specify the name of a file to save data.
 Namespace : System.Windows.Forms.SaveFileDialog

Unit: 5 – Windows Forms and Controls in details 99 Darshan Institute of Engineering & Technology
Properties of SaveFileDialog
Event Description
CreatePrompt Retrieves or sets a value specifying whether or not the dialog box
asks the user that it should create a file if the user specifies a non
existent file.
OverwritePrompt Retrieves or sets a value specifying whether or not the dialog box
displays a warning if the user specifies a name that already exists.

Unit: 5 – Windows Forms and Controls in details 100 Darshan Institute of Engineering & Technology
Example - SaveFileDialog
 Create a from with a label control, a button control and a text box
control.
 Drag and drop a SaveFileDialog control on your form.
 When user click on a button, SaveFileDialog opens and after saving
a file with name, whatever remains in a text box will write in the
file.

Unit: 5 – Windows Forms and Controls in details 101 Darshan Institute of Engineering & Technology
Example – SaveFileDialog Cont..
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace Demo_Windows
{
public partial class SaveFileDialog : Form
{
public SaveFileDialog()
{
InitializeComponent();
}

private void btnSave_Click(object sender, EventArgs e)


{
saveFileDialog1.Filter = "Text Files (*.txt)|*.txt*";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveFileDialog1.FileName, txtPath.Text);
}
}
}
}

Unit: 5 – Windows Forms and Controls in details 102 Darshan Institute of Engineering & Technology
Output – SaveFileDialog

Unit: 5 – Windows Forms and Controls in details 103 Darshan Institute of Engineering & Technology
ToolTip
 The Tooltip control is used to display a small window with
explanatory text for an element on the interface.
 It is for a control or window appears when you move the mouse
over the control or window.
 Namespace : System.Windows.Forms.ToolTip
 Properties for a ToolTip control are as follows.
Properties Description
Active Specifies whether or not the tooltip control is active
AutomaticDelay Obtains or sets the time (in milliseconds) before the tool tip
appears

Unit: 5 – Windows Forms and Controls in details 104 Darshan Institute of Engineering & Technology
Example - ToolTip
 Create a from with a button control.
 Drag and drop a ToolTip control on your form.
 When user hover on a button, ToolTip with the text displays.

Unit: 5 – Windows Forms and Controls in details 105 Darshan Institute of Engineering & Technology
Cont..
using System;
using System.Windows.Forms;

namespace Demo_Windows
{
public partial class Tooltip_Demo : Form
{
public Tooltip_Demo()
{
InitializeComponent();
}

private void Tooltip_Demo_Load(object sender, EventArgs e)


{
ToolTip toolTip = new ToolTip();
toolTip.ShowAlways = true;
toolTip.SetToolTip(btnSave, "Click Here to Save Data.");
}
}
}

Unit: 5 – Windows Forms and Controls in details 106 Darshan Institute of Engineering & Technology

You might also like