Creating Dialog Boxes

When message boxes and input boxes are not sufficient for your applications then you can create your own dialog boxes
To create a dialog box follow the steps given.

  1. In Form1 add one Label, one Text Box and one Command button as shown in fig. 6
  2. Add a new Window form, Form2 to the project and add two buttons with the caption OK and Cancel as well as a text box
    Set the Text property of this form to “Enter your Text” to set the text in the title bar.A label is also added with the prompt “Enter your Text”. Form2 is shown in Fig. 7.
  3. Set the FormBorderStyle property of Form2 to FixedDialog giving it a dialog box border and set the control box property to false to remove the control box (to remove minimize, maximize and close button at upper right)
    Also, set the ShowInTaskBar property of Form2 to false-this means that when this dialog box appears, it will not display an icon in the Windows task bar, which dialog box should not.
    Finally, set the DialogResult property of the OK, and the same property of the Cancel button to Cancel. This property returns a value of the DialogResult enumeration when the dialog box is closed, so you can determine which button the user has clicked

    Code for Form1
    Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim DialogBox As New Form2
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If DialogBox.ShowDialog = Windows.Forms.DialogResult.OK Then
    TextBox1.Text = DialogBox.TextBox1.Text
    'By removing the comment you can demonstrate the 'following message
    'Else
    'If DialogBox.ShowDialog = ' Windows.Forms.DialogResult.Cancel Then
    'TextBox1.Text = "Pressed Cancel Button"
    'End If
    End If
    End Sub
    End Class

    Code for Form2
    Public Class Form2
    'this code is added to give functionality of Enter and Esc 'key to two buttons
    Private Sub Form2_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load
    Me.AcceptButton = Button1
    Me.CancelButton = Button2
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Close()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Me.Close()
    End Sub
    End Class
  4. Run the application: You will get the screen as shown in Fig. 8.
  5. Click on to ‘To Dialog’ button you will get a screen as shown in Fig. 9 and type the text in the text box and click on to OK the text typed is passed to the text box of Form1, which is shown in Fig. 10.



Creating MDI<< Previous

Next>>Accept and Cancel button

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus


Footer1