Visual Basic .NET/Controls
Like its predecessor, Visual Basic .NET excels in creating graphical user interfaces. Although the programmer still has the option of creating controls and setting their properties programatically (i.e., by hand writing the code), he usually will create many, if not all of the controls in the interface by selecting items from the toolbox and adding to a particular form. While working with forms, you can use the toolbox to drag different controls to the form you are designing, resize them and relocate them using the mouse, and set the control's properties in a corresponding properties window to quickly develop the user interface. Events handlers for each control's most common event can be quickly created by double-clicking on the control to create a new event handler and be send to that event handler in the code window.
Some of these controls are described below.
- 1 Visible controls
- 2 Other controls not visible at design time
A TextBox is used to get and display text, this one can be in several fonts, size and color. By default, a TextBox will only display text on a single line. If you require more than one line you can set its Multiline property to true.
If you need a TextBox for accepting a password you should set it's PasswordChar property to a character. The character you choose will be displayed in the TextBox instead of what is actually typed. Good characters to choose include * and ● and any letter you will type.
You can copy the selected text to the clipboard by using the TextBox.Copy method. Use the TextBox.Paste method to paste text into the Textbox from the clipboard. TextBox.Cut places the selected text into the clipboard and removes the text from the TextBox.
To restrict text
To restrict text from being entered into a TextBox create an event handler for the KeyPress event.
This example is for a TextBox named TextBox1. It will allow only numbers (0 - 9), 1 decimal point and 1 minus sign to be entered.
Private Sub SubName (ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Select Case Asc(e.KeyChar) Case 48 To 57, 8 Case 45 If InStr(sender.text, "-") <> 0 Or sender.selectionStart <> 0 Then e.Handled = True End If Case 46 If InStr(sender.Text, ".") <> 0 Then e.Handled = True End If Case Else e.Handled = True End Select End Sub
Labels are used to display text. Unlike the textbox, it is not meant to accept input from the user. Often a Label is used to describe another control, and is often used as a prompt for a Textbox.label display read-only text as far as the user is concerned
Buttons are controls that are usually raised that the user most often can click on to perform some action defined by the programmer. Once the programmer has added the button control to a form, he can define an event handler to perform an action when the button is clicked.
A check box indicates a two-way choice or state (true/false) which can be edited by the user. Check boxes are shown on the screen as a square box that can contain white space (for false) or a check mark (for true). Adjacent to the check box is normally shown a caption describing the meaning of the check box. Inverting the state of a check box is done by clicking the mouse on the button, or the caption. Visual Basic allows the programmer to set the caption through the check box's text property.
A radio button allows the user to choose exactly one of a predefined set of options. Radio buttons are arranged in groups of two or more and displayed on screen a list of circular holes that can contain white space (for unselected) or a dot (for selected). Each radio button can show a caption describing the choice that this radio button represents. This is done by setting the radio button's text property.
Can easily save Richtext files with Colours and Fonts. Has all the qualities of a textbox but with built in save method(richtextbox1.savefile(pathname)) and openfile(Richtextbox1.openfile(filename)).
Other controls not visible at design time
Some of the controls you can add while designing your form do not actually appear on the form, but you can still use the toolbox to add them to the form, where they are keep in a tray below the form for easy reference.
An OpenFileDialog displays the standard "Open" dialog. It lets the user browse for a file. This code is a function that returns the file path.
Function returnFilePath() If OpenFileDialog1.ShowDialog = True Then Return OpenFileDialog1.FileName() End If End Function
A SaveFileDialog displays the standard "Save" dialog. It lets the user browse for a directory to save files and enter a filename. It can (optionally) automatically append extensions to the filename.
A FontDialog lets the user to select a font from a list of installed fonts.
A ColorDialog lets the user select predefined color or specify a custom color.
A Timer Control is a control that will execute code at intervals. It is not visible at runtime. The interval can be set in the properties in the measurement of milliseconds. The timer control will continue to repeat its code at your interval until the control is stopped.