VBI 77.PNG

An example of common dialog control. Before using common dialog control on the form, place a TextBox on the form and change its name to TextMessage. Insert three menus such as file menu, format menu and colors menu using menu editor dialog box. In the file menu, add three commands such as open, save as and exit. Insert font command in format menu and color command in color menu. Follow the following step to handle these dialog boxes on the form –

  • (i) Double click on the open button and write code in the following format –
Private Sub mnuOpen_Click()
Dim Text As String
    With DLGCommon
        .Filter = "Text Files (*.txt)|*.txt| All Files (*.*)|*.*"
        .InitDir = "H:\My Documents"
        .DialogTitle = "Select A File To Open"
        .ShowOpen
    End With
    If DLGCommon.FileName = "" Then
        MsgBox "You Selected No File"
    Else
        Open DLGCommon.FileName For Input As #1
        Input #1, Text
        TextMessage = Text
        Close #1
    End If
End Sub
  • (ii) Double click on the save as command and write code in the following format –
Private Sub mnuSaveAs_Click()
    With DLGCommon
        .Filter = "Text Files (*.txt)|*.txt| All Files (*.*)|*.*"
        .InitDir = "H:\My Documents"
        .DialogTitle = "Save File As"
        .ShowSave
    End With
    If DLGCommon.FileName = "" Then
        MsgBox "You Entered No File Name"
    Else
        Open DLGCommon.FileName For Output As #1
        Write #1, TextMessage
        Close #1
    End If
End Sub
  • (iii) Double click on exit button and write code in the following format –
Private Sub mnuExit_Click()
    End
End Sub
  • (iv) Double click on the font command of format menu and write code in the following format –
Private Sub mnuFont_Click()
    DLGCommon.Flags = cdlCFScreenFonts
    DLGCommon.ShowFont
    With TextMessage
        .Font.Name = DLGCommon.FontName
        .Font.Bold = DLGCommon.FontBold
        .Font.Italic = DLGCommon.FontItalic
        .Font.Size = DLGCommon.FontSize
    End With
End Sub
  • (v) Double click on the color command from colors menu and write code in the following format –
Private Sub mnuColor_Click()
    DLGCommon.Flags = cdlCCRGBInit
    DLGCommon.Color = TextMessage.ForeColor
    DLGCommon.ShowColor
    TextMessage.ForeColor = DLGCommon.Color
End Sub
  • (vi) Run the application and test the dialog box.

VBO 77.PNG

Advertisements