Module - 4 With Content
Module - 4 With Content
Prepared by
Mrs. Suma M G
Assistant Professor
Department of MCA
RNSIT
Bengaluru – 98.
Programming using C# Module – 4 GUI with Windows Forms and WPF
Chapter – 1
• When you drag items from the Toolbox onto the Form, Visual Studio generates code that creates the
object and sets its basic properties. This code is updated when the control or component’s properties are
modified in the IDE.
• Removing a control or component from the Form deletes the corresponding generated code.
• The IDE maintains the generated code in a separate file using partial classes – classes that are split among
multiple files and assembled into a single class by the compiler.
Active Windows:
When there are several windows on the screen, the active window
✓ is the frontmost and has a highlighted title bar.
✓ when the user clicks somewhere inside it, immediately that window have the focus.
Form properties,
Description
methods & an event
Closes a Form and releases all resources, such as the memory used for
Close
Methods
Load is displayed in the Visual Studio editor when you double click the Form
in the Visual Studio designer.
Closes a Form and releases all resources, such as the memory used for the
Close
Methods
• Docking attaches a control to a container such that the control stretches across an entire side or fills an
entire area.
✓ Docking allows a control to span an entire side (left, right, top or bottom) of its parent container or
to fill the entire container.
✓ When the parent control is resized, the docked control resizes as well.
✓ When the Form is resized, the Button is resized to the Form’s new width.
✓ Forms have a Padding property that specifies the distance between the docked controls and the
Form edges. This property specifies four values (one for each side),and each value is set to 0 by
default.
If true, the TextBox has a gray background, and its text cannot be
ReadOnly
edited. The default value is false.
For multiline textboxes, this property indicates which scrollbars
ScrollBars
appear (None—the default, Horizontal, Vertical or Both).
When true, the TextBox becomes a password TextBox, and the
UseSystemPasswordChar
systemspecified character masks each character the user types.
Text Text in the Form’s title bar.
Generated when the text changes in a TextBox (i.e., when the user
Event
using System;
using System.Windows.Forms;
namespace LabelTextBoxButtonTest{
public partial class LabelTextBoxButtonTestForm : Form{
public LabelTextBoxButtonTestForm(){
InitializeComponent();
} // end constructor
private void displayPasswordButton_Click(object sender, EventArgs e)
{
displayPasswordLabel.Text = inputPasswordTextBox.Text;
}
}
}
Example:
using System;
using System.Windows.Forms;
namespace GroupBoxPanelExample {
public partial class GroupBoxPanelExampleForm : Form{
public GroupBoxPanelExampleForm()
{
InitializeComponent();
}
// event handler for Hi Button
private void hiButton_Click( object sender, EventArgs e )
{// change text in Label
messageLabel.Text = "Hi pressed";
}
}
}
CheckBox:
• A CheckBox is a small square that either is blank or contains a check mark.
• When the user clicks a CheckBox to select it, a check mark appears in the box.
• If the user clicks the CheckBox again to deselect it, the check mark is removed.
• You can also configure a CheckBox to toggle between three states (checked, unchecked and
indeterminate) by setting its ThreeState property to true.
• Any number of CheckBoxes can be selected at a time.
Table 1.8: A list of common checkbox properties and events appears
Common
Description
Properties
By default, this property is set to Normal, and the CheckBox displays as a traditional
Appearance checkbox. If it’s set to Button, the CheckBox displays as a Button that looks pressed
when the CheckBox is checked.
Checked Indicates whether the CheckBox is checked (contains a check mark) or unchecked
(blank). This property returns a bool value. The default is false (unchecked).
Indicates whether the CheckBox is checked or unchecked with a value from the
CheckState CheckState enumeration (Checked, Unchecked or Indeterminate)
Text Specifies the text displayed to the right of the CheckBox.
When this property is true, the CheckBox has three states—checked, unchecked and
indeterminate.
ThreeState
By default, this property is false and the CheckBox has only two states—checked and
unchecked.
Common Events
✓ Generated when the Checked property changes.
✓ This is a CheckBox’s default event.
CheckedChanged
✓ When a user double clicks the CheckBox control in design view, an empty event
handler for this event is generated.
CheckStateChanged Generated when the CheckState property changes.
RadioButton:
✓ Radio buttons (defined with class RadioButton) are similar to CheckBoxes in that they also have two
states—selected and not selected (also called deselected).
✓ However, RadioButtons normally appear as a group, in which only one RadioButton can be selected at
a time.
✓ Selecting one RadioButton in the group forces all the others to be deselected.
✓ Therefore, RadioButtons are used to represent a set of mutually exclusive options (i.e., a set in which
multiple options cannot be selected at the same time).
1.8 ToolTips:
ToolTips serve as useful reminders for each toolbar icon’s functionality. ToolTips is used to remind users of
each control’s purpose.
Example: Microsoft Word has tooltips that help users determine the purpose of the application’s icons.
Table 1.9: Common properties and a common event of class ToolTip described below
Properties and Events Description
AutoPopDelay The amount of time (in milliseconds) that the tool tip appears while the
mouse is over a control.
InitialDelay The amount of time (in milliseconds) that a mouse must hover over a
control before a tool tip appears.
ReshowDelay The amount of time (in milliseconds) between which two different tool
tips appear (when the mouse is moved from one control to another).
Common Event Raised when the tool tip is displayed. This event allows programmers to
Draw modify the appearance of the tool tip.
The following figures shows how to create and set the tooltips controls:
i. We named the ToolTip component labelsToolTip.
Example:
using System;
using System.Text;
using System.Windows.Forms;
namespace Painter{
public partial class PainterForm : Form{
bool shouldPaint = false;
public PainterForm(){
InitailaizeComponnent();
}
private void PainterForm_MouseDown (object sender, MouseEventArgs e ){
shouldPaint = true;
}
private void PainterForm_MouseUp( object sender, MouseEventArgs e ){
shouldPaint = false;
}
private void PainterForm_MouseMove(object sender, MouseEventArgs e ){
if ( shouldPaint ) {
using ( Graphics graphics = CreateGraphics() ){
graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X,
e.Y, 4, 4 );
}
}
} }
To create a menu,
➢ open the Toolbox and drag a MenuStrip control onto the Form. This creates a menu bar across the top
of the Form (below the title bar) and places a MenuStrip icon in the component tray.
➢ To select the MenuStrip, click this icon. You can now use Design mode to create and edit menus for
your application.
➢ Menus, like other controls, have
properties and events, which can
be accessed through the Properties
window.
➢ To add menu items to the menu,
click the Type Here TextBox and
type the menu item’s name. This
action adds an entry to the menu of
type ToolStripMenuItem.
• The user can select a date from the currently displayed month or can use the provided arrows to navigate
to another month. When a date is selected, it is highlighted.
• Multiple dates can be selected by clicking dates on the calendar while holding down the Shift key.
• The default event for this control is the DateChanged event, which is generated when a new date is
selected.
• Properties are provided that allow you to modify the appearance of the calendar, how many dates can be
selected at once, and the minimum date and maximum date that may be selected.
Table 1.13: MonthCalendar properties and a common MonthCalendar event
using System;
using System.Windows.Forms;
namespace LinkLabelTest{
public partial class LinkLabelTestForm : Form{
public LinkLabelTestForm(){
InitializeComponent();
}
private void cDriveLinkLabel_LinkClicked( object sender,
LinkLabelLinkClickedEventArgs e) {
driveLinkLabel.LinkVisited = true;
System.Diagnostics.Process.Start( @"C:\" );
}
private void displaylabel_Click(object sender, EventArgs e){
// display the text that the user typed
displayPasswordLabel.Text = inputTextBox.Text;
}
}
}
using System;
using System.Windows.Forms;
namespace ListBoxTest {
public partial class ListBoxTestForm : Form{
public ListBoxTestForm (){
InitializeComponent();
}
1.19 TabControl:
✓ The TabControl creates tabbed windows.
✓ This enables you to specify more information in the same space
on a Form and group displayed data logically.
✓ TabControls contain TabPage objects, which are similar to
Panels and GroupBoxes in that TabPages also can contain
controls.
✓ You first add controls to the TabPage objects, then add the
TabPages to the TabControl.
myTabPage.Controls.Add( myControl );
✓ Only one TabPage is displayed at a time. myTabControl.TabPages.Add( myTabPage );
Table 1.20: List of common properties and Event of TabControl:
properties & event Description
ImageList Specifies images to be displayed on tabs.
ItemSize Specifies the tab size.
Multiline Indicates whether multiple rows of tabs can be displayed.
SelectedIndex Index of the selected TabPage.
SelectedTab The selected TabPage.
TabCount Returns the number of tab pages.
Returns the collection of TabPages within the TabControl as a
TabPages
TabControl.TabPageCollection.
Common Event
SelectedIndexChanged Generated when SelectedIndex changes
Introduction:
WPF is a UI framework, which helps you to create interactive client applications.
❖ It supports various application development features, such as controls, graphics, layout, and data
binding.
❖ WPF uses XAML language for application programming.
❖ Can use WPF for creating both standalone and browser-hosted applications.
The default view of the Calendar control We can change the Display mode to Year and Decade
looks generates by setting the property DisplayModes.
Year Decades
XAML Elements:
XAML based on XML, makes use of markup tags, elements, and attributes to define the UI of WPF applications.
The XAML elements directly correspond to various managed WPF classes, while the XAML attributes
correspond to the properties and events of the classes.
In WPF, the XAML elements are represented as a logical tree with several element nodes. Each element
corresponds to a tree node while attributes of the elements become the properties of the nodes. When you add
an XAML element within an existing element, it becomes the child element of the existing node. This kind of
hierarchical tree-like content model makes WPF applications extensible and easy to render.
When you create your new WPF project, you will encounter some of the XAML code by default in
MainWindow.xaml as shown below.
By default, there are two namespaces that are included in WPF applications. The namespaces are added by
using the xmlns declarations, which are provided through the xmlns attribute of the window or page elements
that as shown below:
▪ Maps the default XAML namespace for WPF client/framework
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
▪ XAML namespace for XAML language which maps it to x: prefix
xmlns:x = "http://schemas.microsoft.com/w infx/2006/xaml"
In WPF, you can also create your own namespace and use it in XAML files by adding an xmlns declaration in
the root element of these files.
Syntax:
xmlns:custom_prefix="clr-namespace:namespace_name;
assembly=assembly_name"
It shows how custom prefixes work in XAML markup. The prefix custom is defined in the root element tag,
and mapped to a specific assembly that is packaged and available with the app.
Markup Extension:
A markup extension is a means of setting the value of a property to an object reference. It also allows you to
delay the assignment of a value to a property until the runtime or bind a property to some value.
The markup extension classes are derived from the MarkupExtension class, which contains the
ProvideValue() method. This method is responsible for returning the object that is to be assigned to the
property after the evaluation of the markup extension. The object that is returned by the ProvideValue()
method depends on the input that you provide to the markup extension.