530 likes | 1.69k Views
VB.NET Controls. ITE 285. Topics. Introduction to controls Control properties Control events Some common controls More controls. Introduction to Controls . Visual objects that are placed on a form to enable customized activities
E N D
VB.NETControls ITE 285
Topics • Introduction to controls • Control properties • Control events • Some common controls • More controls
Introduction to Controls • Visual objects that are placed on a form to enable customized activities • Built-in controls defined in Windows Form class library, and are defined • with ToolBox and Form Designer • or strictly with code • Build your own controls (ITE 370) • Windows Control Library project • Add a control with Add Reference
Categories of Controls w/Prefixes • Text edit (TextBox—txt___) • Text display (Label—default name or lbl___) • Selection from a list (ListBox—lst___, ComboBox—cbo___, …) • Graphic display (PictureBox) • Graphic storage (ImageList) • Value setting (CheckBox—chk___, CheckListBox, RadioButton,…) • Date setting (DateTimePicker, MonthCalendar) • Dialog boxes (OpenFileDialog, PrintDialog…) • Menu controls (MainMenu, …) • Commands (Button—btn___, LinkLabel…) • Grouping other controls (GroupBox, TabControl, Panel)
Control Properties • Many controls share some common properties • Name, Text • Size.Height & Width, Location.X &Y, Dock • CanFocus, ContainsFocus, Focused • Visible & Enabled determine availability to user • Font properties affect text display in the control • Font, size, bold, etc. • Tab Index & Tab Stop
Control Events • Many controls share a common set of events to which they can react • Click & DoubleClick • MouseMove, MouseDown, MouseUp, MouseWheel, MouseHover, MouseLeave • KeyPress, KeyDown, KeyUp • Resize • DragDrop
Focus and Validation Event Sequence • Enter • GotFocus • Leave • Validating • Validated • LostFocus
Adding Controls to Forms • Double Click Control in Toolbox • Click, Drag and Drop • Copy and Paste Existing Controls
Properties Text &Cancel ®Cancel && ® & DialogResult Produces a click result when used in a modal form Ok, Cancel, etc. ‘* example event procedure Private Sub btnBack_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnBack.Click ‘* Exits this form. Me.Close() End Sub Buttons
Labels and LinkLabels • Use labels and link labels for text display • Text property (no more Caption) defines text to display • User cannot change a label • LinkLabel enables hyperlinks • Links.Add inserts a hyperlink into text • Must write event-handler to invoke browser • See example
LinkLabel Example linkLabel1.Text = "Click here to get more info." ‘* Create new link using Add method of LinkCollection class. ‘* 1st parameter is the start position of hyperlinked text ‘* 2nd parameter is no. of chars to hyperlink ‘* 3rd parameter is URL linkLabel1.Links.Add(6, 4, "www.microsoft.com") Private Sub linkLabel1_LinkClicked(sender As Object, _ e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ Handles LinkLabel1.LinkClicked ‘* Determine which link was clicked within the LinkLabel. linkLabel1.Links(linkLabel1.Links.IndexOf(e.Link)).Visited = True ‘* Display the appropriate link based on the value of the LinkData property ‘* of the Link object. System.Diagnostics.Process.Start(e.Link.LinkData.ToString()) End Sub ‘* linkLabel1_LinkClicked
Text box allows user to enter or edit data Properties MaxLength, MultiLine AcceptsTab, AcceptsReturn, WordWrap ScrollBars Properties (cont) SelectionStart, SelectionLength, SelectedText Events TextChanged Text Boxes: Text Edit and Display Private Sub TextBox1_GotFocus(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles TextBox1.GotFocus '* Select all text in the text box TextBox1.SelectionStart = 0 TextBox1.SelectionLength = TextBox1.Text.Length End Sub
Text Box Example The following example creates a multiline TextBox control with vertical scroll bars. This example uses the AcceptsTab, AcceptsReturn, and WordWrap properties to make the multiline text box control useful for creating text documents. ' Create an instance of a TextBox control. Dim textBox1 As New TextBox() ' Set the Multiline property to true. textBox1.Multiline = True ' Add vertical scroll bars to the TextBox control. textBox1.ScrollBars = ScrollBars.Vertical ' Allow the RETURN key to be entered in the TextBox control. textBox1.AcceptsReturn = True ' Allow the TAB key to be entered in the TextBox control. textBox1.AcceptsTab = True ' Set WordWrap to allow text to wrap to the next line. textBox1.WordWrap = True ' Set the default text of the control. textBox1.Text = "Welcome!“ Source: Adapted from MSDN Library-April 2002
ComboBox Properties Text DropDownStyle Simple Dropdown DropdownList Sorted Run Time Properties Items collection Methods Items.Clear .Add .Remove cboChoice.Items.Clear() cboChoice.Items.Add("First") cboChoice.Items.Add("Second") cboChoice.Items.Add("Third") cboChoice.Items.Add(TextBox1.Text) cboChoice.Items.Remove("Third") List Selection Controls
List Boxes: Items and Selected__ • Items is an indexed collection of items in the list txtName.text = (lstEmp.Items(2)) • SelectedIndex property is the zero-based number of the selected item in the list • If user clicked the third item SelectedIndex will be 2 • Has value of -1 if nothing selected • These two lines do the same thing: txtName.text(cboChoice.SelectedItem) txtName.text(cboChoice.Items(cboChoice.SelectedIndex))
CheckBox Control • CheckState property • Checked • Unchecked • Indeterminate (checked but grayed) • Checked property • True or False • Text property displays built-in caption If chkMarried.checkState = CheckState.checked then ‘* code to execute End if If chkMarried.checked = true then ‘* code to execute End if
Radio Button • Checked state • True or False • Text Property displays a built-in caption • Should be contained inside a GroupBox – Can select one option from available Radio Buttons If rdbAdult.checked = True then ‘* code to execute Else If rdbChild checked = True then ‘* code to execute Else ‘* if there is a third option ‘* code to execut End if
Timer Control • Executes code after a specified interval • Timer Event • Unique event that executes after the interval specified in the interval property expires • Interval Property • 0 - 65,535 milliseconds • 0 - means disabled • 60,000 milliseconds is one minute • Enabled property must also be true for timer to work. • Timer control is never visible at run time • Stored in Component Tray at design time
Best Control??? • Fewest Keystrokes • Provides Essential Information • Compensates for Limited Short-Term Memory • Group Related Controls to Help User Understand what is needed • TextBox is often the WORST Control • Most Keystrokes • User Must Remember • Slow • Prone to Data Entry Error
VB.NETControls ITE 285