0% found this document useful (0 votes)
51 views9 pages

Windows Application

Uploaded by

p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views9 pages

Windows Application

Uploaded by

p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Windows Application

C# programmers have made extensive use of forms to build user


interfaces. Each time you create a Windows application, Visual Studio will
display a default blank form, onto which you can drag the controls onto
your applications main form and adjust their size and position.

The first step is to start a new project and build a form. Open your
Visual Studio and select File->New Project and from the new project
dialog box select Other Languages->Visual C# and select Windows Forms
Application. Enter a project name at the bottom of the dialouge box and
click OK button. The following picture shows how to create a new Form in
Visual Studio.

Select Windows Forms Application from New Project dialog box.


After selecting Windows Forms Application , you can see a default
Form (Form1) in your new C# project. The Windows Form you see in
Designer view is a visual representation of the window that will open when
your application is opened. You can switch between this view and Code
view at any time by right-clicking the design surface or code window and
then clicking View Code or View Designer. The following picture shows
how is the default Form (Form1) looks like.

At the top of the form there is a title bar which displays the forms
title. Form1 is the default name, and you can change the name to your
convenience . The title bar also includes the control box, which holds the
minimize, maximize, and close buttons.
If you want to set any properties of the Form, you can use Visual
Studio Property window to change it. If you do not see the Properties
window, on the View menu, click Properties window. This window lists the
properties of the currently selected Windows Form or control, and its here
that you can change the existing values.
For example , to change the forms title from Form1 to MyForm, click
on Form1 and move to the right side down Properties window, set Text
property to MyForm. Then you can see the Title of the form is changed.
Likewise you can set any properties of Form through Properties window.
You can also set the properties of the Form1 through coding. For coding,
you should right-click the design surface or code window and then clicking
View Code.

When you right click on Form then you will get code behind window,
there you can write your code
For example , if you want to change the back color of the form to Brown ,
you can code in the Form1_Load event like the following.
private void Form1_Load(object sender, EventArgs e)
{
this.BackColor = Color.Brown;
}
Properties
All controls have a number of properties that are used to manipulate
the behavior of the control. The base class of most controls, Control, has a
number of properties that other controls either inherit directly or override
to provide some kind of custom behavior.
The table below shows some of the most common properties of the
Control class. These properties will be present in most of the controls .

Name Availability Description


Anchor Read/Write Using this property, you can specify how
the control behaves when its container is
resized. See below for a detailed
explanation of this property.
BackColor Read/Write The background color of a control.
Bottom Read/Write By setting this property, you specify the
distance from the top of the window to the
bottom of the control. This is not the same
as specifying the height of the control.
Dock Read/Write Allows you to make a control dock to the
edges of a window. See below for a more
detailed explanation of this property.
Enabled Read/Write Setting Enabled to true usually means that
the control can receive input from the
user. Setting Enabled to false usually
means that it cannot.
ForeColor Read/Write The foreground color of the control.
Height Read/Write The distance from the top to the bottom of
the control.
Left Read/Write The left edge of the control relative to the
left edge of the window.
Name Read/Write The name of the control. This name can be
used to reference the control in code.
Parent Read/Write The parent of the control.
Right Read/Write The right edge of the control relative to the
left edge of the window.
TabIndex Read/Write The number the control has in the tab
order of its container.
TabStop Read/Write Specifies whether the control can be
accessed by the Tab key.
Tag Read/Write This value is usually not used by the
control itself, and is there for you to store
information about the control on the
control itself. When this property is
assigned a value through the Windows
Form designer, you can only assign a
string to it.
Top Read/Write The top edge of the control relative to the
top of the window.
Visible Read/Write Specifies whether or not the control is
visible
at runtime.
Width Read/Write The width of the control.

1) ComboBox and ListBox:-


 Both are derived from ListControl class.
 This class provides some of the basic list management functionality.
 SelectedIndex returns an integer value that corresponding to the index
of the currently selected item.
Add run time item is as:-
comboBox1.Items.Add("Sunday");
2) DateTimePicker:
 DateTimePicker allows user to select date or time value (or both) in
number of different formats.
 You can display the DateTime based value in any of the standard time
and date formats.
 Format property takes a DateTimePickerFormat
3) Labels:-
 Label controls place static text on a form.
 The text is contained in the Text property and aligned according the
TextAlign property.
4) Button:-
 The Button class represents the simple command button and derived
from ButtonBase class. The most common thing to do is to write code to
handle the Click event of the button.
 E.g private void btnName(object sender,EventArgs e) {
MessageBox.Show(“Hello”); }
5) ImageList
 An ImageList component is exactly what the name implies—a list of
images.
 Typically this property is used for holding a collection of images that are
used as toolbar icons, or icons in a TreeView control.
 Many controls have an ImageList property. The ImageList property
typically comes with an ImageIndex property. The ImageList property is
set to an instance of the ImageList component, the ImageIndex property
is set to the index in the ImageList that represents the image that should
be displayed on the control. You add images to the ImageList component
by using the Add method of the ImageList.Images property. The Images
property returns an ImageCollection. The two most commonly used
properties are ImageSize and ColorDepth. ImageSize uses a Size structure
as its value. The default value is 16×16 but it can be any value from 1 to
256. The ColorDepth uses a ColorDepth enumeration as its value. The
color depth values go from 4 bit to 32 bit. For .NET Framework 1.1 the
default is ColorDepth.Depth8Bit.
6) TextBox and RichTextBox
 The TextBox control is one of the most used controls in the Toolbox.
 The TextBox and RichTextBox controls are both derived from
TextBoxBase.
 TextBoxBase provides properties such as MultiLine and Lines.
 Multi- Line is a Boolean value that allows the TextBox control to display
text in more then one line. Each line in a text box is a part of an array of
strings. This array is exposed through the Lines property. The Text
property returns the entire text box contents as a single string.
TextLength is the total length of the string that text would return. The
MaxLength property will limit the length of the text to the specified
amount.
SelectedText, SelectionLength, and SelectionStart all deal with the
currently selected text in the text box. The selected text is the text that is
highlighted when the control has focus. The TextBox control adds a couple
of interesting properties. AcceptsReturn is a Boolean value that will allow
the TextBox to accept the Enter key as a new line or whether it activates
the default button on the form. When set to true, pressing the Enter key
creates a new line in the TextBox. CharactorCasing determines the casing
of the text in the text box. The CharactorCasing enumeration contains
three values, Lower, Normal, and Upper.
Lower lowercases all text regardless of how it is entered; Upper
renders all text in uppercase letters, and Normal displays the text as it is
entered.
PasswordChar property takes a char the represents what is
displayed to the user when they type text in the textbox. This is typically
used for entering passwords and pin numbers. The text property will
return the actual text that was entered; only the display is affected by this
property.
7) RichTextBox
 If you’re looking for a text box control with more formatting muscle,
consider the RichTextBox.
 Although it won’t help you build the next Microsoft Word (for that, you’d
need much more fine-grained control to intercept key presses and control
painting), it does allow you to format arbitrary sections of text in the font,
color, and alignment you choose.
 The RichTextBox control derives from TextBoxBase, as does the
TextBox, so it shares most of its properties and methods .
 Along with these features, the RichTextBox adds the ability to handle
rich formatting, images, and links.
 It also provides a LoadFile() and a SaveFile() method for saving RTF
documents painlessly.
 One of the key enhancements the RichTextBox adds is a set of selection
properties that allow you to manipulate the formatting of the currently
selected text.
 The RichTextBox supports the familiar SelectedText, SelectionLength,
and SelectionStart properties, but it also adds a much more impressive
set of properties including SelectionColor, SelectionBackColor,
SelectionFont, and SelectionAlignment, which allow you to adjust the
formatting of the selected text. The RichTextBox is a text editing control
that can handle special formatting features. As the name implies, the
RichTextBox control uses Rich Text Format (RTF) to handle the special
formatting. You can make formatting changes by using the Selection
properties: SelectionFont, SelectionColor, SelectionBullet and paragraph
formatting with SelectionIndent, SelectionRightIndent, and
SelectonHangingIndent.
8) StatusBar:- The status bar usually sits at the bottom of the form and
contains a series of panels that display information about the current
status of the application. StatusBarPanel objects are added to the Panels
property.
A StatusBarPanel can contain text or an icon. You can also handle
the DrawItem event and do your own graphics drawing. The StatusBar
control also has properties for sizing grips. If the form that the StatusBar is
on is resizable, then this property presents a grip in the bottom-right
corner for resizing. Initially the StatusBar control displays the value of the
Text property. Even if you add panels, you must call the ShowPanels
method to display them.
9) LinkLabel This specialty label inherits from the Label class, but adds
some properties that make it particularly well suited to representing links.
For example, many applications provide a clickable link to a company Web
site in an About window.The LinkLabel handles the details of displaying a
portion of its text as a hyperlink. You specify this portion in the LinkArea
property using a
LinkArea structure that identifies the first character of the link and
the number of characters in the link. Depending on the LinkBehavior
property, this linked text may always be underlined, it may be displayed
as normal, or it may become underlined when the mouse hovers over it.
Here’s the basic code that creates a link on the Web site address:
lnkWebSite.Text = "See www.prosetech.com for more information."; //
Starts at position 4 and is 17 characters long. lnkWebSite.LinkArea = new
LinkArea(4, 17); lnkWebSite.LinkBehavior = LinkBehavior.HoverUnderline;
private void lnkWebSite_LinkClicked(Object sender,
LinkLabelLinkClickedEventArgs e) { // Change the color if needed.
e.LinkVisited = true; // Use the Process.Start method to open the default
browser with a URL.
System.Diagnostics.Process.Start("http://www.prosetech.com"); }
10) CheckBox and RadioButton The CheckBox and RadioButton
controls provide a Checked property that indicates whether
the control is checked or “filled in.” After the state is changed, a
CheckedChanged event occurs. You can create a special three-state
check box by setting the ThreeState property to true. You need to check
the CheckState property to examine whether it is Checked, Unchecked, or
Indeterminate (shaded but not checked). By default, the control is
checked and unchecked automatically when the user clicks it. You can
prevent this by setting AutoCheck to false and handling the Click event.
This allows you to programmatically prevent a check box or radio button
from being checked (without
trying to “switch it back” after the user has made a change).
11) PictureBox A picture box is one of the simplest controls .NET offers.
You can set a valid image using the Image property and configure a
SizeMode from the PictureBoxSizeMode enumeration. For example, you
can set the picture to automatically stretch to fit the picture box.
pic.Image = System.Drawing.Image.FromFile("mypic.bmp"); pic.SizeMode
= PictureBoxSizeMode.StretchImage;
12) List Controls .NET provides three basic list controls: ListBox,
CheckedListBox, and ComboBox. They all inherit (directly or indirectly)
from the abstract ListControl class, which defines basic functionality that
allows you to use a list control with data binding. Controls can be bound to
objects such as the DataSet, arrays, and ArrayList collections, regardless
of the underlying data source // Bind a list control to an array of city
names. String[] cityChoices = {"Seattle", "New York", "Singapore",
"Montreal"}; listCity.DataSource = cityChoices;
Multiple Document Interface (MDI) MDI-type applications are used
when you have an application that can show either multiple instances of
the same type of form or different forms that must be contained in some
way. An example of multiple instances of the same type of form is a text
editor that can show multiple edit windows at the same time. An
example of the second type of application is Microsoft Access. You can
have query windows, design windows, and table windows all open at the
same time. The windows never leave the boundaries of the main Access
application.
Example:
private void button1_Click(object sender, EventArgs e)
{
Form frm = new Form();
frm.Text = "Hello";
frm.Show();
}
SDI (single document interface):- SDIs can open only a single
document at a time. Notepad is an example of an SDI application. If you
want to open two text files at once, you need to fire up two instances of
Notepad.

You might also like