C# Unit4 Part1 GUI Programming(highlight)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Unit 3 – GUI using Windows Forms and Database Programming

GUI Using Windows Forms


Form
Forms are the basic building blocks of C# .NET GUI applications. It is a window or screen which is initially blank
and on which controls can be placed. Form itself is a control which has its own properties, events, and methods.

When a programmer wants to design a Windows form application, the user defined Form 1 class always inherits
from Form base class.

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}
}

Controls
C# .NET supports various built-in controls that help to create applications. Controls are the objects that can be drag
and dropped on the form. The properties of these controls can be changed or set to suit the needs of the user. Each
control can have a programming code attached to it.

Example: Textbox, Label, Button etc

Event
Programmer creates GUI and writes the code to describe what happens when user interacts with GUI such as click,
double click, mouse movement etc. These interactions are called as events.

Events are the actions performed by the user while interacting with the application.

The code that responds to these events are called event handler and such kind of programming is called Event
Driven Programming.

Event Driven Programming


It is a programming paradigm in which the flow of program is determined by events such as user actions, sensor
output or messages from the programs.

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 1


Unit 3 – GUI using Windows Forms and Database Programming

Form Object
The most basic object in C# .NET is the form object. The form represents a window or a dialog box that makes up
an application user interface. The form is made up of various elements such as title bar, control box and border.
The form class can be used to create standard modeless windows or modal windows. Also, SDI and MDI forms
can be designed.

Important Properties of Form Object


1. BackColor : sets a background of a form
2. Name : sets the name of form (name of object)
3. Text : sets the text/title of the form
4. ControlBox : it is the boolean property. If set to true, the control menu buttons will be visible else it will be
invisible
5. ForeColor : sets the color of the text that is displayed on the form
6. Size : specifies the form size in pixels
7. Icon : determines the icon to be displayed on the taskbar
8. Windows State : Determines how the form will be displayed at a start-up
9. Example : normal, minimise or maximise
10. StartPosition : Determines the position of a form when it first appears
11. isMdiContainer : Determines whether the form is an MDI container

Important Methods of Form Object


1. Activate() : this method activates the form and gives its focus
2. Show () : this method shows the form on the screen
3. Close () : this method closes the form

Important Events of Form Object


1. Activated : occurs when the form is activated
2. Load : occurs whenever user loads the form
3. Resize : occurs when the form resized

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 2


Unit 3 – GUI using Windows Forms and Database Programming

TextBox Control
It represents a windows textbox. This object is commonly used to accept user inputs.

Important Properties of TextBox Control


1. Text : sets the text associated with this control
2. Multiline : It is the boolean property. If set to true it allows the text to span across multiple lines
3. ReadOnly : It is the boolean property. If set to true the text in the text box cannot be changed
4. PasswordChar : set the characters used to mask characters of the password in the single line textbox
5. CharacterCasing : set whether the textbox control modifies the case of the characters as they are typed
6. example : normal / upper case / lower case
7. TextAlign : set how the text is aligned inside the text box
8. example : left / right / Centre

Important Methods of TextBox Control


1. Focus () : sets input focus on the text box
2. Hide () : hides the text box
3. Show () : shows the text box

Important Events of TextBox Control


1. Click : occurs when the textbox is clicked
2. DoubleClick : occurs when textbox is double clicked
3. TextChanged : occurs every time when the user types something or changes the text inside the textbox
4. TextAlignChanged : occurs when value of text align property is changed
5. KeyPress : occurs when user presses the key

Label Control
Represents windows standard label. It is the control that displays read only text. Hence labels are mainly used to
display output on the window.

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 3


Unit 3 – GUI using Windows Forms and Database Programming

Important Properties of Label Control


1. AutoSize : it is a boolean property. If set it to true the label automatically expands to display complete text
2. Text : determines the text to be displayed on the label
3. BorderStyle : sets the border style for the label

Important Methods of Label Control


1. Focus () : sets input focus on the label
2. Hide () : hides the label
3. Show () : shows the label

Important Events of Label Control


1. Click : occurs when the label is clicked
2. DoubleClick : occurs when label is double clicked
3. AutoSizeChanged : occurs when the value of the auto size property changes

Button Control
Represents the windows standard button.

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 4


Unit 3 – GUI using Windows Forms and Database Programming

Important Properties of Button Control


1. Text : sets the text/title of the form
2. BackColor : sets the background color
3. ForeColor : sets the color of the text that is displayed on the form
4. Size : specifies the form size in pixels

Important Methods of Button Control


1. Focus() : Sets the input focus to the control
2. Hide() : Hides the button
3. Show() : Displays the button

Important Events of Button Control


1. Click : Occurs when button is clicked
2. DoubleClick : occurs when label is double clicked
3. Resize : occurs when the button is resized

CheckBox Control
CheckBox provides an option to select. It is a control that is checked when selected or uncheck when not selected.
It is used when we wish to give two-way choices to the user such as true or false, yes or no etc.

Important Properties of Checkbox


1. Text : determines / set the title that appears on the checkbox
2. Checked : determines the current state of the checkbox. if the checkbox is selected the value is true
otherwise it is false
3. CheckState : gets / sets the state of the checkbox. the possible values are
a. Checked : a check appears in the checkbox
b. Unchecked : new check appears in the check box
c. Intermediate : it appears in the check box with a grey background (or filled square)

Important Events of Checkbox


1. CheckChange : occurs when the checkbox property changes

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 5


Unit 3 – GUI using Windows Forms and Database Programming

2. Click : occurs when user clicks on the checkbox

RadioButton Control
A RadioButton is similar to checkbox control. That is, it provides the user with an option on form. Unlike a
checkbox control, if there are more than one radio buttons on the form then the user can select only one at a time.

Important Properties of RadioButton


1. Text : determines / sets the text that appears on a title of the radio button
2. Checked : indicates whether the radio button is checked or unchecked. It has a boolean value

Important Events of RadioButton


1. CheckChange : occurs when the checked property changes

ListBox Control
A ListBox is a control that displays items in the form of a list. The programmers initialise the list box either at the
design time or either at the time of coding.

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 6


Unit 3 – GUI using Windows Forms and Database Programming

The purpose of ListBox is to let the user select the value from the list rather than type the values. The vertical and
horizontal scroll bars appear in the list box if the height and width of list box are not sufficient to display all the
items.

When the user selects an item from list box, the following actions happen:

• the item changes color to highlight section


• C# .NET copies the selected item from the items collection of the list box into the
SelectedItem property of the list box

Important Properties of ListBox


1. SelectedIndex : returns numeric value that gives the index of the selected items. It starts from zero. If no
item is selected the selected index value is -1
2. SelectedItem : returns the selected object corresponding to the item
3. SelectionMode : gets or sets the method in which items are selected in list box. That is, one, Multi simple
and multi extended. By default selection mode will be one.

Important Methods of ListBox


1. Items.Add() : used to add items to the items collection of the list box
2. Items.Remove() : Used to remove an item from the items collection of list box
3. FindString() / FindStringExact : it enables the user to search for an item in the list that contains specific
search string

DateTimePicker Control
The DateTimePicker is a dropdown list box at the top and the month calendar control appears in it. If you click on
arrow in DateTimePicker, it displays a month calendar. You can make selection of a date by clicking on the
calendar.

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 7


Unit 3 – GUI using Windows Forms and Database Programming

Important Properties of DateTimePicker


1. Value : display current date and time for this control
2. MaxDate : sets maximum selectable date and time
3. MinDate : sets minimum selectable date and time
4. Format : gets / sets format of the date and time. possible values are :
a. Long : displays date-time in long format set by operating system
b. Short : displays date-time value in short format set by operating system
c. Time : displays date-time value in time format set by operating system
d. custom : displays date-time value in custom format

Important Events of DateTimePicker


1. ValueChanged : occurs when the new value of date and time is selected

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 8


Unit 3 – GUI using Windows Forms and Database Programming

Menu’s (MenuStrip)
An important feature of user interface in a windows-based application is the menu. The menu is created with the
MenuStrip object. Menu items and options under each menu items can be added as per the user requirements.
Shortcuts and hotkeys also can be designed. MenuStrip items functions as a button control in the form.

Important Properties of MenuStrip


1. Check : represents weather check mark appears on menu item
2. Text : sets the text of menu item
3. Shortcut : sets the shortcut key connected with menu item
4. Enabled : it is boolean property, if set to true the menu item is enabled

ContextMenuStrip
It is the menu that opens on right click operation of a control or area in a window-based form. The
ContextMenuStrip items functions as a button control.

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 9


Unit 3 – GUI using Windows Forms and Database Programming

The ContextMenuStrip property of the Form has to be set to proper contexMenuStrip object to enable the right-
click option on the respective form.

Important Properties of ContextMenuStrip


1. Text : sets the text of menu item
2. Shortcut : sets the shortcut key connected with menu item
3. Enabled : it is boolean property, if set to true the menu item is enabled

ToolStrip
ToolStrip is a container for ToolStripItem elements. Each individual element on the ToolStrip is a ToolStripItem
that manages the layout and event model for the type it contains.

The ToolStrip contains the following controls:

• Button
• Label
• SplitButton
• DropDownButton
• Separator

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 10


Unit 3 – GUI using Windows Forms and Database Programming

• ComboBox
• TextBox
• PreogressBar

Important Properties of ToolStrip


1. BackColor : Specifies the background color of the ToolStrip
2. AutoSize : Specifies whether a control will automatically size itself to fit its contents.
3. RightToLeft : Indicates whether the components of ToolStrip are placed right-to-left
4. Items : Collection of items to display on the ToolStrip
5. LayoutStyle : Specifies the layout orientation of the ToolStrip such as StackWithOverflow,
HorizontalStackWithOverflow, VerticalStackWithOverflow, Flow, Table

Important Methods of ToolStrip


1. ItemClicked() : Occurs when the ToolStripItem is clicked.
2. ItemRemoved() : Occurrs when a ToolStripItem is removed from the
ToolStripItemCollection.
3. GetItemAt() : Returns the item located at a specific location.

GDI
The common language runtime uses an advanced implementation of the Windows Graphics Device Interface
(GDI) called GDI+. With GDI+ you can create graphics, draw text, and manipulate graphical images as objects.
GDI+ is used to render graphical images on Windows Forms and controls.

The services of GDI+ are exposed through a set of managed classes. Following are the managed classes and their
purposes:
Class name Purpose
System.Drawing Provides access to GDI+ basic graphics functionality.
System.Drawing.Drawing2D Provides advanced two-dimensional and vector graphics

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 11


Unit 3 – GUI using Windows Forms and Database Programming

functionality.
System.Drawing.Imaging Provides advanced GDI+ imaging functionality.
System.Drawing.Text Provides advanced GDI+ typography functionality.
System.Drawing.Printing Provides print-related services.

The Graphics class is at the core of GDI+ functionality; it is the class that actually draws lines, curves, figures,
images, and text.

GDI+ draws lines, rectangles, and other shapes on a coordinate system. You can choose from a variety of
coordinate systems, but the default coordinate system has the origin in the upper-left corner with the x-axis
pointing to the right and the y-axis pointing down. The unit of measure in the default coordinate system is the
pixel.

A computer monitor creates its display on a rectangular array of dots called picture elements or pixels. The number
of pixels that appear on the screen varies from one monitor to the next, and the number of pixels that appear on an
individual monitor can usually be configured to some extent by the user.

Constructing a Graphics Object


Graphics myGraphics = CreateGraphics();
example draws a line from the point (10, 10) to the
Constructing a Pen Object point (50, 50):
Pen myPen = new Pen(Color.Blue); myGraphics.DrawLine(myPen, 10, 10, 50, 50);

Drawing a Line
To draw a line, call the DrawLine method of the
Graphics object. The Pen object is passed as one of the
arguments to the DrawLine method. The following Drawing a Rectangle
Drawing rectangles with GDI+ is similar to drawing
lines. To draw a rectangle, you need a Graphics

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 12


Unit 3 – GUI using Windows Forms and Database Programming

object and a Pen object. The Graphics object provides myGraphics.DrawRectangle(myPen,


a DrawRectangle method, and the Pen object stores 100, 50, 80, 40);
attributes, such as line width and color. The Pen object
is passed as one of the arguments to the
DrawRectangle method. The following example draws
a rectangle with its upperleft corner at (100, 50), a
width of 80, and a height of 40:

myGraphics.DrawEllipse(myPen,
100, 50, 80, 40);
Drawing an Ellipse
To draw an ellipse, you need a Graphics object and a Pen
object. The Graphics object provides the DrawEllipse
method, and the Pen object stores attributes, such as
width and color, of the line used to render the ellipse.
The Pen object is passed as one of the arguments to the
DrawEllipse method. The remaining arguments passed
to the DrawEllipse method specify the bounding
rectangle for the ellipse. The following example draws
an ellipse; the bounding rectangle has a width of 80, a
height of 40, and an upper-left corner of (100, 50):

Drawing a Polygon
To draw a polygon, you need a Graphics object, a Pen object, and an array of Point (or PointF) objects. The
Graphics object provides the DrawPolygon method. The Pen object stores attributes, such as width and color, of
the line used to render the polygon, and the array of Point objects stores the points to be connected by straight
lines. The Pen object and the array of Point objects are passed as arguments to the DrawPolygon method. The
following example draws a threesided polygon. Note that there are only three points in myPointArray : (0, 0), (50,
30), and (30, 60). The DrawPolygon method automatically closes the polygon by drawing a line from (30, 60) back
to the starting point (0, 0).

Point[] myPointArray =
{
new Point(0, 0), new Point(50, 30), new
Point(30, 60)
};
myGraphics.DrawPolygon(myPen, myPointArray);

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 13


Unit 3 – GUI using Windows Forms and Database Programming

Dialog Box

A dialog box is a type of window used to enable common communication or dialog between a computer and its
users. They can be used for displaying messages, passwords, confirmation or deletion etc.

Dialog boxes can be of two types:

• Modal dialog box


• Modeless dialog box

Modal dialog box


A dialog box that temporarily halts the parent window (dialog) and the user cannot continue unless dialog has been
closed is called as modal dialog box.

Example : While saving a file if user gives an existing name then a warning is shown that file in the same name
exist and whether it must be overwritten. The parent dialog cannot be saved unless user clicks OK or Cancel
button.

The modal dialog box is displayed using ShowDialog() method.

Modeless dialog box


A dialog box that does not halt the parent window (dialog) and user can continue working on the parent window
(dialog) is called as modeless dialog box.

Example : In an MS Word, if user wants to Find and Replace a particular word , modeless dialog box is used. The
user can continue to work even if dialog box is open.

The modeless dialog box can be displayed using Show() method.

SDI and MDI


A Multiple Document Interface (MDI) programs can display multiple child windows inside them. This is in
contrast to Single Document Interface (SDI) applications, which can manipulate only one document at a time.
Visual Studio Environment is an example of MDI and notepad is an example of an SDI application.

Any windows can become an MDI parent, if you set the IsMdiContainer property to True.

KLS Gogte BCA – Belagavi SemV - .Net Using C# pg. 14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy