2160711 Dot Net Technology Unit 5 Windows Forms

  • Slides: 107
Download presentation
2160711 Dot Net Technology Unit - 5 Windows Forms and Controls in details Prof.

2160711 Dot Net Technology Unit - 5 Windows Forms and Controls in details Prof. Naimish R. Vadodariya 8866215253 naimish. vadodariya@darshan. ac. in

Outline § Introduction to Windows Forms § How to Create Windows Forms? § Windows

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

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

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,

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

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. . 1 5 Unit: 5 – Windows Forms and Controls

Create Windows Forms Cont. . 1 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.

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. Back. Color Sets the form background color. Accept. Button 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 Accept. Button for a form. Cancel. Button The button that's automatically activated when you hit the Esc key. Window. State Form remains in default size as you show in design, but you can change its state by minimized, maximized & normal. Start. Position This property enables you to set the starting position of the form when it is displayed at run time. Unit: 5 – Windows Forms and Controls in details 8 Darshan Institute of Engineering & Technology

Windows Forms Events Description Activated Occurs when the form is activated in code or

Windows Forms 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. Mouse. Hover Occurs when the mouse pointer rests on the form. Double. Click Occurs when the form control is double-clicked. Key. Down 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.

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 List. Box Data. Grid. View Text. Box Combo. Box Button Checked. List. Box Radio. Button Check. Box Picture. Box Unit: 5 – Windows Forms and Controls in details 10 Darshan Institute of Engineering & Technology

Common Properties of Controls Property Description Text The name of the control. This name

Common Properties of Controls Property Description Text The name of the control. This name can be used to reference the control in code. 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. Back. Color The background color of a control. Fore. Color The foreground color of the control. Font Gets or sets the font of the text displayed by the control. Name Unit: 5 – Windows Forms and Controls in details 11 Darshan Institute of Engineering & Technology

Common Events of Controls Event Click Double. Click Drag. Drop Key. Down Key. Press

Common Events of Controls Event Click Double. Click Drag. Drop Key. Down Key. Press Got. Focus Lost. Focus Description Occurs when a control is clicked. In some cases, this event will also occur when a user presses Enter. Occurs when a control is double-clicked. Handling the Click event on some controls, such as the Button control will mean that the Double. Click event can never be called. Occurs when a drag-and-drop operation is completed, in other words, 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 always occurs before Key. Press and Key. Up. Occurs when a key becomes pressed, while a control has focus. This event always occurs after Key. Down and before Key. Up. Occurs when a control receives focus. Do not use this event to perform validation of controls. Use Validating and Validated instead. Occurs when a control looses focus. Do not use this event to perform 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

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 Text. Align Gets or sets how text is

Properties of Label Control Property Description Text. Align 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. Reset.

Methods of Label Control Method Description Focus() Sets input focus to the control. Reset. Text() Resets the Text property to its default value. Reset. Font() Resets the Font property to its default value. Reset. Fore. Color() Resets the Fore. Color 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 Text. Changed Occurs when the Text property value

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

Text. Box Control § The Text. Box control is a windows forms control that

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

Properties of Text. Box Control Property Description Text. Length Gets the length of text

Properties of Text. Box Control Property Description Text. Length Gets the length of text in the Text. Box control. Password. Char Gets or sets the character used to mask characters of a password in a single-line Text. Box control. Multiline Gets or sets a value indicating whether this is a multiline Text. Box control. Read. Only Gets or sets a value indicating whether text in the text box is read-only. Word. Wrap 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 Text. Box Control Method Description Clear() Clears all text from the text

Methods of Text. Box 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. Deselect. All() Specifies that the value of the Selection. Length property is zero so that no characters are selected in the control. Select. All() Selects all text in the text box. Focus() Sets input focus to the control. Reset. Text() Resets the Text property to its default value. Unit: 5 – Windows Forms and Controls in details 19 Darshan Institute of Engineering & Technology

Events of Text. Box Control Event Description Text. Changed Occurs when the text in

Events of Text. Box Control Event Description Text. Changed Occurs when the text in the textbox changes. Text. Align. Changed Occurs when the Text. Align 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

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 Back. Color Gets or sets the background color

Properties of Button Control Property Description Back. Color Gets or sets the background color of the button control. Image Gets or sets the image that is displayed on a button control. Flat. Style 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. Reset.

Methods of Button Control Method Description Focus() Sets input focus to the control. Reset. Back. Color() Resets the Back. Color property to its default value. Reset. Font() Resets the Font property to its default value. Reset. Text() 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

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

Radio. Button Control § Radio. Button Control enables the user to select a single

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

Properties of Radio. Button Control Property Description Checked Gets or sets a value indicating

Properties of Radio. Button Control Property Description Checked Gets or sets a value indicating whether the radio button is checked. Check. Align 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 Radio. Button Control Method Description Focus() Sets input focus to the control.

Methods of Radio. Button Control Method Description Focus() Sets input focus to the control. Reset. Back. Color() Resets the Back. Color property to its default value. Reset. Font() Resets the Font property to its default value. Reset. Text() 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 Radio. Button Control Event Description Checked. Changed Occurs when the value of

Events of Radio. Button Control Event Description Checked. Changed Occurs when the value of the checked property of the radio button control is changed. Appearance. Changed 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

Check. Box Control § A Check. Box control allows users to select a single

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

Properties of Check. Box Control Property Description Check. Align Gets or sets the location

Properties of Check. Box Control Property Description Check. Align Gets or sets the location of the check box portion of the checkbox. Check. State Gets or sets the state of the checkbox. Back. Color 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 Check. Box Control Method Description Focus() Sets input focus to the control.

Methods of Check. Box Control Method Description Focus() Sets input focus to the control. Reset. Back. Color() Resets the Back. Color property to its default value. Reset. Font() Resets the Font property to its default value. Reset. Text() 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 Check. Box Control Event Description Checked. Changed Occurs when the value of

Events of Check. Box Control Event Description Checked. Changed Occurs when the value of the Checked property of the Check. Box control is changed. Check. State. Changed Occurs when the value of the Check. State property of the Check. Box control is changed. Appearance. Changed Occurs when the value of the Appearance property of the Check. Box is changed. Unit: 5 – Windows Forms and Controls in details 32 Darshan Institute of Engineering & Technology

Picture. Box Control § The Windows Forms Picture. Box Control is used to display

Picture. Box Control § The Windows Forms Picture. Box 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. Picture. Box Unit: 5 – Windows Forms and Controls in details 33 Darshan Institute of Engineering & Technology

Properties of Picture. Box Control Property Description Image Gets or sets the image that

Properties of Picture. Box Control Property Description Image Gets or sets the image that is displayed by Picture. Box. Image. Location Gets or sets the path or URL for the image to display in the Picture. Box. Border. Style Indicates the border style for the control. Default. Size 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 Picture. Box Control Method Description Focus() Sets input focus to the control.

Methods of Picture. Box Control Method Description Focus() Sets input focus to the control. Load() Displays the image specified by the Image. Location property of the Picture. Box. Reset. Font() Resets the Font property to its default value. Reset. Text() 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 Picture. Box Control Event Description Click Occurs when the control is clicked.

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

List. Box Control § A List. Box control provides a user interface to display

List. Box Control § A List. Box 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. List. Box Unit: 5 – Windows Forms and Controls in details 37 Darshan Institute of Engineering & Technology

Properties of List. Box Control Property Description Items Gets the items of the List.

Properties of List. Box Control Property Description Items Gets the items of the List. Box. Selected. Item Gets or sets the currently selected item in the List. Box. Selected. Index Gets or sets the zero-based index of the currently selected item in a List. Box. Selection. Mode 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 List. Box Control Method Description Clear. Selected() Unselects all items in the

Methods of List. Box Control Method Description Clear. Selected() Unselects all items in the List. Box. Sort() Sorts the items in the List. Box. Reset. Back. Color() Resets the Back. Color property to its default value. Reset. Font() Resets the Font property to its default value. Reset. Text() 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 List. Box Control Event Description Selected. Index. Changed Occurs when the Selected.

Events of List. Box Control Event Description Selected. Index. Changed Occurs when the Selected. Index property has changed. Size. Changed Occurs when the Size property value changes. Unit: 5 – Windows Forms and Controls in details 40 Darshan Institute of Engineering & Technology

Example - List. Box private void List. Box_Load(object sender, Event. Args e) { //

Example - List. Box private void List. Box_Load(object sender, Event. Args e) { // Add Items in List. Box lst. State. Items. Add("Gujarat"); lst. State. Items. Add("Maharastra"); lst. State. Items. Add("Punjab"); lst. State. Items. Add("Delhi"); //Sets currently selected item in the List. Box lst. State. Selected. Item = "Gujarat"; //Sets currently selected Index in the Combo. Box lst. State. Selected. Index = 1; //Removes Item From List with value "Delhi" lst. State. Items. Remove("Delhi"); //Removes Item From List at Index 2 (Index start from 0) lst. State. Items. Remove. At(2); //Accessing All List. Box Items string value = ""; for (int i = 0; i < lst. State. Items. Count; i++) { value += lst. State. Get. Item. Text(lst. State. Items[i]) + "n"; } Message. Box. Show(value); } Unit: 5 – Windows Forms and Controls in details 41 Darshan Institute of Engineering & Technology

Example – List. Box lst. State. Selected. Item = null; //Clears All Items From

Example – List. Box lst. State. Selected. Item = null; //Clears All Items From List. Box lst. State. Items. Cle ar(); Unit: 5 – Windows Forms and Controls in details 42 Darshan Institute of Engineering & Technology

Example – List. Box Unit: 5 – Windows Forms and Controls in details 43

Example – List. Box Unit: 5 – Windows Forms and Controls in details 43 Darshan Institute of Engineering & Technology

Example - List. Box public partial class List. Box_Demo : Form { public List.

Example - List. Box public partial class List. Box_Demo : Form { public List. Box_Demo() { Initialize. Component(); lst. Left. Sorted = true; } private void btnleft_Click(object sender, Event. Args e) { if (lst. Left. Selected. Index != -1) { lst. Right. Items. Add(lst. Left. Selected. Item); lst. Left. Items. Remove(lst. Left. Selected. Item); } } private void btn. Right_Click(object sender, Event. Args e) { if (lst. Right. Selected. Index != -1) { lst. Left. Items. Add(lst. Right. Selected. Item); lst. Right. Items. Remove(lst. Right. Selected. Item); } } } Unit: 5 – Windows Forms and Controls in details 44 Darshan Institute of Engineering & Technology

Combo. Box Control § A Combo. Box control is a combination of a Text.

Combo. Box Control § A Combo. Box control is a combination of a Text. Box and a List. Box control. § Only one list item is displayed at one time in a Combo. Box and other available items are loaded in a Combo. Box list. § Namespace : System. Windows. Forms. Combo. Box Unit: 5 – Windows Forms and Controls in details 45 Darshan Institute of Engineering & Technology

Properties of Combo. Box Control Property Description Items Gets the items of the Combo.

Properties of Combo. Box Control Property Description Items Gets the items of the Combo. Box. Display. Member Gets or sets the property to display for this List. Control. 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 Combo. Box Control Method Description Clear. Selected() Unselects all items in the

Methods of Combo. Box Control Method Description Clear. Selected() Unselects all items in the Combo. Box. Sort() Sorts the items in the Combo. Box. Reset. Back. Color() Resets the Back. Color property to its default value. Reset. Font() Resets the Font property to its default value. Reset. Text() 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 Combo. Box Control Event Description Selected. Index. Changed Occurs when the Selected.

Events of Combo. Box Control Event Description Selected. Index. Changed Occurs when the Selected. Index property has changed. Size. Changed Occurs when the Size property value changes. Got. Focus Occurs when the control receives focus. Unit: 5 – Windows Forms and Controls in details 48 Darshan Institute of Engineering & Technology

Example - Combo. Box private void Combo_Box_Load(object sender, Event. Args e) { // Add

Example - Combo. Box private void Combo_Box_Load(object sender, Event. Args e) { // Add Items in Combo. Box cmb. State. Items. Add("Gujarat"); cmb. State. Items. Add("Maharastra"); cmb. State. Items. Add("Punjab"); cmb. State. Items. Add("Delhi"); //Sets currently selected Item in the Combo. Box cmb. State. Selected. Item = "Gujarat"; //Sets currently selected Index in the Combo. Box cmb. State. Selected. Index = 0; //Removes Item From List with value "Delhi" cmb. State. Items. Remove( "Delhi"); //Removes Item From List at Index 2 (Index start from 0) cmb. State. Items. Remove. At(2); //Accessing All Combo. Box Items for (int i = 0; i < cmb. State. Items. Count; i++) { string value = cmb. State. Get. Item. Text(cmb. State. Items[i]); Message. Box. Show(value); } } Unit: 5 – Windows Forms and Controls in details 49 Darshan Institute of Engineering & Technology

Output - Combo. Box cmb. State. Selected. Item = null; //Clears All Items From

Output - Combo. Box cmb. State. Selected. Item = null; //Clears All Items From Combo. Box cmb. State. Items. Cle ar(); Unit: 5 – Windows Forms and Controls in details 50 Darshan Institute of Engineering & Technology

Checked. List. Box Control § The Windows Forms Checked. List. Box control displays a

Checked. List. Box Control § The Windows Forms Checked. List. Box control displays a list of items, like the List. Box 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. Checked. List. Box Unit: 5 – Windows Forms and Controls in details 51 Darshan Institute of Engineering & Technology

Properties of Checked. List. Box Control Property Description Checked. Items Collection of checked items

Properties of Checked. List. Box Control Property Description Checked. Items Collection of checked items in this Checked. List. Box. Checked. Indices Collection of checked indexes in Checked. List. Box. Check. On. Click 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 Checked. List. Box. Sorted Gets or sets a value indicating whether the items in the Checked. List. Box are sorted alphabetically. Unit: 5 – Windows Forms and Controls in details 52 Darshan Institute of Engineering & Technology

Methods of Checked. List. Box Control Method Description Clear. Selected() Unselects all items in

Methods of Checked. List. Box Control Method Description Clear. Selected() Unselects all items in the Checked. List. Box. Sort() Sorts the items in the Checked. List. Box. Reset. Back. Color() Resets the Back. Color property to its default value. Reset. Font() Resets the Font property to its default value. Reset. Text() 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 Checked. List. Box Control Event Description Click Occurs when the user clicks

Events of Checked. List. Box Control Event Description Click Occurs when the user clicks the Checked. List. Box control. Item. Check Occurs when the checked state of an item changes. Selected. Index. Changed Occurs when the Selected. Index property or the Selected. Indices collection has changed. Selected. Value. Changed Occurs when the Selected. Value property changes. Unit: 5 – Windows Forms and Controls in details 54 Darshan Institute of Engineering & Technology

Example - Checked. List. Box private void Form 1_Load(object sender, Event. Args e) {

Example - Checked. List. Box private void Form 1_Load(object sender, Event. Args e) { chklst. Box. Items. Add("Gujarat"); chklst. Box. Items. Add("Maharastra"); chklst. Box. Items. Add("Punjab"); chklst. Box. Items. Add("Delhi"); chklst. Box. Items. Add("Bihar"); } private void btn. Display_Click(object sender, Event. Args e) { //With Items foreach (string s in chklst. Box. Checked. Items) { lst. Box. Items. Add(s); } // With Indexes for (int i = 0; i < chklst. Box. Checked. Indices. Count; i++) { lst. Box. Items. Add(chklst. Box. Checked. Indices[i]); } } Unit: 5 – Windows Forms and Controls in details 55 Darshan Institute of Engineering & Technology

Output - Checked. List. Box Values Indexes Unit: 5 – Windows Forms and Controls

Output - Checked. List. Box Values Indexes Unit: 5 – Windows Forms and Controls in details 56 Darshan Institute of Engineering & Technology

Data. Grid. View Control § Displaying data in tabular format like rows and columns

Data. Grid. View Control § Displaying data in tabular format like rows and columns with the help of Data. Grid. View Control. § The Data. Grid. View 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 Data. Grid. View. Cell base class. Unit: 5 – Windows Forms and Controls in details 57 Darshan Institute of Engineering & Technology

Properties of Data. Grid. View Control Property Description Cell. Border. Style Gets the cell

Properties of Data. Grid. View Control Property Description Cell. Border. Style Gets the cell border style for the Data. Grid. View. Column. Count Gets or sets the number of columns displayed in the Data. Grid. View. Columns Gets a collection that contains all the columns in the control. Current. Row Gets the row containing the current cell. Unit: 5 – Windows Forms and Controls in details 58 Darshan Institute of Engineering & Technology

Methods of Data. Grid. View Control Method Description Auto. Resize. Rows() Adjusts the heights

Methods of Data. Grid. View Control Method Description Auto. Resize. Rows() 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. Reset. Back. Color() Resets the Back. Color property to its default value. Reset. Font() Resets the Font property to its default value. Reset. Text() 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 Data. Grid. View Control Event Description Cell. Click Occurs when any part

Events of Data. Grid. View Control Event Description Cell. Click Occurs when any part of a cell is clicked. Cell. Leave Occurs when a cell loses input focus and is no longer the current cell. Click Occurs when the control is clicked. Data. Error 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 - Data. Grid. View Control Unit: 5 – Windows Forms and Controls in

Example - Data. Grid. View Control Unit: 5 – Windows Forms and Controls in details 61 Darshan Institute of Engineering & Technology

Bind Data. Grid. View Control Unit: 5 – Windows Forms and Controls in details

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

Bind Data. Grid. View Control Cont. . Unit: 5 – Windows Forms and Controls

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

Bind Data. Grid. View Control Cont. . Unit: 5 – Windows Forms and Controls

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

Bind Data. Grid. View Control Cont. . Unit: 5 – Windows Forms and Controls

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

Bind Data. Grid. View Control Cont. . Unit: 5 – Windows Forms and Controls

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

Bind Data. Grid. View Control Cont. . Unit: 5 – Windows Forms and Controls

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

Bind Data. Grid. View Control Cont. . Unit: 5 – Windows Forms and Controls

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

Bind Data. Grid. View Control Cont. . Unit: 5 – Windows Forms and Controls

Bind Data. Grid. View 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

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

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 Tool. Strip. Menu. Item 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. Menu. Strip. 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.

Menus Cont. . § There are main two classes in the standard menu handling. • Menu. Strip : Acts as a container for the menu structure of a form. • Tool. Strip. Menu. Item : 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

Tool. Strip. Menu. Item Class § The Tool. Strip. Menu. Item class supports the

Tool. Strip. Menu. Item Class § The Tool. Strip. Menu. Item 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. Tool. Strip. Menu. Item. Unit: 5 – Windows Forms and Controls in details 73 Darshan Institute of Engineering & Technology

Example – Adding Menu Items § Drag and drop a Menu. Strip control, named

Example – Adding Menu Items § Drag and drop a Menu. Strip control, named menu. Strip 1, 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

Context. Menu. Strip Control § Context menus are shortcut menus that appear when a

Context. Menu. Strip Control § Context menus are shortcut menus that appear when a user rightclick 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 Context. Menu. Strip control to create a context menu and provide users to access frequently used menu commands. § Namespace : System. Windows. Forms. Context. Menu. Strip. Unit: 5 – Windows Forms and Controls in details 75 Darshan Institute of Engineering & Technology

Context. Menu. Strip Control Cont. . § Similar to main menus, context menu items

Context. Menu. Strip 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 Context. Menu. Strip 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. §

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 Open. File. Dialog, Save. File. Dialog and Font. Dialog etc. § The Show. Dialog() 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 § § § Color. Dialog Control Folder. Browser. Dialog

Types of Dialog box Controls § § § Color. Dialog Control Folder. Browser. Dialog Contol Font. Dialog Control Open. File. Dialog Control Save. File. Dialog Control Unit: 5 – Windows Forms and Controls in details 78 Darshan Institute of Engineering & Technology

Color. Dialog Control § The Color dialog box is use to select or pick

Color. Dialog 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. Color. Dialog Unit: 5 – Windows Forms and Controls in details 79 Darshan Institute of Engineering & Technology

Properties of Color. Dialog Event Description Allow. Full. Open Retrieves or sets a value

Properties of Color. Dialog Event Description Allow. Full. Open Retrieves or sets a value specifying whether or not the user can use the dialog box to define custom colors. Any. Color 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. Custom. Colors Retrieves or sets the set of custom colors shown in the dialog box. Full. Open Retrieves or sets a value specifying whether or not the controls used to create custom colors are visible when the dialog box is opened. Show. Help Retrieves or sets a value specifying whether or not a Help button appears in the color dialog box. Solid. Color. Only 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 – Color. Dialog Cont. . § Create a from with a label control

Example – Color. Dialog Cont. . § Create a from with a label control and a button control. § Drag and drop a Color. Dialog control on your form. § When user click on a button, Color. Dialog 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 – Color. Dialog using System; using System. Component. Model; using System. Windows. Forms;

Example – Color. Dialog using System; using System. Component. Model; using System. Windows. Forms; namespace Demo_Windows { public partial class Color. Dialog : Form { public Color. Dialog() { Initialize. Component(); } private void btn. Set. Color_Click(object sender, Event. Args e) { if (dia. CD. Show. Dialog() == Dialog. Result. OK) { lbl. Change. My. Color. Back. Color = dia. CD. Color; lbl. Change. My. Color. Text = "Color Changed!!"; } } Unit: 5 – Windows Forms and Controls in details 82 Darshan Institute of Engineering & Technology

Output – Color. Dialog Unit: 5 – Windows Forms and Controls in details 83

Output – Color. Dialog Unit: 5 – Windows Forms and Controls in details 83 Darshan Institute of Engineering & Technology

Folder. Browser. Dialog Control § As name indicates, the Folder. Browser. Dialog control opens

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

Properties of Folder. Browser. Dialog Event Description Retrieves or sets the description text displayed

Properties of Folder. Browser. Dialog Event Description Retrieves or sets the description text displayed above the Tree. View control in the dialog box. Root. Folder Retrieves or sets the root folder where the browsing starts. Selected. Path Retrieves or sets the path selected by the user. Show. New. Folder. Button 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 – Folder. Browser. Dialog § Create a from with a label control, textbox

Example – Folder. Browser. Dialog § Create a from with a label control, textbox control and a button control. § Drag and drop a Folder. Browser. Dialog control on your form. § When user click on a button, Folder. Browser. Dialog 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 – Folder. Browser. Dialog using System; using System. Component. Model; using System. Windows.

Example – Folder. Browser. Dialog using System; using System. Component. Model; using System. Windows. Forms; namespace Demo_Windows { public partial class Dialog. Box. Demo : Form { public Dialog. Box. Demo() { Initialize. Component(); } // Browse Button Click Event private void btn. Browse_Click(object sender, Event. Args e) { Folder. Browser. Dialog fbd = new Folder. Browser. Dialog(); fbd. Root. Folder = Environment. Special. Folder. My. Computer; fbd. Selected. Path = "D: \"; fbd. Description = "Select Path"; if (fbd. Show. Dialog() == Dialog. Result. OK) { txt. Path. Text = fbd. Selected. Path; } } Unit: 5 – Windows Forms and Controls in details 87 Darshan Institute of Engineering & Technology

Output – Folder. Browser. Dialog Unit: 5 – Windows Forms and Controls in details

Output – Folder. Browser. Dialog Unit: 5 – Windows Forms and Controls in details 88 Darshan Institute of Engineering & Technology

Font. Dialog Control § The font dialog box is use to select the font,

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

Properties of Font. Dialog Event Description Font Retrieves or sets the selected font. Font.

Properties of Font. Dialog Event Description Font Retrieves or sets the selected font. Font. Must. Exist 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. Max. Size Retrieves or sets the maximum font size a user can select. The default value for the font size is 0. Min. Size Retrieves or sets the minimum font size a user can select. Show. Apply Retrieves or sets a value specifying whether or not the dialog box contains an Apply button. Show. Color Retrieves or sets a value specifying whether or not the dialog box displays the color choice. Show. Help 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 - Font. Dialog § Create a from with a label control and a

Example - Font. Dialog § Create a from with a label control and a button control. § Drag and drop a Font. Dialog control on your form. § When user click on a button, Font. Dialog 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 – Font. Dialog Cont. . using System; using System. Component. Model; using System.

Example – Font. Dialog Cont. . using System; using System. Component. Model; using System. Windows. Forms; namespace Demo_Windows { public partial class Font. Dialog : Form { public Font. Dialog() { Initialize. Component(); } private void btn. Change. Font_Click(object sender, Event. Args e) { if (font. Dialog 1. Show. Dialog() == Dialog. Result. OK) { lbl. Change. My. Font = font. Dialog 1. Font; lbl. Change. My. Font. Fore. Color = font. Dialog 1. Color; lbl. Change. My. Font. Text = "Font Changed. "; } } Unit: 5 – Windows Forms and Controls in details 92 Darshan Institute of Engineering & Technology

Output – Font. Dialog Unit: 5 – Windows Forms and Controls in details 93

Output – Font. Dialog Unit: 5 – Windows Forms and Controls in details 93 Darshan Institute of Engineering & Technology

Open. File. Dialog Control § This control allows user to select a file and

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

Properties of Open. File. Dialog Event Description Check. File. Exists Retrieves or sets a

Properties of Open. File. Dialog Event Description Check. File. Exists Retrieves or sets a value indicating whether the dialog box diaplays a warning if the user specifies a non-existent file. Multi. Select Retrieves or sets a value specifying whether or not the dialog box allows multiple file selections. Read. Only. Checked Retrieves or sets a value specifying whether or not the read only checkbox is selected on the dialog box. Safe. File. Name Retrieves the file name and extension for the selected file in the dialog box. The file name does not include the path. Show. Read. Only 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 - Open. File. Dialog § Create a from with a label control and

Example - Open. File. Dialog § Create a from with a label control and a button control. § Drag and drop a Open. File. Dialog control on your form. § When user click on a button, Open. File. Dialog 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 – Open. File. Dialog Cont. . using System; using System. Component. Model; using

Example – Open. File. Dialog Cont. . using System; using System. Component. Model; using System. Windows. Forms; namespace Demo_Windows { public partial class Open. File. Dialog : Form { public Open. File. Dialog() { Initialize. Component(); } private void btn. Set. File. Name_Click( object sender, Event. Args e) { open. File. Dialog 1. Initial. Directory = @"C: "; open. File. Dialog 1. Title = "Select Files"; open. File. Dialog 1. Check. File. Exists = true; open. File. Dialog 1. Check. Path. Exists = true; open. File. Dialog 1. Default. Ext = "jpg"; open. File. Dialog 1. Filter = "Image files (*. jpg)|*. jpg|All files (*. *)|*. *" ; if (open. File. Dialog 1. Show. Dialog() == Dialog. Result. OK) { lbl. File. Name. Text = open. File. Dialog 1. File. Name; } } Unit: 5 – Windows Forms and Controls in details 97 Darshan Institute of Engineering & Technology

Output – Open. File. Dialog Unit: 5 – Windows Forms and Controls in details

Output – Open. File. Dialog Unit: 5 – Windows Forms and Controls in details 98 Darshan Institute of Engineering & Technology

Save. File. Dialog Control § The Save. File. Dialog control supports the Save As

Save. File. Dialog Control § The Save. File. Dialog 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. Save. File. Dialog Unit: 5 – Windows Forms and Controls in details 99 Darshan Institute of Engineering & Technology

Properties of Save. File. Dialog Event Description Create. Prompt Retrieves or sets a value

Properties of Save. File. Dialog Event Description Create. Prompt 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. Overwrite. Prompt 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 - Save. File. Dialog § Create a from with a label control, a

Example - Save. File. Dialog § Create a from with a label control, a button control and a text box control. § Drag and drop a Save. File. Dialog control on your form. § When user click on a button, Save. File. Dialog 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 – Save. File. Dialog Cont. . using System; using System. Component. Model; using

Example – Save. File. Dialog Cont. . using System; using System. Component. Model; using System. Windows. Forms; using System. IO; namespace Demo_Windows { public partial class Save. File. Dialog : Form { public Save. File. Dialog() { Initialize. Component(); } private void btn. Save_Click(object sender, Event. Args e) { save. File. Dialog 1. Filter = "Text Files (*. txt)|*. txt*"; if (save. File. Dialog 1. Show. Dialog() == Dialog. Result. OK) { File. Write. All. Text(save. File. Dialog 1. File. Name, txt. Path. Text); } } Unit: 5 – Windows Forms and Controls in details 102 Darshan Institute of Engineering & Technology

Output – Save. File. Dialog Unit: 5 – Windows Forms and Controls in details

Output – Save. File. Dialog Unit: 5 – Windows Forms and Controls in details 103 Darshan Institute of Engineering & Technology

Tool. Tip § The Tooltip control is used to display a small window with

Tool. Tip § 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. Tool. Tip § Properties for a Tool. Tip control are as follows. Properties Description Active Specifies whether or not the tooltip control is active Automatic. Delay 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 - Tool. Tip § Create a from with a button control. § Drag

Example - Tool. Tip § Create a from with a button control. § Drag and drop a Tool. Tip control on your form. § When user hover on a button, Tool. Tip 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

Cont. . using System; using System. Windows. Forms; namespace Demo_Windows { public partial class Tooltip_Demo : Form { public Tooltip_Demo() { Initialize. Component(); } private void Tooltip_Demo_Load(object sender, Event. Args e) { Tool. Tip tool. Tip = new Tool. Tip(); tool. Tip. Show. Always = true; tool. Tip. Set. Tool. Tip(btn. Save, "Click Here to Save Data. "); } } } Unit: 5 – Windows Forms and Controls in details 106 Darshan Institute of Engineering & Technology