VB.NET Technology

VB.Net Projects

VB.Net Project 1

Creating Windows Application
Previous Home Next
adplus-dvertising

For creating a new project you need to first load Visual Studio .NET and select Windows Application as in Figure below . Type the name of the project below along with selecting the desired location to store your files.

Designing The Interface

We are going to design a simple application for adding values in List Box from from textbox input by the user, for this will need to add the following items onto your form.

  • GroupBox
  • Label
  • ComboBox
  • Textbox
  • Button
  • ListBox

Adding The Code : When this form loads we need to populate the ComboBox with the appropriate values. Add the following code by clicking on the form on the outside of the groupBox. You should see something like this:

write this code on Form Load Event

Private Sub Form1_Load(sender As Object, e As EventArgs)
	comboBox1.Items.Add("Dr.")
	comboBox1.Items.Add("Er.")
	comboBox1.Items.Add("Mr.")
	comboBox1.Items.Add("Mrs.")
	comboBox1.Items.Add("Ms.")
	comboBox1.Focus()
End Sub

Double-click on the OK button and add the following code:

Private Sub button1_Click(sender As Object, e As EventArgs)
	listBox1.Items.Add((comboBox1.Text + " " + textBox1.Text & " ") + textBox2.Text)
	textBox1.Text = ""
	textBox2.Text = ""
	comboBox1.Text = ""
	comboBox1.Focus()
End Sub

When we want to allow the user to clear all fields entered into the listBox, we will need to go back like we did above to the visual designer and double-click on the Clear List button, this should again switch to a code view and allow you to add the following code-

Private Sub button2_Click(sender As Object, e As EventArgs)
	listBox1.Items.Clear()
	comboBox1.Focus()
End Sub

And finally we want to allow the user to be able to close the application when they want. To show you another way to allow the user to close the program aside from that catchy X in the upper right-hand corner, I have provided a button entitled Close.

Private Sub button3_Click(sender As Object, e As EventArgs)
	Me.Dispose()
End Sub

Controls

System.Windows.Forms.Control class This class defines the basic functionality of the controls, which is why many properties and events in the controls. Some controls, named custom or user controls, derive from another class: System.Windows.Forms.UserControl. This class is itself derived from the Control class and provides the functionality we need to create controls ourselves.

Properties

All controls have a number of properties that are used to manipulate the behavior of the control. The base class of most controls, Control, has a number of properties that other controls either inherit directly or override to provide some kind of custom behavior.

NameAvailabilityDescription
AnchorRead/WriteIt tells how control behaves when its container is resized.
BackColorRead/Writeabout background color of a control.
BottomRead/WriteYou can specify the distance from the top of the window to the bottom of the control. This is not the same as specifying the height of the control.
DockRead/Writeby this property you can make a control dock to the edges of a window.
EnabledRead/WriteEnabled to true usually means that the control can receive input from the user. Setting Enabled to false usually means that it cannot.
ForeColorRead/Write foreground color of the control.
HeightRead/Writedistance from the top to the bottom of the control.
LeftRead/Writeleft edge of the control relative to the left edge of the window.
NameRead/Write name of the control. This name can be used to reference the control in code.
ParentRead/Writeparent of the control.
RightRead/Write right edge of the control relative to the left edge of the window.
TabIndexRead/Write number the control has in the tab order of its container.
TabStopRead/Write Specifies whether the control can be accessed by the Tab key.
TagRead/WriteThis value is usually not used by the control itself, and is there for you to store information about the control on the control itself. When this property is assigned a value through the Windows Form designer, you can only assign a string to it.
TopRead/WriteThe top edge of the control relative to the top of the window.
VisibleRead/Write Specifies whether or not the control is visible at runtime.
WidthRead/Writewidth of the control.

Events

When a user clicks a button or presses a button, you as the programmer of the application, want to be told that this has happened. To do so, controls use events. The Control class defines a number of events that are common to the controls we'll use in this chapter.

NameDescription
MouseMoveOccurs continually as the mouse travels over the control.
MouseUpOccurs when the mouse pointer is over a control and a mouse button is released.
ClickOccurs when a control is clicked. In some cases, this event will also occur when a user presses Enter..
DoubleClickOccurs when a control is double-clicked. Handling the Click event on some controls, such as the Button control will mean that the DoubleClick event can never be called.
DragDropOccurs when a drag-and-drop operation is completed, in other words, when an object has been dragged over the control, and the user releases the mouse button.
DragEnter Occurs when an object being dragged enters the bounds of the control.
DragLeave Occurs when an object being dragged leaves the bounds of the control.
DragOverOccurs when an object has been dragged over the control.
KeyDownOccurs when a key becomes pressed while the control has focus. This event always occurs before KeyPress and KeyUp.
KeyPress Occurs when a key becomes pressed, while a control has focus. This event always occurs after KeyDown and before KeyUp. The difference between KeyDown and KeyPress is that KeyDown passes the keyboard code of the key that has been pressed, while KeyPress passes the corresponding char value for the key.
KeyUpOccurs when a key is released while a control has focus. This event always occurs after KeyDown and KeyPress.
GotFocus Occurs when a control receives focus. Do not use this event to perform validation of controls. Use Validating and Validated instead.
LostFocus Occurs when a control looses focus. Do not use this event to perform validation of controls. Use Validating and Validated instead.
MouseDownOccurs when the mouse pointer is over a control and a mouse button is pressed. This is not the same as a Click event because MouseDown occurs as soon as the button is pressed and before it is released.
Paint Occurs when the control is drawn.
ValidatedThis event is fired when a control with the CausesValidation property set to true is about to receive focus. It fires after the Validating event finishes and indicates that validation is complete.
ValidatingFires when a control with the CausesValidation property set to true is about to receive focus. Note that the control which is to be validated is the control which is losing focus, not the one that is receiving it. previous
Previous Home Next