1 / 20

VB.NET Controls

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

elon
Download Presentation

VB.NET Controls

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VB.NETControls ITE 285

  2. Topics • Introduction to controls • Control properties • Control events • Some common controls • More controls

  3. 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

  4. 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)

  5. 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

  6. 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

  7. Focus and Validation Event Sequence • Enter • GotFocus • Leave • Validating • Validated • LostFocus

  8. Adding Controls to Forms • Double Click Control in Toolbox • Click, Drag and Drop • Copy and Paste Existing Controls

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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))

  16. 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

  17. 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

  18. 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

  19. 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

  20. VB.NETControls ITE 285

More Related