Professional Documents
Culture Documents
Client area
Controls
4/105
Adding Controls
Two methods in adding controls
Double-click the control to be added in the toolbox and then drag the new control to the desired place in the form; or simply click and drag the control from the toolbox
Select Windows under Visual Basic in the Project Types box. Click the Windows Application icon in the Templates box.
6/105
12/11/2013
Form Properties
Property Description
Returns or sets the button automatically clicked when the user presses the ENTER. Returns the currently active form in the application. Returns the currently active multiple-document interface (MDI) child window. Returns or sets whether the form will accept data that the user drags onto it. Returns or sets whether the form adjusts to fit the font used and scales its controls. Returns or sets a value indicating whether the form enables autoscrolling. Returns or sets mode by which form automatically resizes itself.
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
8/105
7/105
Form Properties
Property
BackColor
Form Properties
Property Description
Returns or sets the foreground color of the form. Returns or sets the border style of the form. Returns or sets the forms height(in pixels) Returns or sets the icon for the form. Returns a value indicating whether the form is a multiple-doc um ent interface (MDI) child form. Returns or sets a value indicating whether the form is a container for multiple-docu me nt interface (MDI) child forms. Returns or sets the coordinates of the upperleft corner of the form with respect to the upper-left corner of the screen.
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
10/105
Description
Sets the background color. ForeColor
BackgroundIma ge
CancelButton ClientSize ControlBox
image of a
FormBorderStyle Height Icon IsMdiChild IsMdiContainer
Returns or sets the button control that is clicked when the user presses ESC key. Returns and sets the size of the client area of the form. Returns or sets a value indicating whether a control box is displayed in the caption bar of the form. Returns or sets the mouse cursor when the mouse is over the form. Returns or sets the dialog result for the form. Returns or sets the text font in the form.
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
9/105
Location
Form Properties
Property
MaximizeBox
Form Properties
Property
MinimumSize Modal
Description
Returns or sets a value indicating whether the Maximize button is displayed in the caption bar of the form. Returns the maximum size the form can be resized to. Returns an array of forms that represent the multiple-docu me nt interface (MDI) child forms that are parented to this form. Returns or sets current multiple-doc um ent interface (MDI) parent form of this form. Returns or sets the menu that is displayed in the form Returns or sets a value indicating whether the Minimize button is displayed in the caption bar of the form.
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
11/105
Description
Returns or sets the minimum size the form can be resized to. Returns a value indicating whether this form is displayed modally.
MaximumSize MdiChildren
Name
Opacity OwnedForms
12/105
12/11/2013
Form Properties
Property
StartPosition Text TopMost TransparencyKey Visible Width WindowState
Form Methods
Methods Acticate Description Activates the form and gives it focus.
Description
Returns or sets the starting position of the form at run time. Returns or sets the text in the title bar. Returns or sets a value indicating whether the form should be displayed a topmost form. Returns or sets the color that will represent transparent areas of the form. Returns or sets whether the form is visible. Returns or sets the width of the form (in pixel) Returns or sets the forms window state.
AddOwnedForm
BringToFront Close Focus Hide LayoutMdi
RemovedOwnedForm SendToBack
13/105
Form Methods
Methods Description Sets the bounds of the form in desktop coordinates. Sets the location of the form in desktop coordinates.
Show
ShowDialog UpdateDefaultButton
15/105
16/105
Form Events
Description
Activated
Click Closed Closing Deactivated DoubleClick Enter
KeyPress
Load MouseMove Move Paint Resize
Working with Windows Forms and Controls Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
17/105
Occurs when the key is pressed while the form has focus.
Occurs before a form is displayed for the first time. Occurs when the mouse pointer is moved over the form. Occurs when the is moved. Occurs when the form is redrawn. Occurs when the form is resized.
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
18/105
12/11/2013
Example :
Form1.Size = New Size(100, 100) Form1.Location = New Point(0,0)
19/105
20/105
Example:
Form1.Visible = False Button1.Visible = True
To show and hide forms and controls, use the Show and Hide methods. Example:
From1Hide() Form1.Show()
Working with Windows Forms and Controls
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
21/105
22/105
Syntax:
Function Msg (Pro mpt As Objec t [, Buttons _As MsgBoxStyle = Msg BoxStyle.OKO nly] [, Titl e_As Objects = Nothing ]) As MsgBox ResultArgume nts
23/105
24/105
12/11/2013
Description
Ok Bbutton only (default) Ok and Cancel buttons. Abort, Retry , and Ignore buttons. Yes, No, and Cancel buttons.
Description
DefaultButton3
ApplicationModal
YesNo
RetryCancel Crtical Question Exclamation Information
MsgBoxRtlReading
MsgBoxHelp
Cancel
Abort Retry Ignore Yes No
28/105
Descriptions
T he message box contains Abort, Retry , and Ignore buttons. T he message box contains an OK button.
Fun cti on Sh ow (By Va l t ext A s S tr ing ,_B yV al cap ti on As Str ing , ByV al bu tto ns As Ms gB oxB utt on s, Msg Bo x ico n A s M es sag eBo xI con , B yV al de fau ltB ut ton As Mes sag eBo xD efa ult Bu tto n, By Val o pti ons A s Mes sge Box Op tio ns) A s D ial og Re su lt
OKCancel
RetryCancel YesNo YesNoCancel
29/105
12/11/2013
Excalamation
The message box contains a symbol consisting of an exclamation point in a triangle with a yellow background.
T he message box contains a symbol consisting of a white X in a circle with a red background.
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
Hand
Warning
31/105
32/105
33/105
Example:
Pri vat e S ub Bu tto n3 _Cl ick (B yVa l sen der A s Sys tem .Ob je ct, By Va l e As S yst em .Ev ent Ar gs) Han dle s B ut ton 3.C li ck Tex tBo x1. Te xt = " Yo ur nam e is " & Inp utB ox( "P lea s t yp e y our n ame ." ) End Su b
Working with Windows Forms and Controls Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
35/105
36/105
12/11/2013
MDI Applications
Multiple Document Interface (MDI) Windows can display multiple child windows inside them MDI Application Foundation of MDI Application Contains the MDI child windows To create the MDI parent form Set the IsMDIContainer property of the first form to True To manage the MDI children Use the Show and Hide Methods
Working with Windows Forms and Controls Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
Select the windows Form icon from the Templates box then click Add
37/105
38/105
Text Box
Window s Forms Text Box control
Used to get input form the user to to display text
Example of TextBox
Types of TextBox
Standard Textbox Rich text
Working with Windows Forms and Controls
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
39/105
40/105
TextBox Properties
Property
TextBox Properties
Description Returns the maximum number of characters allowed in the text box. The default value is 0, meaning that there is no limit on the length. Gets or sets a value indicating whether this is a multiline TextBox control. Gets or sets the character used to mask characters of a password in a single-line TextBox control. MaxLength
Property AcceptsReturn
Description Returns or sets a value indicating whether pressing Enter in a multiline TextBox control creates a new line of text in the control or activates the default button for the form.
Multiline PasswordChar
BackColor
BorderStyle CharacterCasing
Gets or sets a value indicating whether the contents of the TextBox control can be changed. Gets or sets which scroll bar should appear in multiline TextBox control.
Gets or sets the expected length of the specified text box, in characters. The default value for the size of a text box is 0, which means that the browser will use its default text box length.
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
Font
ForeColor
41/105
42/105
12/11/2013
TextBox Properties
Property Text TextAlign Description Returns or sets the text content of the TextBox control. Returns or sets how text is aligned in a TextBox control.
WordWrap
Returns or sets a value indicating whether the text content wraps within a multiline text box.
Example:
strText = TextBox1.Text TextBox1.Text = strText
43/105
44/105
TextBox Methods
Methods AppendT ext Description Appends text to the text currently in the text box.
Clear
Copy Cut Paste
Select
SelectAll Undo
45/105
46/105
True
48/105
12/11/2013
Buttons
Windows Forms Text Box control
Allows the user to click it to perform an action Can display both text and images When clicked, it looks like it was pushed and released
Buttons Properties
Property DialogResult Description Returns or sets a value returned to the parent form when the button is clicked. Returns or sets whether the button has a flat appearance. Returns or sets an image displayed Returns or sets the alignment of the image in a button. Returns or sets alignment of the text in the button.
Example of Button
49/105
50/105
Buttons Methods
Method Description
Buttons Events
Ev ents Description
Click
DragDrop
DragEnter
DoubleClick GotFocus Keypress KeyUp KeyDown
Click
Command
52/105
Label
Displays read-only text on a form Displays text on the client area of a form
Example:
Pub lic Cl as s F orm 1 Dim iT ime s As int eg er = 0 Pri vat e S ub Bu tto n1 _Cl ick (B yVa l sen der A s Sye tem .Ob je ct, By Va l e As S yst em .Ev ent Ar gs) Ha nd els But ton 1.C li ck
Example of a Label
iti mes = it Tim es + 1 But ton 1.T ex t = "I w as Cli ck ed " & i tim es & "ti me s"
End Su b End Cl ass
Working with Windows Forms and Controls Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE Working with Windows Forms and Controls Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
53/105
54/105
12/11/2013
Label Properties
Properties
AutoSize
Label Properties
Properties
ImeMode
Description
Returns or sets a value indicating whether the control is automatically resized to display its entire contents.
Description
Returns or sets the Input Method Editor (IME) mode supported by this control.
BerderStyle
FlatStyle Image
T abStop
T ext T extAlign UseMnemonic
Returns or sets a value indicating whether the user can tab to the Label.
Returns or sets the text content of the label control. Returns or sets the alignment of text in the label. Returns or sets a value indicating whether the control interprets an ampersand character (&) in the controls Text property to be an access key prefix character.
ImageAlign
ImageIndex ImageList
56/105
Label Method
Methods Description
Aligning Text
ContentAlignment. BottomCenter BottomLeft BottomRight MiddleCenter MiddleLeft Description Vertically aligned at the bottom, horizontally aligned to the center. Vertically aligned at the bottom, horizontally aligned to the left. Vertically aligned at the bottom, horizontally aligned to the right. Vertically aligned at the middle, horizontally aligned to the center. Vertically aligned at the middle, horizontally aligned to the left.
Dispose
DrawImage Equals
57/105
58/105
Aligning Text
ContentAlignment. Description
Check Box
Windows Forms Check Box
Indicates a Boolean such as Yes/No or True/False Consists of a small square with a text string next to it.
MiddleRight
T opCenter T opLeft T opRight
Example of a CheckBox.
59/105
60/105
10
12/11/2013
CheckBox Properties
Properties
Appearance AutoCheck
CheckBox Properties
Properties
T extAlign
Description
Returns or sets the appearance of a CheckBox control. Returns or set a value indicating whether the Checked or CheckState values and the CheckBoxs apperance are automatically changed when the CheckBox is clicked. Returns or sets the horizontal and vertical alignment of the check mark on a CheckBox control. Returns or sets value indicating whether the CheckBox control is checked. Returns or sets the state of the CheckBox.
Description
Returns or sets the alignment of the text label associated with the CheckBox control. Returns or sets a value indicating whether the CheckBox will allow three check states rather than two.
ThreeState
61/105
62/105
CheckBox Methods
Methods AddAttributesToRen der LoadPostData Description Adds the HT ML attributes and styles of a CheckBox control to be rendered to the specified ouput stream. Processes the postback data for the CheckBox control. On-Click
CheckBox Methods
Methods Description Raises the Click event. Registers client script for generating postback prior to rendering on the client if AutoPostBack is True. Process a mnemonic character. Displays the CheckBox on the client. Saves the changes to the CheckBox view state since the time the page was posted back to the server.
LoadViewState
OnApperanceChanged OnCheckedChanged
63/105
64/105
CheckBox in a Code
Example:
Pub lic Cl as s F orm 1 Pri vat e S ub Ch eck Bo x1_ Cli ck (By Va l s end er As Ob je ct, ByV al e A s Sys tem .E ven tAr gs ) H an dle s C he ckB ox1 .c lic k If Che ckB ox 1.C hec ke d = Tr ue Th en Che ckB ox1 .T ext = "C hec ked "
Radio Button
Windows Forms Radio Buttons
Also called as Option Buttons Are typically grouped with other RadioButtons Only one of the controls in the group can be checked at one time.
Example of a RadioButton.
Els e
Che ckB ox1 .T ext = "U nch eck ed " End If End Su b End Cl ass
65/105
66/105
11
12/11/2013
RadioButton Properties
Properties Description
RadioButton Methods
Methods LoadPostData Description Prcoeeses postback data for the RadioButton control.
Appearance
AutoCheck
OnCheckedChange
OnClick OnEnter
CheckAlign Cheked
OnHandleCreated OnMouseUp
OnPreRender PerformClick
67/105
68/105
RadioButton in a Code
Example:
Pub lic Cl as s F orm 1
List Box
Windows Forms List Box Control
Controls that display a list of items, allowing the user to select one or multiple items.
Pri vat e S ub Ra dio Bu tto n1_ Ch eck ed Cha nge d( ByV al se nde r As Sys tem .Ob je ct, By Va l e As S yst em .Ev ent Ar gs) Ha nd les Rad ioB utt on 1.C lic k If Rad ioB ut ton 1.T ex t = Tr ue Th en
Rad ioB utt on 1.C hec ke d = Fa ls e Els e Rad ioB utt on 1.C hec ke d = Tr ue End If End Su b End Cl ass
Example of a ListBox.
69/105
70/105
Description Returns or sets whether the list box supports multiple columns.
Returns or sets whether the vertical scrollbar is always visible. Returns collection containing the (zero-based) indices of all currently selected items. Returns collection containing the currently selected items in the list box. Returns or sets the method in which items are selected in the list box. Returns or sets whether the items in the list box are sorted alphabetically . Returns or sets the index of the first visible item in the list box.
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
72/105
ColumnWidth
DrawMode HorizontalExtent HorizontalScollBar IntegralHeight ItemHeight Items
Returns or sets the width of the columns (only for multicolumn list boxes).
Returns or sets the drawing mode for the control. Returns or sets the width by which the horizontal scroll bar of ListBox can scroll. Returns or sets a value indicating whether a horizontal scroll bar is displayed in the control. Returns or sets whether the list box should resize to avoid showing partial items. Returns or sets the height of list box items. Gets or sets a collection od the list box items.
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
12
12/11/2013
ClearSelected
73/105
74/105
75/105
76/105
Combo Box
Windows Forms Combo Box Control
Combines of a text box and a drop-down list User can enter text or select an item from a drop-down part
Example of a ComboBox
DropDownHeight DropDownStyle DropDownWidth DroppedDown
77/105
78/105
13
12/11/2013
Description
Return or sets a value indicating whether the control should resize to avoid showing partial items.
Description
Gets or sets the index specifying the currently selected item.
ItemHeight
Items MaxDropdownItems
SelectedT ext
SelectionLength SelectionStart Sorted
Gets or sets the text that is selected in the editable portion of a ComboBox.
Gets or sets the number of characters selected in the editable portion of the ComboBox. Gets or sets the starting index of text selected in the ComboBox. Get or sets a value indicating whether the items in the ComboBox are sorted.
MaxLength
Returns or sets the maximum number of characters possible in the editable portion of a ComboBox.
79/105
80/105
FindString
FindStringExact GetItemHeight Select SelectAll
Finds the first item in the ComboBox that start with the specified string.
Find the first item that exactly matches the specified string Returns the height of an item in the ComboBox. Select a range of text. Select all text in the text box of the combo box.
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
81/105
82/105
83/105
84/105
14
12/11/2013
Picture Box
Windows Forms Picture Box Control
Displays graphics in bitmap, icon, JPEG, GIF, or other image formats
Picture Box
Creating a PictureBox.
Add a PictureBox to a form Use the Image property to specify the image to be displayed At design time, select the Image property from the Properties window Click the ellipsis() button
Example of a PictureBox
Click the Import button then select picture from the appearing dialog box. Click Open then click OK to load the selected picture.
Working with Windows Forms and Controls Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
85/105
86/105
Picture Box
Creating a PictureBox ( cont..) At runtime, an image can be loaded into the Image property by assigning an Image object to that property. To create an image object from an image file, use the image classFromFile method as follow s:
PictureBox1.Image = Image.FromFile_("c:\vbnet\pictureBox\waterlilies.j pg")
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
Picture Box
Sizing PictureBoxes to Contents
PictureBoxSizeMode.AutoSize PictureBoxSizeMode.Center Image PictureBoxSizeMode.Nor mal PictureBoxSizeMode.StretchImage PictureBoxSizeMode.Zoom
87/105
88/105
PictureBox Properties
Properties BorderStyle Description Indicates the border style for the control. ImeMode
PictureBox Properties
Properties Description Gets or sets the Input Method Editor (IME) mode supported by this control. Gets or sets a value indicating whether displayed in the PictureBox control while the main image is loading. Gets or sets a value indicating whether controls elements are aligned to support locales using right-to-left fonts. Indicates how the image is displayed. Gets or sets the tab index value. Gets or sets the text of the PictureBox. Gets or sets a value indicating whether an image is loaded synchronously .
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
90/105
DefaultIMEMode
ErrorImage
Gets a value indicating the mode for Input Method Editor (IME) for the PictureBox.
Gets or sets the image to display when an error occurs during the image loading process or if the image load is cancelled. Gets or sets the image that the PictureBox displays.
Image
Image Location
Gets or sets the path for the image display in the PictureBox.
89/105
15
12/11/2013
PictureBox Methods
Methods CancelAsync Dispose Load LoadAsync T oString Description Cancels an asynchronous image load. Releases all resources used by the PictureBox. Displays an image in the PictureBox. Loads an image synchronously . Returns a string that represents the current PictureBox control.
Tool Tip
Windows Forms Tool Tip Control
Small rectangular windows with a yellow background Displays helpful text when the mouse hovers over some visual element in a program.
91/105
92/105
Tool Tip
Creating ToolTips
not created as standard controls at design time
Activ e
ToolTip Properties
Properties Description Gets or sets a value indicating whether the ToolT ip is currently active. Gets or sets the automatic delay for the ToolT ip. Gets or sets the period of time the ToolT ip remains visible if the pointer is stationary on a control with specified ToolT ip text. Gets or sets the background color for the ToolT ip.
Appears in the component tray To assign text to a ToolTip, use this SetToolTip method: ToolTip1.setToolTip(Textbox1, This is a textbox.")
AutomaticDelay AutoPopDelay
BackColor
CreateParams ForeColor
InitialDelay
Gets the creation parameters for the ToolTip window Gets or sets the foreground color for the ToolTip.
Gets or sets the time that passes before the ToolT ip appears.
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
94/105
93/105
ToolTip Properties
Properties IsBalloon OwnerDraw Description Gets or sets a value indicating whether the ToolT ip should use a balloon window. Gets or sets a value indicating whether the ToolT ip is drawn by the operating system or by code that you provide. Gets or sets the length of time that must transpire before subsequent ToolT ip windows appear as the pointer moves from one control to another. Properties CanExtend Dispose Finalize
ToolTip Methods
Description Returns true if the ToolTip can offer an extender property to the specified target component. Releases the resources used by the ToolTip. Releases the unmanaged resources and performs other cleanup operations before the Cursor is reclaimed by the garbage collector. Retrieves the ToolTip text associated with the specified control. Removes all ToolTip currently associated with the ToolTip component.
ReShowDelay
SetT oolTip
StopTimer T oString
Working with Windows Forms and Controls Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
95/105
16
12/11/2013
Timer
Windows Forms Timer Control
used to create and handle periodic events added to the component tray at design time
Timer
Creating a Timer
add a timer to a form to set how often you want the timer to generate Tick events
set the interval property (milliseconds the default is 100) a code can be executed in a handler for this event each time a T ick event occurs
Example of a Timer
timer use the computers built-in clock timer ticks wont happen faster than 18 ticks per second
97/105
98/105
Timer Properties
Properties AutoReset Description Gets or sets a value indicating whether the T imer should raise the Elapsed event each time the specified interval elapses or only after the first time it elapses. Gets or sets a value indicating whether the T imer should raise the Elapsed event. Gets or sets the interval at which to raise the Elapsed event. Gets or sets the site that blinds the T ime to its container in design mode. Methods BeginInit
Timer Methods
Description Begins the run-time initialization of a T imer that is used on a form or by another component.
ChangeMode
Close Dispose
Changes the start time and the interval between method invocations for a timer.
Releases the resources used by the T imer. Releases all resources used by the current instance of Timer.
EndInit
Start
Ends the run-time initialization of a Timer that is used on a from or by another component.
Starts raising the Elapsed event by setting Enabled to T rue
T ag
Stop T oString
Stops raising the Elapsed event by setting Enabled to False Returns a string that represents the Timer.
Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
100/105
99/105
Timer Events
Ev ents Disposed Elapsed Description Adds an event handler to listen to the Disposed event on the component. Occurs when the interval elapses.
Built-in Dialog
Same dialogs used by other Window s application Built-in Dialogs available through Window s controls
Open File Save File Folder Browser Font Color Print Preview Page Setup Print
101/105
Example:
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Mybased.Load Timer1.enabled = True End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
102/105
17
12/11/2013
Built-in Dialog
allow s the user to specify w hat files to open DialogResult.Abort
DialogResult.Abort DialogResult.Cancel DialogResult.Ignore DialogResult.No DialogResult.None DialogResult.OK DialogResult.Retry DialogResult.Yes
Working with Windows Forms and Controls Prepared by: Jefferson A. Costales, MIT, IBM-CDA, MCTS, ZCE
103/105
Built-in Dialog
OpenFileDialog
allows the user to specify what files to open Example:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then MessageBox.Show (OpenFileDialog1.FileName) End If End Sub
104/105
Built-in Dialog
Save File Dialogs
allow user to give a program the name and/or location of the file where data will be saved
Example:
Pri vat e S ub Bu tto n1 _Cl ick (B yVa l sen der A s S yst em .Ob je ct, ByV al e A s Sys tem .E ven tAr gs ) H an dle s B ut ton 1.C li ck If Sav eFi le Dia log .S how Dia lo g1. Sh owD ial og () = Dia log Res ul t.O K T he n Tex tBo x1. Te xt = S av eFi leD ia log 1. Fil eNa me
End If End Su b
105/105
18