You are on page 1of 27

WindowsForm

Visual Studio and Windows Forms


• Windows Forms applications can be used for
stand-alone productivity applications, office
desktop applications, or the front end to an
application with n-tier architecture
• Windows Forms applications helps building GUI
application
• When building Windows Forms applications in
Visual Studio, developers have access to a rich
set of controls for creating powerful and intuitive
user interfaces
• Visual Studio is the main development
environment where C# applications are
developed in practice.
Hierarchy
System.Object

System.MarshallByRefObject

System.ComponentModel.Component

System.Windows.Forms.Control
System.Windows.Forms.Control

System.Windows.Forms.ButtonBase

System.Windows.Forms.ScrollableControl

System.Windows.Forms.TextBoxBase

DataGrid,DataGridView,DataTimePicker,
GroupBox,Label,ListControl,ListView,
MidClient,MonthCalendar,PictureBox,
PrinterPreviewControl,ProgressBar,
ScrollBar,splitter,StatusBarTabControl,
ToolBar,TrackBar,TreeView
System.Windows.Forms.ButtonBase

System.Windows.Form.RadioButton
System.Windows.Form.Button
System.Windows.Form.CheckBox

System.Windows.Form.ScrollableControl

System.Windows.Forms.ContainerControl

System.Windows.Forms.Form
System.Windows.Form.RaftingContainer
System.Windows.Form.PropertyGrid
System.Windows.FormUpDownBase
System.Windows.Form.SplitContainer
System.Windows.Form.UserControl

System.Windows.Forms.Panel
System.Windows.Form.ToolStrip
System.Windows.Form.ActivDocumentHost
System.Windows.Form.TextBoxBase

System.Windows.Forms.TextBox
System.Windows.Form.RichTextBox
Creating a project

Click OK
What gets created…

This is what you see

•One file with a class where we have Main


•Two files for the Form
Changing Form Properties
Change the background color, title and form name through the
property box.
Peek at the code
• Program.cs with Main().
Form files
• Two files are generated for the Form.
• Form1.cs
• Form1.Designer.cs
• Visual Studio takes advantage of the partial
class feature of the framework and separates all
of the designer-generated code into a separate
file called Form1.Designer.cs
• The code in the Designer.cs should not be
edited directly.
•Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WApp1
{
public partial class EmployeeForm : Form
{
public EmployeeForm()
{
InitializeComponent();
}
}
}
namespace WApp1
{ •Form1.Designer.cs
partial class EmployeeForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code


/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// EmployeeForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.HotTrack;
this.ClientSize = new System.Drawing.Size(481, 415);
this.Name = "EmployeeForm";
this.Text = "Employee Form";
this.ResumeLayout(false);

#endregion
}
}
System.Windows.Forms.Form
• Form class
• represents a window within an application including
dialog boxes, modeless windows, and Multiple
Document Interface (MDI) client and parent windows.
• derives from ContainerControl Class which means
they are special type of controls that can function as a
container for other controls.
• Examples of some of the methods of a
ContainerControl Class (as well as the Form) are
• Dispose(): Releases the resources used by the
Component.
• Refresh() :Forces the control to invalidate its client area
and immediately redraw itself and any child controls
• And all the properties that appears on the form property box
Controls
• data entry controls
• TextBox
• ComboBox
• Display controls
• Label
• ListView
• Command Controls
• Button
• ToolBar
Components
• Both Form and Controls derive from the
Control class.
• There are some ‘components’ that do not derive
from the Control class but still provide visual
features to a Windows-based application.
• Examples
• ToolTip and ErrorProvider, extend the
capabilities or provide information to the user.
• Menu, MenuItem, and ContextMenu, provide
the ability display menus to the user to invoke
commands within an application
• The Help and HelpProvider classes enable
you to display help information to the user of
your applications.
Common Dialog Boxes
• The OpenFileDialog and SaveFileDialog
classes provide the functionality to display a
dialog box that allows the user to browse to and
enter the name of a file to open or save.
• The FontDialog class displays a dialog box to
change elements of the Font object used by
your application
• The PageSetupDialog,
PrintPreviewDialog, and PrintDialog
classes display dialog boxes that allow the user
to control aspects of printing documents.
• MessageBox class for displaying a message box
that can display and retrieve data from the user
Does not fall into ‘common’ dialog box
Creating a forms with controls

View Toolbar
Drag and Drop Controls
Simple Example
• We will enter Name and ID in two text boxes and
display them on the screen when ok button is
clicked.
• Drag three Labels and two TextBoxes.
• Drag a button.
• Change the title of labels to Name and ID. Make
the third labels ‘visible’ property as false. Change
the title of the Button to OK.
• Change the name of the TextBoxes – one to
NameTxt and other to IDTxt.
• Double click on the button to add code on what
will happen when it is clicked.
using System;
using System.Collections.Generic;

namespace WApp1{
public partial class EmployeeForm : Form
{
public EmployeeForm() {
InitializeComponent();
}
private void OKBtn_Click(object sender, You add this
EventArgs e) {
ResultL.Text = NameTxt.Text + "( ID )" +
IDTxt.Text;
ResultL.Visible = true;
}
}} TIP : After typing a part of the control name, you could press CTRL-SPACE
to invoke the ‘CODE ASSIST’.
Try it out
• Add a combo box called hobbies with ‘Reading’,
‘Writing’ , ‘Drawing’ and ‘Programming’
• On clicking ok the selected text must appear
concatenated in the result label.

• Similarly try to work with the following controls


• CheckBox
• RadioButton
• ListBox
• Checkout the DateTimePicker!
Creating Menu

Create two menus View Result and Refresh


Double click and write the same code as that of OK button
to View Result and
NameTxt.Text = "";
IDTxt.Text = "";
into Refresh.
Attaching a color dialog
• Drag a ColorDialog.
• Create another button with title ‘Show
Color’.
• Double click and the following code
• colorDialog1.ShowDialog();
• Run
• On clicking ‘Show Color’ it shows the Note that since the Menu
color dialog but on selecting the color and ColorDialog are
components they appear
and clicking ok nothing happens. in the component tray
• So how to solve this problem? under the Designer.

Extremely simple : add another line after


colorDialog1.ShowDialog();
this.BackColor = colorDialog1.Color;
Validating a form using ErrorProvider
• Drag an ErrorProvider.
• On OK add the following code.

errorProvider1.Clear();
if (NameTxt.Text.Length == 0 )
errorProvider1.SetError(NameTxt, "Must enter a value");
try {
if (IDTxt.Text.Length > 0 && Convert.ToInt32(IDTxt.Text) <
1)
errorProvider1.SetError(IDTxt, "Must be a non-zero +ve
number");
} catch (Exception ex) {
errorProvider1.SetError(IDTxt, "Must be a non-zero +ve
number");
}
ResultL.Text = NameTxt.Text + "( ID:" + IDTxt.Text + ")
Hobby:" + comboBox1.Text;
ResultL.Visible = true;
On Executing….
Creating new form

You might also like