Professional Documents
Culture Documents
Quick Recap
Using Development Environment Features Creating a Windows Application Creating a Web Application Creating a Console Application Visual basic Statements Data Types Using Variables Why use windows form Windows form class Hierarchy Forms Method/ Event/ Properties Creating MDI forms Creating Inherited forms
Windows Forms: Text Boxes, Rich Text Boxes, Labels, and Link Labels
Text Boxes
box-like controls in which you can enter text. Text boxes can be multiline, have scroll bars, be read-only, and have many other attributes The TextBox class is derived from the TextBoxBase class, which is based on Control:
Object MarshalByRefObject Component Control TextBoxBase TextBox Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click TextBox1.Text = "Hello from Visual Basic" End Sub
To work with part of the text in a text box, you select the text you want using three properties: SelectionLength Returns or sets the number of characters selected. SelectionStart Returns or sets the starting point of text selected; indicates the position of the insertion point if no text is selected. SelectedText Returns or sets the string containing the currently selected text; consists of a zero-length string ("") if no characters are selected.
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click TextBox1.SelectionStart = 0 TextBox1.SelectionLength = Len(TextBox1.Text) TextBox1.SelectedText = "Hello from Visual Basic" End Sub
Saving and Loading RTF Files from and to Rich Text Boxes
Private Sub Button6_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button6.Click RichTextBox3.SaveFile("text.rtf") RichTextBox1.LoadFile("text.rtf") End Sub
Labels
Labels usually are used to display text that cannot be edited by the user.
Object MarshalByRefObject Component Control Label
Link Labels
Link labels are new in VB .NET. They're based on the Label class, but also let you support Web-style hyperlinks to the Internet and other Windows forms
Object MarshalByRefObject Component Control Label LinkLabel
Creating a LinkLabel
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, _ ByVal e As ystem.Windows.Forms.LinkLabelLinkClickedEventArgs) _ Handles LinkLabel1.LinkClicked LinkLabel1.LinkVisited = True System.Diagnostics.Process.Start("www.coriolis.com") End Sub
Buttons
There is no more popular control in Visual Basic than buttons, with the possible exception of text boxes. Buttons are the plain controls that you simply click and release. Besides using buttons in forms directly, they're very popular in dialog boxes. you can set the AcceptButton or CancelButton property of a form to let users click a button by pressing the Enter or Esc keys-even if the button does not have focus. And when you display a form using the ShowDialog method, you can use the DialogResult property of a button to specify the return value of ShowDialog. You also can change the button's appearance, giving it an image or aligning text and images in it as you like. You can even-make it look flat for a "Web" look, setting the FlatStyle property to FlatStyle.Flat. Or, you can set the FlatStyle property to FlatStyle.Popup, which means it looks flat until the mouse pointer passes over it, when the button pops up to give it the standard Windows button appearance.
Public Class Form1 Inherits System.Windows.Forms.Form Private Sub Button_Click(ByVal sender As System.Object, _ Dim WithEvents Button1 As Button ByVal e As System.EventArgs) Dim WithEvents Button2 As Button If sender Is Button1 Then Dim WithEvents Button3 As Button TextBox1.Text = "You clicked button 1" Friend WithEvents Button4 As Button End If Friend WithEvents TextBox1 As TextBox If sender Is Button2 Then 'Windows Form Designer generated code TextBox1.Text = "You clicked button 2" Private Sub Button4_Click(ByVal sender As System.Object, _ End If ByVal e As System.EventArgs) Handles End Sub Button4.Click Button1 = New Button() End Class Button2 = New Button() Button3 = New Button() Button1.Size = New Size(80, 30) Button1.Location = New Point(115, 20) Button1.Text = "Button 1" Button2.Size = New Size(80, 30) Button2.Location = New Point(115, 60) Button2.Text = "Button 2" Button3.Size = New Size(80, 30) Button3.Location = New Point(115, 100) Button3.Text = "Button 3" Controls.Add(Button1) Controls.Add(Button2) Controls.Add(Button3) AddHandler Button1.Click, AddressOf Button_Click AddHandler Button2.Click, AddressOf Button_Click AddHandler Button3.Click, AddressOf Button_Click
Disabling Buttons
you can-you can disable the button by setting its Enabled property to False when it's inappropriate to use that button
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Button1.Enabled = False End Sub
Checkboxes
Checkboxes are also familiar controlsyou click a checkbox to select it, and click it again to deselect it. When you select a checkbox, a check appears in it, indicating that the box is indeed selected. You use a checkbox to give the user an option, such as true/false or yes/no. The checkbox control can display an image or text or both.
Getting a Checkbox's State: Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click If CheckBox1.Checked Then Button1.Text = "The check mark is checked" End If End Sub Setting a Checkbox's State: Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click CheckBox1.Checked = True End Sub
You use the CheckState property to get or set the value of the three-state checkbox. The three states are: Checked A check appears in the checkbox. Unchecked No check appears in the checkbox. Indeterminate A check appears in the checkbox on a gray background.
Radio Buttons
Radio buttons, also called option buttons, are similar to checkboxesthe user can select and deselect themexcept for two things: they are round where checkboxes are square, and you usually use radio buttons together in groups.
Panels
You use panels to group other controls, usually to divide a form into regions by function. For example, you may have a menu form that lets the user select drinks in one panel and what they want on their sandwich in another. You can use grouping controls such as panels and group controls to make it clear which controls are associatedand it makes it easier to handle groups of controls at design time too, because when you move a panel, all the controls it contains are moved as well.
Group Boxes
Like panels, group boxes are used to provide a grouping for other controls. Group boxes are similar to panels, but, as mentioned above, only group boxes display captions and only the panels can have scroll bars. Group boxes display frames around their contained controls and can display text in a caption
List Boxes
list boxes display a list of items from which the user can select one or more. If there are too many items to display at once, a scroll bar automatically appears to let the user scroll through the list. In Visual Basic .NET, each item in a list box is itself an object.
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim intLoopIndex As Integer For intLoopIndex = 0 To ListBox1.Items.Count - 1 MsgBox(ListBox1.Items(intLoopIndex)) Next End Sub
Private Sub ListBox1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles ListBox1.Click TextBox1.Text = ListBox1.SelectedItem.ToString() End Sub
For Each Item In ListBox1.SelectedItems TextBox1.Text &= Item.ToString() Next
Combo Boxes
The Windows forms combo box control is used to display data in a drop-down combo box. The combo box is made up of two parts: The top part is a text box that allows the user to type in all or part of a list item. The other part is a list box that displays a list of items from which the user can select one or more.
SelectedIndexChanged Events
Private Sub ComboBox1_SelectedIndexChanged(ByVal _ sender As System.Object, ByVal e As System.EventArgs) _ Handles ComboBox1.SelectedIndexChanged Dim SelectedIndex As Integer SelectedIndex = ComboBox1.SelectedIndex Dim SelectedItem As Object SelectedItem = ComboBox1.SelectedItem TextBox1.Text = "Selected item text: " & SelectedItem.ToString() & _ " Selected index: " & SelectedIndex.ToString() End Sub
Picture Boxes
Picture boxes are used to display graphics from a bitmap, icon, JPEG, GIF or other image file type.
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click PictureBox1.Image = _ Image.FromFile("c:\vbnet\ch07\pictureboxes\image.jpg") End Sub
Scroll Bars
vertical or horizontal controls that display a scroll box or thumb that you can manipulate, and when you drag it to a new position, the value of the scroll bar changes, causing a corresponding action in the program.
Setting Scroll Bars' Minimum and Maximum Values Setting Up Scroll Bar Clicks (Large Changes) Setting Up Scroll Bar Arrow Clicks (Small Changes) Getting and Setting a Scroll Bar's Current Value
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load HScrollBar1.Minimum = 0 HScrollBar1.Maximum = 100 HScrollBar1.LargeChange = 10 HScrollBar1.SmallChange = 1 HScrollBar1.Value = 20 End Sub
Splitters
splitter controls let you resize controls, extending them as needed, or tucking them away when their job is over. You can use splitters to let the user resize controls. Here's how it works: you add a control to a form, then dock it. Next, you add a splitter and dock it to the same side of the same container, which places the control just before the splitter in the docking order. When you run the program, the splitter is invisible until the mouse passes over it, when the splitter changes the mouse cursor, indicating that the control can be resized,
Track Bars
Track bars are very much like scroll bars but differ in appearance. Track bars look more like controls you might see on stereos
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles TrackBar1.Scroll TextBox1.Text = "Track bar value: " & TrackBar1.Value End Sub
Pickers
There are two types of pickersdate-time pickers, and month calendar controls.
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged TextBox1.Text = "Date selected: " & DateTimePicker1.Text End Sub Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load DateTimePicker1.Format = DateTimePickerFormat.Custom DateTimePicker1.CustomFormat = "MMMM dd hh:mm:ss tt" End Sub Private Sub MonthCalendar1_DateSelected(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles _ MonthCalendar1.DateSelected TextBox1.Text = "Day of the month selected: " & _ MonthCalendar1.SelectionRange.Start.Day End Sub
Notify Icons
This is a pretty cool one, new to Visual Basic .NET. Notify icons let you display an icon in the status notification area of the Windows taskbar (in the indented panel at extreme right in the taskbar) called the Windows system tray.
Tool Tips
small windows that appear with explanatory text when you let the mouse rest on a control or window. That's what tool tips are used for to give quick help when the mouse rests on an item.
ToolTip1.SetToolTip(Button1, "This is a button") Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ToolTip1.SetToolTip(Me, "This is a form.") ToolTip2.SetToolTip(Button1, "This is a button") End Sub
Timers
Timers are also very useful controls, because they let you create periodic events. Strictly speaking, timers are no longer controls but components, and they do not appear in a window at run time.
Summary
Windows Forms: Text Boxes, Rich Text Boxes, Labels, and Link Labels Button Check box, radio box, panels Picture Boxes Scroll Bars Splitters Track Bars Notify Icons Tool Tips Timers