stu nicholls dot com | menu - Professional dropdown #3



Adding and Removing Controls in Code

Programs can be written to add different kinds of controls to your application. You can add single control or you can also add multiple controls to your application by writing an appropriate program. Similarly you can remove the added control with the help of a program.

Adding a Text box to the form at run time:

The following code is written to add a text box to the form. We may specify the Property of the text box which obviously is “text”, we have to give the name to the textbox in this case it is “TextBox1”, we need to specify the location where we want the text box be displayed. We have also need to give the size for the tex box. Tab Index specifies the index of the control. A button is added to trigger the code which creates text box. Fig. 69 displays the executed application which asks the user to select the appropriate choice.

Program to add text box to the form at run time: Public Class AddRemControl
Dim TextBox1 As New TextBox()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
With TextBox1
.Name = "Text1"
.Text = "TextBox1"
.Location = New Point(64, 40)
.Size = New Size(100, 20)
.TabIndex = 0
End With
Controls.Add(TextBox1)
End Sub
End Class

Fig69



Removing single control :

Following code can be used to remove the added textbox from the form at the run time.

Code to remove a single control

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Controls.Remove(TextBox1)
End Sub

Fig. 70 is the resultant screen when the application with both add and remove TextBox control code is put together and executed.

Fig70



Adding multiple controls:

You can add multiple text boxes or other controls to the form at run time. Following code adds multiple text boxes to the form. The program is executed the and you can add multiple text boxes at run time as shown in Fig. 71.
Program to add multiple text boxes at run time

Public Class MultipleTextB
Dim TextBox1 As New TextBox()
Dim i As Integer = 1
Dim j As Integer = 0

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1 = New TextBox()
With TextBox1
.Text = "TextBox" & i
.Location = New Point(64, 40 + j)
.Size = New Size(100, 20)
.TabIndex = i
i += 1
j += 25
End With
Controls.Add(TextBox1)
End Sub
End Class

Fig71



The Input Box:

You can use the Input Box to get an input from the user. The syntax for the Input Box is as given below.
InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])

The Message Box
MsgBox Message, Type, Title

In the above syntax:
Message :Text message to be displayed
Type :Type of message box
Title:Text in title bar of message box

You do not have any control over where the message box appears on the screen.

Following is the code for the Input and Message Box. Fig. 72(Input Box), Fig. 73 and Fig. 74 (Message Box) shows the result of execution of the application with the following code.

Public Class Form3
Dim inp1, inp2, res, Cancel
Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click
inp1 = InputBox("Enter Your First Name", "Name Please", "Vishwanath")
inp2 = InputBox("Enter Your Surname", "again pls", " ")
res = MsgBox("Hello You are..." & " " & inp1 & " " & inp2 & " " & "right?", vbQuestion + vbYesNo, "Greetings")
If res = vbNo Then
MsgBox("Sorry")
Else
MsgBox("Hello" & " " & inp1 & " " & inp2, vbExclamation, "Greeting")
End If
End Sub
End Class

Fig72



Fig73



Fig74



<< Previous   Next>>


comments powered by Disqus
click here
Footer1