CIS 338: VB.NET Components
Dr. Ralph D. Westfall April, 2011
Components = Building Blocks
form is the fundamental VB.NET component = interface between user and other code
parts of a form control box (click or double-click upper left
corner) Text (title) property title bar small buttons: minimize, maximize, close
follow along on laptops
Form Methods
if only one form, shows at Startup can stop it by clicking small Close (x) button
at top right, or run Close in code do NOT use [frmName].Hide with only one
form! [frmName.]Close closes form, and also shuts
down the project if there is only one form End shuts down the project if there is more
than one form
Controls
visible objects on forms or elsewhere built into Visual Basic.NET or available
from 3rd parties examples of built in form controls
Button, TextBox, Label, CheckBox, RadioButtons, ListBox, ComboBox, etc. (view names on Visual Studio.NET Toolbox buttons)
Commonly Used VB.NET Controls
Label – puts text on a form TextBox – accepts inputs, displays outputs (Command) Button – causes code to run CheckBox – accepts Yes/No inputs (Option) RadioButtons – selects only one
from multiple choices ListBox – select from a list of choices ComboBox – select from list or enter value
Sample Properties of Controls
properties = variables e.g., (Name) of form or control Location.X .Y = # of pixels from left
and top edges of “container” sets position on screen container = what item is inside a form is the container for Button,
etc. Size: Width Height are also in pixels Anchor – attaches to side(s) of the form
Sample Properties of Controls - 2
more properties Enabled – True means user can change
for a Text Box that shows a calculated output, set Enabled = False (or show output in a Label instead)
Visible = True (or False) guess what this does where use? why? 'code
Using Properties of Controls
can set during development (“design time”)can also use code to change (“run time”) example: disable a Go to Next Form
button until after user has entered inputs and clicked Enter button
use “dot notation” to reference properties txtPayment.Text = CStr(decPayment)
decPayment = CDec(txtPayment.Text) conversions needed with Option Strict On
Some Label Control Properties
can set in Properties window or in code AutoSize – label’s “box” expands to fit text Font – size (points), font (typeface), style
(bold, italic, etc.) ForeColor – text color BackColor – background color (behind text)
can use a white background color to make Label look like a TextBox (but why?)
Some TextBox Control Properties
setting content value (default) txtName.Text = "Bubba"
for handling larger quantities of text MultiLine – True enables text wrapping
goes to next line when reaches right side
ScrollBars determines if a scroll bar is used on a text box, and what type(s) (None | Horizontal | Vertical | Both)
(Command) Button Properties
can use & in Text property to set shortcut key [Alt] e runs &Exit, [Alt] r runs G&rin may see an underline if & is before a
letter in the middle of the word e.g., Grin
Enabled if =True button is usable, but not if
=False when would you set it =False?
CheckBox Properties
2 possible Checked (Boolean) Values: False (not checked) True (checked)
3 possible CheckState Properties: Unchecked Indeterminate (checked and grayed;
try it) Checked
(Option) RadioButtons
only one radio button at a time can be selected clicking one "unclicks" the other different than clicking 2+ checkboxes
can use a GroupBox (container) to set up separate groups of radio buttons user can only select one from each group
(Option) RadioButtons - 2
Checked property is either True or Falsetwo ways to run (checkboxes also) can use [name]_Click method to cause
code to run as soon as user clicks one can use If … Then … Else or a Select
Case structure to test a user choice later, instead of triggering an immediate action
GroupBoxes (are Containers)
can put other controls in a GroupBoxneed to 1st put GroupBox on form, then put controls into it GroupBox will not include controls already
on form, even if you put it over them
can change appearance Text – present or absent FlatStyle – Standard, Popup, or System ForeColor, BackColor, BackGroundImage
ListBoxes
2 different kinds of ListBox ListBox: selection is highlighted CheckedListBox: selection is checked
lst[name].SelectedIndex property item number of selected item in list (zero-
based, starts at 0, but is -1 if unselected)
lst[name].Items.Count property (2 dots) total number of items in ListBox 'code
Other List Box Properties
can use Sorted = True to sort, but this only sorts by left side of list if leftmost characters are the same
numbers are handled as text characters (lexical sort), so don't sort correctly e.g.,
119 1-9 (or a-9)5649 2-64 (or b-64, etc.)94 3-1199 4-5649 'prefix gives order
'or use leading zeros (0009, 0064 …)
ListBox Multiple Selection
0, 1, or many based on SelectionMode works on ListBox, not on CheckedListBox
0 None – disabled 1 One – can select only one item 2 MultiSimple – can select with mouse,
or arrow key followed by spacebar 3 MultiExtended – also use Shift, Ctrl
keys if no item is selected, then SelectedIndex
value is -1 regardless of SelectionMode
ListBox Multiple Selection - 2
total number of selected items = lst[name].SelectedItems.Count
VB.NET creates a collection of specific selected items can cycle through in a for loop e.g., For Each item inListBox1.SelectedItems MsgBox(item)Next
ListBox Properties that Are Useable in Code
.SelectedItem property shows user's 1st selected item (simple approach)str[variable] = lst[name].SelectedItem
.SelectedIndex property shows the sequential identifier of selected item collection of items in the ListBox (1st) selected item # is lst[name].ListIndex
lst[name].Items(lst[name].ListIndex)
Programmable List Box Properties - 2
can set default selection in code with SelectedIndex property 1st item in ListBox has SelectedIndex
of 0 2nd item has 1 SelectedIndex of -1 means that none
is selectedlst[name].SelectedIndex = 2 'which item?
can't set this in Properties window
ListBox & Collection Together
lst[name].Items(lst[name]._SelectedIndex)orlst[name].Items.SelectedItem
als[name](lst[name]._ SelectedIndex)
Joelst[name].Items(Items(lst[name].SelectedIndex)
123456789als[name](Items(lst[name].SelectedIndex)
Analst[name].Items(1) ' hard coded
345678901als[name](1) ' hard coded
Adding Items to ListBox
using Items editor via Properties window type in items, then click OK can also paste into the editor window
programmaticallylst[name].Items.Add("CIS")lst[name].Items.Add(str[variable]) can load values into a parallel ArrayList
(unlike an array, don't need to use a counter)als[name].Add(191919) 'code
ComboBox
like a combination (combo) of a TextBox and a ListBox user can type information into the
TextBox part to either enter an item not in the list or scroll down to item(s) in the (Sorted = True) list that start with the same letters
properties and methods are usually same or similar to those of a ListBox
ComboBox Properties
DropDownStyle property 0 Simple (TextBox on top of a ListBox)
can select an item or type in something not in the ListBox part (Prof. Westfall's FAVORITE!!)
1 Dropdown (ListBox drops down on a click) can type in an item or initial letter(s), but don't see
any match from the ListBox until it drops down 2 DropdownList (ListBox drops down on click)
can't type anything in the TextBox part, but must click to drop down the ListBox to select an item
ComboBox Properties - 2
Text property (for Styles Simple or DropDown) can set it as a default selection, or
make it be an instruction to the user when it runs, or leave it blank
code can access .Text property to get user inputs that are not in the list(box)
ComboBox Properties - 3
can add objects to a ComboBox and make one attribute the DisplayItemcan then access other attributes of selected item e.g., select name, get Social Security #
'code
Controlling Tab Sequence
if a control's TabStop property = True, user can use tab key to move from one control to the next sequence is initially determined by
order that item was added to form set TabStop = False for items that will
not be used for input (e.g. a Label or a TextBox or ListBox that displays outputs)
Changing Tab Sequence
can change by changing TabIndex use a large number to make an item last VB 6 changed tab indices to compensate
for other changes Visual Basic.NET seems to keep track of
this in another way tutorial (click Types of Windows>Tab
Order)
set TabStop=True for Radio Buttons
Clicking to Change Tab Stops
in VB.NET, can mouse click items to change their tab sequence View>Tab Order then click each item in the order that
you want for the tab sequence repeated clicks on same item cycles
through the TabIndex values then click View>Tab Order (toggle it
off) to set new values
Clicking to Change Tab Stops - 2
note that Labels have a TabIndex property too, but not a TabStop property so can't tab to them could click them repeatedly to set all the
values to 0, but not necessary
note that tab values in View>Tab Order don't match with TabIndex properties
Clearing Common Controls
sometimes need to clear (remove contents) of a control txtName.Text = Nothing or txtName
= "" chkComputerOwner.Checked = False rdoCategory.Checked = False lstPersons.ClearSelected() cboCities.SelectedItem = Nothing
Design Tips for Controls
hold down left mouse button and use cursor to select multiple controls or hold Ctrl key and click items
after multiple items are selected, can move them as a groupcan change properties of all selected controls by changing in one control e.g., Font on Labels, Format>Align
Visual Studio's Format Menu
horizontal or vertical spacing increase/decrease, make equal,
remove
make same size size to grid (all corners on grid points)
alignment tops, lefts, etc.
Showing Grid Lines on Form
it can be helpful in designing a form to have grid lines showing during designTools>Options>Windows Forms Designer>General>Layout Settings>click Show Grid option SnapToGrid LayoutMode forces controls
to space themselves based on grid
Exercise
put a Button on a Form, and several other input controls covered todayclick the Button, write code to put the keyboard inputs from one control into a different kind of control that takes Texttest, and then repeat with another pair of controls