1 of 100

Windows and Web Applications

Chapter 7

2 of 100

Introduction

  • C# and .NET Provides extensive support for building windows applications.
  • All windows applications provide a GUI to their users and respond to user interfaces .
  • By providing different applications with a consistent set of intuitive user interface components ,GUIs allows users to spend less time using the program in a predictive manner .

3 of 100

4 of 100

GUI Components

  • GUIs are built from GUI components
  • A GUI component is an object with which the user interacts via the mouse or keyboard.

5 of 100

6 of 100

WinForms

  • Windows Forms (also Called WinForms) create GUIs for programs .
  • A form is a graphical element that appears on the desktop . A form can be a dialog, a window or an MDI window.

7 of 100

ToolBox in Microsoft Visual Studio.NET

8 of 100

9 of 100

10 of 100

11 of 100

12 of 100

13 of 100

14 of 100

15 of 100

Important Controls

16 of 100

1.Button

  • Raises an event when the user clicks it .

17 of 100

2.Check Box

  • Enables the user to select or clear the option

18 of 100

3.Check List Box

  • Displays a list of items with a check box on the left side of the item.

19 of 100

4.ComboBox

  • Displays an editable text box with a drop down list of permitted list .

20 of 100

5.DateTimePicker

  • Enables the user to select a date and time and to display that data and time in a specified time.

21 of 100

6.Label

  • Provides runtime information or descriptive text for a control.

22 of 100

7.Link Label

  • Displays a label control that supports hyperlink functionality ,formatting and control.

23 of 100

8.List Box

  • Displays a list from which the user can select item.

24 of 100

9.List View

  • Displays a collection of items in one of five different views (Large Icon ,Details ,Small Icon ,List ,Tile)

25 of 100

10.Month Calendar

  • Displays a Monthly calendar from which a user can select a date.

26 of 100

11.Radio Button

  • Displays a notification icon in the notification area on the right side of the windows task bar , during runtime.

27 of 100

12.Text Box

  • Enables the user to enter the text and provides multiline editing password character masking.

28 of 100

Forms

  • The Form acts as container for components and controls . Controls must be added to the form using code.
  • When the user interacts with a control by using the mouse or keyword events are generated and event handlers process those events.

29 of 100

System.Windows.Forms

  • Each .Net FrameWork class (Form , Component and Control ) is in the System.Windows.Forms namespace.
  • System.Windows.Forms.Form is a class for forms.
  • System.Windows.Forms.Button

30 of 100

Form Properties

  • Accept Button – Which button will be clicked when enter is pressed.
  • AutoScroll – Whether scrollbar appear when needed.
  • Cancel Button – Button that is clicked when the Escape Key is pressed.
  • FormBorderStyle – Border of the form (Eg.None , Single , 3D , Sizable)
  • Font – Font of the text displayed on the form as well as the different font of the controls added to the form
  • Text – Text in the forms title bar.

31 of 100

Forms Methods

  • Close – Closes Forms and releases all resources. A closed form cannot be reopened.
  • Hide – Hides(Does not release resources)
  • Show – Displays a hidden form.

32 of 100

Event Handling Model

  • GUIs are event driven (i.e. they generates events when the program’s user interact with the GUI)
  • Typical interaction include moving the mouse ,clicking the mouse , clicking a button , typing in a textbox , selecting an item from a menu and closing a window.
  • EventHandlers are methods that process event and performs tasks.

33 of 100

Example

  • Consider a form that changes color when a button is clicked . When clicked the button generates an event and passes it to the event handler and the event handler code changes the forms color.
  • Each control that can generate events has an associated delegate that defines the signature for that control’s event handler.
  • Event delegates are multicast - they contain lists of method reference. Each method must have the same signature.

34 of 100

35 of 100

Basic Event Handling

  • In most cases we do not have to create our own events .
  • Instead we can handle the events generated by .NET controls such as button and text boxes.
  • In this example the form will take some action when clicked .Double click the click event in the properties window to create an empty event handler in the program code.

36 of 100

private void FormName_Click (Object sender,System.EventArgs e) { }

This is the method that will be called when the form is clicked .

37 of 100

private void FormName_Click (Object sender , System.EventArgs e)

{ MessageBox.Show(“Form was pressed “);

}

38 of 100

using System;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Windows.Forms;

Public dlass MyForm : System.Windows.Forms.Form

{ private System.ComponentModel.Container Components = null ;

static void Main()

{ Application.Run(new MyForm());

}

private void MyForm_Click (Object sender , System.EventsArgs e)

{ MessageBox.Show(“Form was Pressed”);

}

}

39 of 100

void ControlName _ EventName (Object sender ,EventArgs e)

{

event handling code ;

}

40 of 100

Registering an event

41 of 100

Control Common Properties

42 of 100

Control Common Methods

43 of 100

Anchoring

44 of 100

Anchoring ----

45 of 100

Docking Demonstration

46 of 100

Control Layout Properties

47 of 100

using System;

using System.Window.Forms;

using System.Drawing;

namespace Ex1

{ class Test

{ static void Main() { Application.Run(new MyWindow()); }

class MyWindow:Forms

{ public MyWindow() : base()

{ this.Text = “My First windows Application “;

this.size = new size(300,300);

label lblGreeting = new Label();

lblGreeting.Text = “HellWinForm “;

lblGreeting.Location = new Point(100,100);

this.Controls.Add (lblGreeting);

}

}

}

48 of 100

namespace Ex2

{ class Test

{ static void Main() { Application.Run(new MyWindow()); }

class MyWindow:Forms

{ public MyWindow() : base()

{ this.Text = “My First windows Application “;

this.size = new size(300,300);

this.StartPosition = FormStartPosition.CenterScreen;

//label

label lblGreeting = new Label();

lblGreeting.Text = “HellWinForm “;

lblGreeting.Location = new Point(100,100);

//Button

Button btnExit = new Button();

btnExit.Text = “Exit”;

btnExit.Location = new Point(180,180);

btnExit.Size = new Size(80,30);

btnExit.Click + = new EventHandler(BtnExitOnClick);

this.Controls.AddRange(new Control[] { lblGreeting ,btnExit});

}

public void BtnExitOnClick(Object sender ,EventArgs e)

{ Application.Exit();

}

} }

49 of 100

Labels ,Text Boxes and Buttons

  • Labels provide text instructions or information about the program.
  • Labels are defined with class Label, which derives from class Control.
  • A Label displays read-only text, or text that the user cannot modify.
  • Once labels are created, programs rarely change their contents.

50 of 100

Label Properties

51 of 100

Text Box

  • A textbox (class TextBox) is an area in which text can be either input by the user from the keyboard or displayed.
  • A password textbox is a TextBox that hides what the user entered. As the user types in characters, the password textbox displays only a certain character (usually *).
  • Altering the PasswordChar property of a textbox makes it a password textbox and sets the appropriate character to be displayed.
  • Deleting the value of PasswordChar in the Properties window sets the textbox back to a regular textbox.

52 of 100

53 of 100

Button

  • A button is a control that the user clicks to trigger a specific action. A program can use several other types of buttons, such as checkboxes and radio buttons.
  • All the button types are derived from ButtonBase (namespace System.Windows.Forms ) , which defines common button features.

54 of 100

55 of 100

56 of 100

57 of 100

58 of 100

59 of 100

60 of 100

61 of 100

Group Boxes and Panels

  • GroupBoxes and Panels arrange components on a GUI. For example, buttons related to a particular task can be placed inside a GroupBox or Panel inside the Visual Studio .NET form designer. All these buttons move together when the GroupBox or Panel is moved
  • The main difference between the two classes is that GroupBoxes can display a caption, and Panels can have scrollbars.
  • The scrollbars allow the user to view additional controls inside the Panel by scrolling the visible area.
  • GroupBoxes have thin borders by default, but Panels can be set to have borders by changing their BorderStyle property.

62 of 100

63 of 100

  • To create a GroupBox, drag it from the toolbar and place it on a form.
  • Create new controls and place them inside the GroupBox, causing them to become part of this class.
  • These controls are added to the GroupBox’s Controls property. The GroupBox’s Text property determines its caption.

64 of 100

65 of 100

  • To create a Panel, drag it onto the form and add components to it. To enable the scrollbars, set the Panel’s AutoScroll property to true.
  • These scrollbars then can be used to view all the components in the Panel (both when running and designing the form). This allows the programmer to see the GUI exactly as it appears to the client.

66 of 100

67 of 100

68 of 100

69 of 100

70 of 100

71 of 100

CheckBoxes and RadioButtons

  • Visual C# has two types of state buttons—CheckBox and RadioButton—that can be in the on/off or true/false state.

  • Classes CheckBox and RadioButton are derived from class ButtonBase.

  • A RadioButton is different from a CheckBox in that there are normally several RadioButtons grouped together, and only one of the RadioButtons in the group can be selected (true) at any time.

  • A checkbox is a small white square that can be blank or contain a checkmark. When a checkbox is selected, a black checkmark appears in the box. There are no restrictions on how many checkboxes are used:

Any number may be selected at a time. The text that appears alongside a checkbox is referred to as the checkbox label.

72 of 100

73 of 100

74 of 100

75 of 100

76 of 100

77 of 100

78 of 100

79 of 100

80 of 100

81 of 100

82 of 100

83 of 100

84 of 100

85 of 100

PictureBoxes

  • A picture box (class PictureBox) displays an image. The image, set by an object of class Image, can be in a bitmap (.bmp), .gif, .jpg, icon or metafile format.
  • GIF (Graphics Interchange Format) and JPEG (Joint Photographic Expert Group) files are widely used file formats.

86 of 100

87 of 100

88 of 100

89 of 100

90 of 100

Mouse Event Handling

  • This section explains how to handle mouse events, such as clicks, presses and moves. Mouse events are generated when the mouse interacts with a control.
  • They can be handled for any GUI control that derives from class System.Windows.Forms.Control.
  • Mouse event information is passed using class MouseEventArgs, and the delegate to create the mouse event handlers is MouseEventHandler.
  • Each mouse event-handling method must take an object and a MouseEventArgs object as arguments.

91 of 100

92 of 100

93 of 100

94 of 100

95 of 100

Keyboard Event Handling

  • Key events are generated when keys on the keyboard are pressed and released.
  • These events can be handled by any control that inherits from System.Windows.Forms.Control.
  • There are two types of key events. The first is event Key Press, which fires when a key representing an ASCII character is pressed (determined by KeyPressEventArgs property Key Char).
  • ASCII is a 128-character set of alphanumeric symbols.

96 of 100

97 of 100

98 of 100

99 of 100

100 of 100