0% found this document useful (0 votes)
6 views38 pages

VB Unit III

This document provides an overview of building Windows applications using Visual Basic .NET, focusing on key concepts such as Windows Forms, controls, and properties. It details the steps to create a Windows Forms application, the various controls available, and their properties and events. Additionally, it explains how to manipulate form properties and handle user input through controls like TextBoxes and RichTextBoxes.

Uploaded by

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

VB Unit III

This document provides an overview of building Windows applications using Visual Basic .NET, focusing on key concepts such as Windows Forms, controls, and properties. It details the steps to create a Windows Forms application, the various controls available, and their properties and events. Additionally, it explains how to manipulate form properties and handle user input through controls like TextBoxes and RichTextBoxes.

Uploaded by

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

VISUAL PROGRAMMING - 7BIT5C2

Unit – III

Building Windows Applications – Creating a Windows Applications using window controls


- Windows Forms - Text Boxes - Rich Text boxes – Labels and link labels – Buttons -
Check boxes - Radio buttons - Panels and Group Boxes - List Boxes - Checked List boxes -
Combo boxes and Picture boxes - Scroll bars – Calendar control - Timer control –
Handling Menus – Dialog boxes – Deploying an Application – Graphics.

BUILDING WINDOWS APPLICATIONS:

 Windows Application is really inappropriate as a project title, considering that it has the
ability to be compiled to IL and ported to another operating system where it can be
natively compiled.

 Rather than attempting to demonstrate every feature related to developing Windows


applications in Visual Basic .NET, we will use key features and tools that will help you
see the object-oriented nature of .NET as you work with the standard design components
in the Visual Studio .NET IDE.
 The main focus of this chapter is to introduce you to some key concepts related to .NET
development using forms and controls, while looking at their underlying implementation
in .NET.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
CREATING A WINDOWS APPLICATIONS USING WINDOW CONTROLS

 Creating a
Window Forms
Application by
following the
following steps in
Microsoft Visual
Studio - File → New
Project → Windows
Forms Applications
 Finally, select
OK, Microsoft Visual
Studio creates your
project and displays
following window
Form with a
name Form1.

 Visual Basic Form is


the container for all the
controls that make up the
user interface.
 Every window you
see in a running visual basic
application is a form, thus
the terms form and window
describe the same entity.
 Visual Studio creates
a default form for you when
you create a Windows
Forms Application.
 Every form will have

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
title bar on which the form's caption is displayed and there will be buttons to close,
maximize and minimize the form shown below –
 If you click the icon on the top left corner, it opens the control menu, which contains the
various commands to control the form like to move control from one place to another
place, to maximize or minimize the form or to close the form.
Form Properties
Properties Description
 The button that's automatically activated when you press Enter, no matter
AcceptButton which control has the focus at the time.
 Usually the OK button on a form is set as AcceptButton for a form.
 The button that's automatically activated when you hit the Esc key.
CancelButton
 Usually, the Cancel button on a form is set as CancelButton for a form.
 This Boolean property indicates whether scroll bars will be automatically
AutoScroll attached to the form if it is resized to a point that not all its controls are
visible.
BackColor  Sets the form background color.
 The BorderStyle property determines the style of the form's border and the
appearance of the form −
 None − Borderless window that can't be resized.
 Sizable − This is default value and will be used for resizable window
that's used for displaying regular forms.
 Fixed3D − Window with a visible border, "raised" relative to the main
BorderStyle area. In this case, windows can't be resized.
 FixedDialog − A fixed window, used to create dialog boxes.
 FixedSingle − A fixed window with a single line border.
 FixedToolWindow − A fixed window with a Close button only. It looks
like the toolbar displayed by the drawing and imaging applications.
 SizableToolWindow − Same as the FixedToolWindow but resizable. In
addition, its caption font is smaller than the usual.
 By default, this property is True and you can set it to False to hide the icon
ControlBox
and disable the Control menu.
Font  This property specify font type, style, size

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 Determines whether a Help button should be displayed in the caption box
HelpButton
of the form.
Height  This is the height of the Form in pixels.
 By default, this property is True and you can set it to False to hide the
MinimizeBox
Minimize button on the title bar.
 By default, this property is True and you can set it to False to hide the
MaximizeBox
Maximize button on the title bar.
 This specifies the minimum height and width of the window you can
MinimumSize
minimize.
 This specifies the maximum height and width of the window you
MaximumSize
maximize.
Name  This is the actual name of the form.
 These two properties set or return the coordinates of the form's top-left
Top, Left
corner in pixels.
 This property is a True/False value that lets you specify whether the form
TopMost will remain on top of all other forms in your application.
 Its default property is False.
Width  This is the width of the form in pixel.
Form Methods
Method Name Description
Activate  Activates the form and gives it focus.
ActivateMdiChild  Activates the MDI child of a form.
AddOwnedForm  Adds an owned form to this form.
BringToFront  Brings the control to the front of the z-order.
 Centers the position of the form within the bounds of the parent
CenterToParent
form.
CenterToScreen  Centers the form on the current screen.
Close  Closes the form.
 Retrieves a value indicating whether the specified control is a
Contains
child of the control.
Focus  Sets input focus to the control.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
Hide  Conceals the control from the user.
 Forces the control to invalidate its client area and immediately
Refresh
redraw itself and any child controls.
 Scales the control and all child controls by the specified scaling
Scale(SizeF)
factor.
ScaleControl  Scales the location, size, padding, and margin of a control.
ScaleCore  Performs scaling of the form.
Select  Activates the control.
SendToBack  Sends the control to the back of the z-order.
SetAutoScrollMargin  Sets the size of the auto-scroll margins.
SetDesktopBounds  Sets the bounds of the form in desktop coordinates.
SetDesktopLocation  Sets the location of the form in desktop coordinates.
SetDisplayRectLocation  Positions the display window to the specified value.
Show  Displays the control to the user.
ShowDialog  Shows the form as a modal dialog box.
Form Events
Event Description
Activated  Occurs when the form is activated in code or by the user.
Click  Occurs when the form is clicked.
Closed  Occurs before the form is closed.
Closing  Occurs when the form is closing.
DoubleClick  Occurs when the form control is double-clicked.
DragDrop  Occurs when a drag-and-drop operation is completed.
Enter  Occurs when the form is entered.
GotFocus  Occurs when the form control receives focus.
HelpButtonClicked  Occurs when the Help button is clicked.
KeyDown  Occurs when a key is pressed while the form has focus.
KeyPress  Occurs when a key is pressed while the form has focus.
KeyUp  Occurs when a key is released while the form has focus.
Load  Occurs before a form is displayed for the first time.
LostFocus  Occurs when the form loses focus.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 Occurs when the mouse pointer is over the form and a mouse button
MouseDown
is pressed.
MouseEnter  Occurs when the mouse pointer enters the form.
MouseHover  Occurs when the mouse pointer rests on the form.
MouseLeave  Occurs when the mouse pointer leaves the form.
MouseMove  Occurs when the mouse pointer is moved over the form.
 Occurs when the mouse pointer is over the form and a mouse button
MouseUp
is released.
MouseWheel  Occurs when the mouse wheel moves while the control has focus.
Move  Occurs when the form is moved.
Resize  Occurs when the control is resized.
Scroll  Occurs when the user or code scrolls through the client area.
Shown  Occurs whenever the form is first displayed.
VisibleChanged  Occurs when the Visible property value changes.

WINDOWS FORMS

 VB.Net 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 and drop controls
from the Visual Studio
Toolbox window.
 The first step is to start
a new project and build a
form. Open your Visual Studio and select File->NewProject and select Visual Basic from
the New project dialog box and select Windows Froms Application.
 Enter your project name instead of WindowsApplication1 in the bottom of dialouge box
and click OK button.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 The following picture shows how to create a new Form in Visual Studio.

 Select project type from New project dialog Box.

 When you add a Windows Form to your project, many of the forms properties are set by
default.
 Although these values are convenient, they will not always suit your programming needs.
 The following picture shows how is the default Form look like.
 At the top of the
form there is a title bar
which displays the forms
title.
 Form1 is the
default name, 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.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 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
like this.
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Me.BackColor = Color.Brown
End Sub
TEXT BOXES

 VB.Net provides several


mechanisms for gathering input in a
program.
 A TextBox control is used to
display, or accept as input, a single line of
text.
 VB.Net programmers make
extensive use of the TextBox control to

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
let the user view or enter large amount of text.
 A text box object is used to display text on a form or to get user input while a VB.Net
program is running.
 In a text box, a user can type data or paste it into the control from the clipboard.
 For displaying a text in a TextBox control , you can code like this.
TextBox1.Text = "http://vb.net-informations.com"
 You can also collect the input value from a TextBox control to a variable like this way.
Dim var As String
var = TextBox1.Text
VB.Net TextBox Properties
 You can set TextBox properties through
Property window or through program.
 Normally Property window is located
under the solution explorer.
 You can open Properties window by
pressing F4 or right click on a control and select
Properties menu item.
 The below code set a textbox width as 150
and height as 25 through source code.
TextBox1.Width = 150
TextBox1.Height = 25
Background Color and Foreground Color
 You can set Textbox background color and foreground color through property window,
also you can set it programmatically.
TextBox1.BackColor = Color.Red
TextBox1.ForeColor = Color.Gray
TextBox BorderStyle
 You can set 3 different types of border style for textbox in vb.net, they are None,
FixedSingle and fixed3d.
TextBox1.BorderStyle = BorderStyle.FixedSingle
VB.Net TextBox Events
TextBox Keydown event
 Keydown event occurs when a key is pressed while the control has focus.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
MessageBox.Show("Enter key pressed")
ElseIf e.KeyCode = Keys.Escape Then
MessageBox.Show("Escape key pressed")
End If
End Sub
TextChanged Event
 TextChanged Event is raised if the Text property is changed by either through program
modification or user input.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Label1.Text = TextBox1.Text
End Sub
VB.Net Textbox Maximum Length
 Maximum Length property sets the maximum number of characters or words the user can
input into the text box control. That means you can limit the user input by this property.
TextBox1.MaxLength = 25
How to ReadOnly Textbox
 when a program wants to prevent a user from changing the text that appears in a text box,
the program can set the controls Readonly property is to True.
TextBox1.ReadOnly = True
Multiline TextBox in vb.net
 By default TextBox accept single line of characters , If you need to enter more than one
line in a TextBox control, you should change the Multiline property is to True.
TextBox1.Multiline = True
Textbox passowrd character
 Sometimes you want a textbox to receive password from the user. In order to keep the
password confidential, you can set the PasswordChar property of a textbox to a specific
character.
TextBox1.PasswordChar = "*"

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 The above code set the PasswordChar to * , so when the user enter password then it
display only * instead of typed characters.
RICH TEXT BOXES

 The RichTextBox control allows the user to display, enter, and edit text while also
providing more advanced formatting features than the conventional TextBox control.
 Following is a general description of the RichTextBox (RTB) control along with
summaries of its pertinent properties, methods, and events; all compiled from the MSDN
help files.
 The RichTextBox control provides a number of properties you can use to apply
formatting to any portion of text within the control.
 To change the formatting of text, it must first be selected.
 Only selected text can be assigned character and paragraph formatting.
 Using these properties, you can make text bold or italic, change the color, and create
superscripts and subscripts.
 You can also adjust paragraph formatting by setting both left and right indents, as well as
hanging indents.
 The RichTextBox control opens and saves files in both the RTF format and regular
ASCII text format.
 You can use methods of the control (LoadFile and SaveFile) to directly read and write
files, or use properties of the control such as SelRTF and TextRTF in conjunction with
Visual Basic's file input/output statements.
 The RichTextBox control supports object embedding by using
the OLEObjects collection.
 Each object inserted into the control is represented by an OLEObject object.
 This allows you to create documents with the control that contain other documents or
objects.
 For example, you can create a document that has an embedded Microsoft Excel
spreadsheet or a Microsoft Word document or any other OLE object registered on your
system.
 To insert objects into the RichTextBox control, you simply drag a file or a highlighted
portion of a file used in another application (such as Microsoft Word), and drop the
contents directly onto the control.
 The RichTextBox control supports both clipboard and OLE drag/drop of OLE objects.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 When an object is pasted in from the clipboard, it is inserted at the current insertion point.
 When an object is dragged and dropped into the control, the insertion point will track the
mouse cursor until the mouse button is released, causing the object to be inserted. This
behavior is the same as Microsoft Word.
 To print all or part of the text in a RichTextBox control use the SelPrint method.
 Because the RichTextBox is a data-bound control, you can bind it with a Data control to
a Binary or Memo field in a Microsoft Access database or a similar large capacity field in
other databases (such as a TEXT data type field in SQL Server).
 The RichTextBox control supports almost all of the properties, events and methods used
with the standard TextBox control, such
as MaxLength, MultiLine, ScrollBars, SelLength, SelStart, and SelText.
 Applications that already use TextBox controls can easily be adapted to make use
of RichTextBox controls.
 However, the RichTextBox control doesn't have the same 64K character capacity limit of
the conventional TextBox control.

LABELS AND LINK LABELS

 Microsoft Visual Studio .NET controls


are the graphical tools you use to build the user
interface of a VB.Net program. Labels are one of
the most frequently used Visual Basic control.
 A Label control lets you place
descriptive text , where the text does not need to
be changed by the user. The Label class is defined in the System.Windows.Forms
namespace.
 Add a Label control to the form. Click Label in the Toolbox and drag it over the forms
Designer and drop it in the desired location.
 If you want to change the display text of the Label, you have to set a new text to the Text
property of Label.
o Label1.Text = "This is my first Label"
 You can load Image in Label control , if you want to load an Image in the Lable control
you can code like this
 Label1.Image = Image.FromFile("C:\testimage.jpg")

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 The following source code shows how to set some properties of the Label through
coding.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Label1.Text = "This is my first Label"
Label1.BorderStyle = BorderStyle.FixedSingle
Label1.TextAlign = ContentAlignment.MiddleCenter
End Sub
End Class
BUTTONS

 Windows Forms controls are


reusable components that encapsulate
user interface functionality and are used
in client side Windows applications.
 A Button is a control, which is an
interactive component that enables users
to communicate with an application
which we click and release to perform
some actions.
 The Button control represents a
standard button that reacts to a Click
event.
 A Button can be clicked by using
the mouse, ENTER key, or SPACEBAR if the button has focus.
 When you want to change display text of the Button , you can change the Text property
of the button.
o Button1.Text = "My first Button"
 Similarly if you want to load an Image to a Button control , you can code like this.
o Button1.Image = Image.FromFile("C:\testimage.jpg")
 The following vb.net source code shows how to change the button Text property while
Form loading event and to display a message box when pressing a Button Control.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Button1.Text = "Click Here"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox("http://vb.net-informations.com")
End Sub
End Class

CHECK BOXES

 CheckBoxes allow the user to make


multiple selections from a number of options.
You can click a check box to select it and click
it again to deselect it.
 CheckBoxes comes with a caption,
which you can set in the Text property.
o CheckBox1.Text = "Net-
informations.com"
 You can use the CheckBox control
ThreeState property to direct the control to
return the Checked, Unchecked, and
Indeterminate values.
 You need to set the check boxs ThreeState property to True to indicate that you want it to
support three states.
o CheckBox1.ThreeState = True
 To apply the same property settings to multiple CheckBox controls, use the Style
property.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
RADIO BUTTONS

 A radio button or option button is a type of


graphical user interface element that allows the user
to choose only one of a predefined set of options.
 When a user clicks on a radio button, it
becomes checked, and all other radio buttons with
same group become unchecked.
 The radio button and the check box are used
for different functions.
 Use a radio button when you want the user to
choose only one option.
 When you want the user to choose all
appropriate options, use a check box.
 Like check boxes, radio buttons support a Checked property that indicates whether the
radio button is selected.

PANELS AND GROUP BOXES

 A GroupBox is a container control that places a border around its collection of controls.
 It is often used to group radio buttons; but it is also a convenient way to organize and
manage any related controls on a form.
 For example, setting the Enabled property of a group box to false disables all controls in
the group box.
o constructor: public GroupBox()
 The constructor creates an untitled GroupBox having a default width of 200 pixels and a
default height of 100 pixels.
The Panel Class
 The Panel control is a container used to group a collection of controls.
 It’s closely related to the GroupBox control, but as a descendent of the ScrollableControl
class, it adds a scrolling capability.
o constructor: public Panel()
 Its single constructor creates a borderless container area that has scrolling disabled.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 By default, a Panel takes the background color of its container, which makes it invisible
on a form.
 Because the GroupBox and Panel serve the same purpose, the programmer is often faced
with the choice of which to use. Here are the factors to consider in selecting one:
 A GroupBox may have a visible caption, whereas the Panel does not.
 A GroupBox always displays a border; a Panel’s border is determined by its BorderStyle
property.
 It may be set to BorderStyle.None, BorderStyle.Single, or BorderStyle.Fixed3D.
 A GroupBox does not support scrolling; a Panel enables automatic scrolling when its
AutoScroll property is set to true.
 A Panel offers no features to assist in positioning or aligning the controls it contains. For
this reason, it is best used when the control layout is known at design time.
 But this is not always possible.
 Many applications populate a form with controls based on criteria known only at runtime.
To support the dynamic creation of controls, .NET offers two layout containers that
inherit from Panel and automatically position controls within the container: the
FlowLayoutPanel and the TableLayoutPanel.
The FlowLayoutPanel Control

 This "no-frills" control has a single parameterless constructor and two properties worth
noting: a FlowDirection property that specifies the direction in which controls are to be
added to the container, and a WrapControls property that indicates whether child controls
are rendered on another row or truncated.
TableLayoutPanel Control

 This code segment creates a TableLayoutPanel and adds the same four controls used in
the previous example.
 Container properties are set to define a layout grid that has two rows, two columns, and
uses an Inset border style around each cell.
 Controls are always added to the container moving left-to-right, top-to-bottom.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2

LIST BOXES

 VB.Net provides several mechanisms


for gathering input in a program.
 A Windows Forms ListBox control
displays a list of choices which the user can
select from.
 You can use the Add or Insert
method to add items to a list box.
 The Add method adds new items at
the end of an unsorted list box.
 The Insert method allows you to
specify where to insert the item you are
adding.
o ListBox1.Items.Add("Sunday")
 The SelectionMode property
determines how many items in the list can be
selected at a time.
 A ListBox control can provide single or multiple selections using the SelectionMode
property .
 If you want to retrieve a single selected item to a variable , you can code like this.
o Dim var As String
o var = ListBox1.SelectedItem
 If you change the selection mode property to multiple select , then you will retrieve a
collection of items from ListBox1.SelectedItems property.
o ListBox1.SelectionMode = SelectionMode.MultiSimple

CHECKED LIST BOXES

 The CheckedListBox control gives you all the capability of a list box and also allows you
to display a check mark next to the items in the list box.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2

 To add objects to the list at run time, assign an array of object references with the
AddRange method. The list then displays the default string value for each object.
o Dim days As String() = {"Sunday", "Monday", "Tuesday"}
o checkedListBox1.Items.AddRange(days)
 You can add individual items to the list with the Add method. The CheckedListBox
object supports three states through the CheckState enumeration: Checked,
Indeterminate, and Unchecked.
o CheckedListBox1.Items.Add("Sunday", CheckState.Checked)
o CheckedListBox1.Items.Add("Monday", CheckState.Unchecked)
o CheckedListBox1.Items.Add("Tuesday", CheckState.Indeterminate)

COMBO BOXES

 VB.Net controls are located in the Toolbox of


the development environment, and you use them to
create objects on a form with a simple series of mouse
clicks and dragging motions.
 The ComboBox control , which lets the user
choose one of several choices.
 The user can type a value in the text field or
click the button to display a drop down list.
 In addition to display and selection functionality,
the ComboBox also provides features that enable you to

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
efficiently add items to the ComboBox.
Add item to combobox
 ComboBox1.Items.Add("Sunday")
 ComboBox1.Items.Add("Monday")
 ComboBox1.Items.Add("Tuesday")
How to set the selected item in a comboBox
 You can set which item should shown while it displaying in the form for first time.
 comboBox1.Items.Add("test1")
 comboBox1.Items.Add("test2")
 comboBox1.Items.Add("test3")
 comboBox1.SelectedItem = "test3"
 or
 ComboBox1.SelectedItem = ComboBox1.Items(1)
 or
 comboBox1.SelectedIndex = comboBox1.FindStringExact("test3")
 ComboBox SelectedItem
How to retrieve value from ComboBox
 If you want to retrieve the displayed item to a string variable , you can code like this
 Dim var As String
 var = ComboBox1.Text
 Or
 Dim item = Me.comboBox1.GetItemText(Me.comboBox1.SelectedItem)
 MessageBox.Show(item)
How to remove an item from ComboBox in VB.Net
 You can remove items from a combobox in two ways. You can remove item at a the
specified index or giving a specified item by name.
 ComboBox1.Items.RemoveAt(1)
 The above code will remove the second item from the combobox.
 ComboBox1.Items.Remove("Friday")
 The above code will remove the item "Friday" from the combobox.
DropDownStyle

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 The DropDownStyle property specifies whether
the list is always displayed or whether the list is
displayed in a drop down.
 The DropDownStyle property also specifies
whether the text portion can be edited.
o ComboBox1.DropDownStyle =
ComboBoxStyle.DropDown
o ComboBox DataSource Property
 Programmatically Binding DataSource to
ComboBox
 You can populate a combo box
with a DataSet in a simple way..
 Consider an sql query string
like...."select au_id,au_lname from
authors";
 Make a datasource in your
program and bind it like the following...
o comboBox1.DataSource = ds.Tables(0)
o comboBox1.ValueMember = "au_id"
o comboBox1.DisplayMember = "au_lname"

PICTURE BOXES

 The Windows Forms PictureBox control is used to


display images in bitmap, GIF,icon, or JPEG formats.
 You can set the Image property to the Image you
want to display, either at design time or at run time.
 You can programmatically change the image
displayed in a picture box, which is particularly useful
when you use a single form to display different pieces of
information.
o PictureBox1.Image =
Image.FromFile("C:\testImage.jpg")

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 The SizeMode property, which is set to values in the PictureBoxSizeMode enumeration,
controls the clipping and positioning of the image in the display area.
o PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
 There are five different PictureBoxSizeMode is available to PictureBox control.
o AutoSize - Sizes the picture box to the image.
o CenterImage - Centers the image in the picture box.
o Normal - Places the upper-left corner of the image at upper left in the picture box.
o StretchImage - Allows you to stretch the image in code.
 You can change the size of the display area at run time with the ClientSize property.
o pictureBox1.ClientSize = New Size(xSize, ySize)
 The following VB.Net program shows how to load a picture from a file and display it in
streach mode.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
PictureBox1.Image = Image.FromFile("d:\testImage.jpg")
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
End Class

SCROLL BARS

 The horizontal and vertical scrollbars which the user can


use to move content into view.
 Most of the controls that use scrollbars come with them
built in, such as multiline text boxes, combo boxes etc.
 You can set the Value property yourself in code, which
moves the scroll box to match.
 The controls Value property gets and sets its current
numeric value.
 The Minimum and Maximum properties determine the range of values that the control
can display.
 The following VB.Net program shows a TextBox control with scrollbars.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
CALENDAR & TIMER CONTROL

 The DateTimePicker control allows you to


display and collect date and time from the user
with a specified format.
 The DateTimePicker control prompts the
user for a date or time using a graphical calendar
with scroll arrows.
 The most important property of the
DateTimePicker is the Value property, which holds
the selected date and time.
 DateTimePicker1.Value = "12/31/2010"
 The Value property is set to the current date by default. You can use the Text property or
the appropriate
o member of Value to get the date and time value.
o Dim idate As String
o idate = DateTimePicker1.Value
 The control can display one of several styles, depending on its property values. The
values can be displayed in four
 formats, which are set by the Format property: Long, Short, Time, or Custom.
 DateTimePicker1.Format = DateTimePickerFormat.Short
 The following VB.Net program shows how to set and get the value of a DateTimePicker1
control.
HANDLING MENUS

 A menu is located on the menu bar and contains a list of related commands. MainMenu is
the container for the Menu structure of the form and menus are made of MenuItem
objects that represent individual parts of a menu.
 You can create a main menu object on your form using the MainMenu control. The
following picture shows how to drag the Menustrip Object to the Form.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 After drag the Menustrip on
your form you can directly create
the menu items by type a value into
the "Type Here" box on the
menubar part of your form.
 From the following picture
you can understand how to create
each menu items on mainmenu
Object.
 If you need a separator bar ,
right click on your menu then go to
insert->Separator.
 After creating the Menu on
the form , you have to double click on each
menu item and write the programs there
depends on your requirements.
 The following Vb.Net program
shows how to show a messagebox when
clicking a menu item.

DIALOG BOXES

 There are many built-in dialog


boxes to be used in Windows forms for
various tasks like opening and saving files,
printing a page, providing choices for
colors, fonts, page setup, etc., to the user of
an application.
 These built-in dialog boxes reduce
the developer's time and workload.
 All of these dialog box control classes inherit from the CommonDialog class and override
the RunDialog() function of the base class to create the specific dialog box.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 The RunDialog() function is automatically invoked when a user of a dialog box calls
its ShowDialog() function.
 The ShowDialog method is used to display all the dialog box controls at run-time. It
returns a value of the type of DialogResult enumeration. The values of DialogResult
enumeration are −
o Abort − returns DialogResult.Abort value, when user clicks an Abort button.
o Cancel − returns DialogResult.Cancel, when user clicks a Cancel button.
o Ignore − returns DialogResult.Ignore, when user clicks an Ignore button.
o No − returns DialogResult.No, when user clicks a No button.
o None − returns nothing and the dialog box continues running.
o OK − returns DialogResult.OK, when user clicks an OK button
o Retry − returns DialogResult.Retry , when user clicks an Retry button
o Yes − returns DialogResult.Yes, when user clicks an Yes button
 The following diagram shows the common dialog class inheritance −

 All these above-mentioned classes have corresponding controls that could be added from
the Toolbox during design time.
 When you double click any of the dialog controls in the toolbox or drag the control onto
the form, it appears in the Component tray at the bottom of the Windows Forms
Designer, they do not directly show up on the form.
 The following table lists the commonly used dialog box controls. Click the following
links to check their detail –

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
Sr.No. Control & Description
1 ColorDialog
It represents a common dialog box that displays available colors along with controls
that enable the user to define custom colors.
2 FontDialog
It prompts the user to choose a font from among those installed on the local computer
and lets the user select the font, font size, and color.
3 OpenFileDialog
It prompts the user to open a file and allows the user to select a file to open.
4 SaveFileDialog
It prompts the user to select a location for saving a file and allows the user to specify
the name of the file to save data.
5 PrintDialog
It lets the user to print documents by selecting a printer and choosing which sections of
the document to print from a Windows Forms application.
1. ColorDialog Control
 The ColorDialog
control class represents a
common dialog box that
displays available colors
along with controls that
enable the user to define
custom colors.
 It lets the user select
a color.
 The main property
of the ColorDialog control
is Color, which returns
a Color object.
 Following is the
Color dialog box −

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
Properties of the ColorDialog Control
 The following are some of the commonly used properties of the ColorDialog control −
Property & Description
AllowFullOpen
Gets or sets a value indicating whether the user can use the dialog box to define custom
colors.
AnyColor
Gets or sets a value indicating whether the dialog box displays all available colors in the
set of basic colors.
Color
Gets or sets the color selected by the user.
CustomColors
Gets or sets the set of custom colors shown in the dialog box.
FullOpen
Gets or sets a value indicating whether the controls used to create custom colors are
visible when the dialog box is opened.
ShowHelp
Gets or sets a value indicating whether a Help button appears in the color dialog box.
SolidColorOnly
Gets or sets a value indicating whether the dialog box will restrict users to selecting solid
colors only.
Methods of the ColorDialog Control
 The following are some of the commonly used methods of the ColorDialog control −
Method Name & Description
Reset
Resets all options to their default values, the last selected color to black, and the custom
colors to their default values.
RunDialog
When overridden in a derived class, specifies a common dialog box.
ShowDialog
Runs a common dialog box with a default owner.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 Clicking on the Change Color button,
the color dialog appears, select a color and
click the OK button.
 The selected color will be applied as
the forecolor of the text of the label.
2. FontDialog Control
 It prompts the user to choose a font
from among those installed on the local
computer and lets the user select the font,
font size, and color. It returns the Font and
Color objects.
 Following is the Font
dialog box −
 By default, the Color
ComboBox is not shown on
the Font dialog box. You
should set
the ShowColor property of
the FontDialog control to
be True.
 Properties of the
FontDialog Control
 The following are
some of the commonly used
properties of the FontDialog control −
Property & Description
AllowSimulations
Gets or sets a value indicating whether the dialog box allows graphics device interface (GDI)
font simulations.
AllowVectorFonts
Gets or sets a value indicating whether the dialog box allows vector font selections.
AllowVerticalFonts

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
Gets or sets a value indicating whether the dialog box displays both vertical and horizontal
fonts, or only horizontal fonts.
Color
Gets or sets the selected font color.
Font
Gets or sets the selected font.
FontMustExist
Gets or sets a value indicating whether the dialog box specifies an error condition if the user
attempts to select a font or style that does not exist.
MaxSize
Gets or sets the maximum point size a user can select.
MinSize
Gets or sets the minimum point size a user can select.
ShowApply
Gets or sets a value indicating whether the dialog box contains an Apply
button.
ShowColor
Gets or sets a value indicating whether the dialog box displays the color choice.
ShowEffects
Gets or sets a value indicating whether the dialog box contains controls that allow the user to
specify strikethrough, underline, and text color options.
ShowHelp
Gets or sets a value indicating whether the dialog box displays a Help button.
Methods of the FontDialog Control
 The following are some of the commonly used methods of the FontDialog control −
Method Name & Description
Reset
Resets all options to their default values.
RunDialog
When overridden in a derived class, specifies a common dialog box.
ShowDialog
Runs a common dialog box with a default owner.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2

 Enter some text and Click on the Change Font button.

 The Font dialog appears, select a font and a color and click the OK button. The selected
font and color will be applied as the font and fore color of the text of the rich text box.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
3. OpenFileDialog Control
 The OpenFileDialog control prompts
the user to open a file and allows the user to
select a file to open. The user can check if
the file exists and then open it. The
OpenFileDialog control class inherits from
the abstract class FileDialog.
 If the ShowReadOnly property is set
to True, then a read-only check box appears
in the dialog box. You can also set the
ReadOnlyChecked property to True, so that
the read-only check box appears checked.
 Following is the Open File dialog box −
Properties of the OpenFileDialog Control
 The following are some of the commonly used properties of the OpenFileDialog control
Property & Description
AddExtension
Gets or sets a value indicating whether the dialog box automatically adds an extension to a file
name if the user omits the extension.
AutoUpgradeEnabled
Gets or sets a value indicating whether this FileDialog instance should automatically upgrade
appearance and behavior when running on Windows Vista.
CheckFileExists
Gets or sets a value indicating whether the dialog box displays a warning if the user specifies a file
name that does not exist.
CheckPathExists
Gets or sets a value indicating whether the dialog box displays a warning if the user specifies a
path that does not exist.
CustomPlaces
Gets the custom places collection for this FileDialog instance.
DefaultExt
Gets or sets the default file name extension.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
FileName
Gets or sets a string containing the file name selected in the file dialog box.
FileNames
Gets the file names of all selected files in the dialog box.
ReadOnlyChecked
Gets or sets a value indicating whether the read-only check box is selected.
RestoreDirectory
Gets or sets a value indicating whether the dialog box restores the current directory before closing.
SafeFileName
Gets the file name and extension for the file selected in the dialog box. The file name does not
include the path.
ShowHelp
Gets or sets a value indicating whether the Help button is displayed in the file dialog box.
ShowReadOnly
Gets or sets a value indicating whether the dialog box contains a read-only check box.
Title
Gets or sets the file dialog box title.
ValidateNames
Gets or sets a value indicating whether the dialog box accepts only valid Win32 file names.
Methods of the OpenFileDialog Control
 The following are some of the commonly used methods of the OpenFileDialog control −
Method Name & Description
OpenFile
Opens the file selected by the user, with read-only permission. The file is specified by the
FileName property.
Reset
Resets all options to their default value.

 Click on the Load Image File button to load an


image stored in your computer.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2

4. SaveFileDialog Control
 The SaveFileDialog control
prompts the user to select a location for
saving a file and allows the user to specify
the name of the file to save data. The
SaveFileDialog control class inherits from
the abstract class FileDialog.
 Following is the Save File dialog
box −
 Properties of the SaveFileDialog
Control
 The following are some of the
commonly used properties of the SaveFileDialog control −
Property & Description
AddExtension
Gets or sets a value indicating whether the dialog box automatically adds an extension to a
file name if the user omits the extension.
CheckFileExists
Gets or sets a value indicating whether the dialog box displays a warning if the user specifies
a file name that does not exist.
CheckPathExists
Gets or sets a value indicating whether the dialog box displays a warning if the user specifies
a path that does not exist.
DefaultExt
Gets or sets the default file name extension.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
FileName
Gets or sets a string containing the file name selected in the file dialog box.
FileNames
Gets the file names of all selected files in the dialog box.
Filter
Gets or sets the current file name filter string, which determines the choices that appear in the
"Save as file type" or "Files of type" box in the dialog box.
InitialDirectory
Gets or sets the initial directory displayed by the file dialog box.
RestoreDirectory
Gets or sets a value indicating whether the dialog box restores the current directory before
closing.
ShowHelp
Gets or sets a value indicating whether the Help button is displayed in the file dialog box.
Title
Gets or sets the file dialog box title.
ValidateNames
Gets or sets a value indicating whether the dialog box accepts only valid Win32 file names.
Methods of the SaveFileDialog Control
 The following are some of the commonly used methods of the SaveFileDialog control −
Method Name & Description
OpenFile
Opens the file with read/write permission.
Reset
Resets all dialog box options to their default values.

 We have set the Filter property of the


SaveFileDialog control to display text file types
with .txt extensions only.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 Write some text in the text box and click on the Save Comment button to save the text as
a text file in your computer.

5. PrintDialog Control
 The PrintDialog control lets the
user to print documents by selecting a
printer and choosing which sections of
the document to print from a Windows
Forms application.
 There are various other
controls related to printing of
documents. Let us have a brief look at
these controls and their purpose. These
other controls are −
 The PrintDocument control − it
provides support for actual events and
operations of printing in Visual Basic
and sets the properties for printing.
 The PrinterSettings control − it
is used to configure how a document is printed by specifying the printer.
 The PageSetUpDialog control − it allows the user to specify page-related print settings
including page orientation, paper size and margin size.
 The PrintPreviewControl control − it represents the raw preview part of print previewing
from a Windows Forms application, without any dialog boxes or buttons.
 The PrintPreviewDialog control − it represents a dialog box form that contains a
PrintPreviewControl for printing from a Windows Forms application.
 Following is the Print dialog box −

Properties of the PrintDialog Control
 The following are some of the commonly used properties of the PrintDialog control −
Property & Description
AllowCurrentPage

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
Gets or sets a value indicating whether the Current Page option button is displayed.
AllowPrintToFile
Gets or sets a value indicating whether the Print to file check box is enabled.
AllowSelection
Gets or sets a value indicating whether the Selection option button is enabled.
AllowSomePages
Gets or sets a value indicating whether the Pages option button is enabled.
Document
Gets or sets a value indicating the PrintDocument used to obtain PrinterSettings.
PrinterSettings
Gets or sets the printer settings the dialog box modifies.
PrintToFile
Gets or sets a value indicating whether the Print to file check box is selected.
ShowHelp
Gets or sets a value indicating whether the Help button is displayed.
ShowNetwork
Gets or sets a value indicating whether the Network button is displayed.
Methods of the PrintDialog Control
 The following are some of the commonly used methods of the PrintDialog control −
Method Name & Description
Reset
Resets all options to their default values.
RunDialog
When overridden in a derived class, specifies a common dialog box.
ShowDialog
Runs a common dialog box with a default owner.

 Click the Print button to make the Print dialog box


appear.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
DEPLOYING AN APPLICATION

 Open the Solution of your application.


 File -> Add -> New Project...
 Other Project Types -> Setup and Deployment -> Visual Studio Installer -> Setup
Project.
 In the File System Tab right-click on "Application Folder" -> Add -> Project Output and
Select "Primary output" from your application project.

GRAPHICS

 Graphics on three controls:


o Form
o PictureBox
o ImageBox

 The main difference in these three controls is that the ImageBox control is
designed specifically for displaying images and not for creating new images
or manipulating them.
 The other two controls provide drawing methods that let you design graphics at runtime.
 The methods for loading graphics on the various controls are simpler than
creating graphics from scratch. You can place graphics oncontrols-at design time and,
runtime.
 To load a graphic (bitmap or icon) on a control at design time, you assign its filename to
the Picture property of the control in the Properties window.
 This same procedure can’t change the image displayed at runtime; instead, you must use
the LoadPictureO function, which is described shortly.
 If the graphic is assigned to a control at design time, ‘it’s stored along with
the application. One of the files that the Visual Basic editor generates for each Form in a
project has the extension FRX.This is where the image (the actual bitmap) is stored.
 As a consequence, the size of the application will increase if its Forms contain bitmaps
that must be loaded at runtime.
 The alternative is to load the .graphic at runtime with the LoadPictureO function.

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
 This reduces the size of the fRX file, but your application must make sure the file is
available at runtime. Vector drawings aren’t loaded, they are generated on the fly.
Sizing Image
 When an linage is loaded on a Form or a PictureBox, you must make sure it will fill the
available space, unless you let the user select the graphic at runtime.
 Graphics are usually placed on ImageBox or Picture Box controls.
 The lmageBox is good for displaying graphics and uses fewer resources than PictureBox
controls, The Picture ” Box control provides methods for drawing at runtime and is much
more flexible than the Image.Box control.
 As a consequence, the PictureBox control uses more resources.
 Each control provides a different property for controlling the appearance of the picture
displayed
The ImageBox ,Control’
 If the Stretch property is True, the image is resized to fill the area of the
ImageBox control. Unless the ~ontrol’s dimensions have the same aspect tatio as the
image’s dimensions, the image is distorted as it’s resized.
 If the Stretch property is False, the ImageBox control behaves like a PictureBox with its
Aut6Size property set to True.
 Notice how the image is resized in the ImageBox control whose Stretch property is True.
The PictureBox Control
 If the AutoSize property is True, the control is
resized to the dimensions of the image it contains.
 If the AutoSize property is False, only the
part of the image that can fit on the control is
displayed.
Loading and Saving Images
 To load a graphic on a control at runtime, use
the LoadPicture method as follows:
 Forml. Picture – LoadPicture (fileNam)”
 The fileName variable is ‘the name of the file
containing the graphic.
 This file can have one of the following

S.Priyadharshini MCA., M.Phil Department of Information Technology


VISUAL PROGRAMMING - 7BIT5C2
‘extensions:
 BMP Bitmap
 GIF Graphics Interchange Format
 JPG Joipt Photographic Experts Group .
 DIB Device Independent Bitmap
 WMF Windows MetaFile
 EMF Enhanced MetaFile
 At present, Visual Basicdoesn’t support other types of graphic files.
 If you use the LoadPicture method without an argument, the current picture is unloaded
(along with any drawing or printing painted onto the control or Form with the Circle,
Line, PSet, or Print commands).
 Calling the LoadPicture without an argument clears the control.
 To remove a graphic from a Picture Box (or any control that’ can play graphics), issue
the following command: Picturel.
 Picture LoadPicture() This technique is similar to the Cls method, which clears any
drawing-or printing from a Form or a control.
 If your application processes the displayed image during the course of its execution and
you want to save the image, you can use the SavePicture statement. Its syntax is as
follows:
 SavePicture picture, filename
 The picture argument is the Picture property of the PictureBox or ImageBox
control whose contents you want to save, Hndfilenanu: is the name of thefile that
will store the image.
 To save the contents of the Picturel control to file, you must use a statement like the
following:
 SavePicture Picturel. Picture, c:/ tmplma·Je .bmp
 The Savef’icture statement supports only BMP files, even if the original image loaded on
the control comes from a GlF or a JPG file.
 When prompting the user w.ith the FileOpen common dialog box to select an image file
to open, Y9U can use the extensions aMP, GIF, or JPG. In the corresponding FileSave
common dialog box though, you can specify only the I3MP(or DIB) extension .
*************************************************************************************

S.Priyadharshini MCA., M.Phil Department of Information Technology

You might also like