Saturday, July 23, 2011

Owned Forms, InputBox

Owned Forms
Visual Basic also allows us to create owned forms. An owned form is tied to the form that owns it. If you minimize the owner form, the owned form will also be minimized, if you close the owner form, the owned form will be closed and so on. Owned Forms are added to a form with the AddOwnedForm method and removed with the RemoveOwnedForm method. The following code demonstrates the creation of owned forms. Drag a Button from the toolbox onto the form (form1). When you click the button, Form1 will make an object of the Form class into an owned form and displays it. The code for that looks like this:


Public Class Form1 Inherits System.Windows.Forms.Form
Dim OwnedForm1 As New Form()

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles Button2.Click
Me.AddOwnedForm(OwnedForm1)
'adding an owned form to the current form
OwnedForm1.show()
'displaying the owned form
End Sub


The image below displays output from above code.

Owned Form
InputBox Function 
InputBox function gets a string of text entered by the user. They are similar to the JavaScript prompt windows that ask us to enter some text and performs some action after the OK button is clicked. In Visual Basic Input boxes let you display a prompt, read the text entered by the user and returns a string result. The following code demonstrates InputBox function. Drag a Button and TextBox control from the toolbox onto the form. When you click the Button, InputBox asks to enter some text and after you click OK it displays the text you entered in the TextBox. The image below displays output.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles Button1.Click
Dim s As String = InputBox("Enter some text")
'storing the text entered in a string
TextBox1.Text = s
'displaying string in the textbox
End Sub


No comments:

Post a Comment