0% found this document useful (0 votes)
26 views11 pages

Chapter Five LAB

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

Chapter Five

Menus and Dialog Boxes


Menus
 A set of options presented to the user of a computer application to help the user
find information or execute a program function.
To create menu in VB 2010 we can use either menu strip or MDI parent:
1. How menu strip work
Some steps
A. Drag a menu strip control from ToolBox.
B. Then write what you want, like this:

C. You can customize it by clicking on “Edit Items” and get this:


2. MDI parent
A. First go to the “Project” menu and click on “Add window Form”

B. Then the following dialog will appear so, look for MDI parent and select it.
C. After you add that item you will get the following, now you can customize.

D. Now if you click on “Edit” menu the following will appear:


E. Let us active Cut, Copy, Paste as a sample
So insert the following code for each
Public Class Form2
Private Sub CutToolStripMenuItem1_Click()
RichTextBox1.Cut()
End Sub
Private Sub CopyToolStripMenuItem1_Click()
RichTextBox1.Copy()
End Sub
Private Sub PasteToolStripMenuItem1_Click()
RichTextBox1.Paste()
End Sub
Dialog Boxes
1. Massage Box

Example

MsgBox( "Click OK to Proceed", 1, "StartupMenu") and

MsgBox("Click OK to Proceed“, vbOkCncel,"Startup Menu") are the same


2. InputBox

Private Sub Button4_Click()


Dim userMsg As String

userMsg = InputBox("What is your message?", "Message Entry Form", "Enter


your messge here")
If (userMsg <> "") Then
TextBox1.Text = userMsg
Else
TextBox1.Text ="No Message"
End If
End Sub
3. Common DialogBox
1. OpenFileDialog
Public Class Dialogs
Private strFileName As String
Private Sub Button1_Click()
With OpenFileDialog1
.Filter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*"
.FilterIndex = 1
.Title = "Demo Open File Dialog"
End With
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
strFileName = OpenFileDialog1.FileName
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(strFileName)
TextBox1.Text = fileContents
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try End If End Sub

2. SaveFileDialog
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
With SaveFileDialog1
.DefaultExt = "txt"
.FileName = strFileName
.Filter = "Text Documents (*.txt)|*.txt|All Files
(*.*)|*.*"
.FilterIndex = 1
.OverwritePrompt = True
.Title = "Demo Save File Dialog"
End With
'Show the Save dialog and if the user clicks the Save button,
'save the file
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK
Then
Try
strFileName = SaveFileDialog1.FileName
My.Computer.FileSystem.WriteAllText(strFileName,
TextBox1.Text, False)
'Save the filepath and name
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title,
_
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
End Class
3. FontDialog
Private Sub Button2_Click()
FontDialog1.ShowColor = True
'Show the Font dialog and if the user clicks the OK button,
'update the font and color in the text box
If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK
Then
TextBox1.Font = FontDialog1.Font
TextBox1.ForeColor = FontDialog1.Color
End If
End Sub
4. ColorDialog
If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

Me.BackColor = ColorDialog1.Color

End If

You might also like