Thursday, December 15, 2011

Formatting Menu

1. Start Visual Basic and create a label control within the form.

2. Double click MenuStrip, ToolStrip, PrintDocument, and SaveFileDialog from the Toolbox and do the following:



3. On File Menu, add the following submenu.



4. On Edit Menu, add the following submenu.









5. On Help Menu, add the following submenu.




6. On the code window, enter the following:

Code:


Public Class frmFormattingMenu

Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click

Dim Response As Integer
Response = MsgBox("Are you sure you want to exit", MsgBoxStyle.YesNo + vbQuestion, "My Program")
If Response = vbYes Then
End
End If
End Sub

Private Sub mnuChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuChange.Click

Dim strCaption As String
strCaption = InputBox("Please enter the new text", "Change Text")
If Len(strCaption) = 0 Then
MsgBox("No text Entered", MsgBoxStyle.Exclamation, "Invalid")
Else
lblText.Text = strCaption
End If
End Sub

Private Sub mnuRegular_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuRegular.Click

lblText.Font = _
New Font(lblText.Font, FontStyle.Regular)
mnuRegular.Checked = True
mnuBold.Checked = False
mnuItalic.Checked = False
End Sub

Private Sub mnuBold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuBold.Click

lblText.Font = _
New Font(lblText.Font, FontStyle.Bold)
mnuBold.Checked = True
mnuRegular.Checked = False
mnuItalic.Checked = False
End Sub

Private Sub mnuItalic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuItalic.Click

lblText.Font = _
New Font(lblText.Font, FontStyle.Italic)
mnuItalic.Checked = True
mnuRegular.Checked = False
mnuBold.Checked = False
End Sub

Private Sub mnuLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuLeft.Click

mnuLeft.Checked = True
mnuRight.Checked = False
mnuCenter.Checked = False
lblText.TextAlign = ContentAlignment.MiddleLeft
End Sub

Private Sub mnuRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuRight.Click

mnuRight.Checked = True
mnuLeft.Checked = False
mnuCenter.Checked = False
lblText.TextAlign = ContentAlignment.MiddleRight
End Sub

Private Sub mnuCenter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCenter.Click

mnuCenter.Checked = True
mnuRight.Checked = False
mnuLeft.Checked = False
lblText.TextAlign = ContentAlignment.MiddleCenter
End Sub

Private Sub mnuSize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSize.Click

Dim strSize As String
strSize = InputBox("Please enter the desired text size", "Change Size")
If Len(strSize) = 0 Then
MsgBox("No value Entered", MsgBoxStyle.Exclamation, "Invalid")
Else
lblText.Font = New Font(lblText.Font.FontFamily, strSize)
End If
End Sub

Private Sub mnuGray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuGray.Click

Me.BackColor = Color.Gray
End Sub

Private Sub mnuWhite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuWhite.Click

Me.BackColor = Color.White
End Sub

Private Sub mnuRed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuRed.Click

Me.BackColor = Color.Red
End Sub

Private Sub mnuBlue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuBlue.Click

Me.BackColor = Color.Blue
End Sub

Private Sub mnuYellow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuYellow.Click

Me.BackColor = Color.Yellow
End Sub

Private Sub mnuGreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuGreen.Click

Me.BackColor = Color.Green
End Sub

Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click

MsgBox("Menu and Dialog Application" & vbCrLf & "by Patrick", MsgBoxStyle.Information, "About")
End Sub

Private Sub mnuPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrint.Click

PrintDocument1.PrinterSettings.Copies = 2
PrintDocument1.Print()
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(lblText.Text, lblText.Font, Brushes.Blue, 100, 100)
End Sub

Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click

SaveFileDialog1.ShowDialog()
End Sub

Private Sub tsbSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbSave.Click

SaveFileDialog1.ShowDialog()
End Sub

Private Sub tsbPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbPrint.Click

PrintDocument1.PrinterSettings.Copies = 2
PrintDocument1.Print()
End Sub

Private Sub frmFormattingMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub tsbEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbEdit.Click

mnuEdit.ShowDropDown()
End Sub

End Class

No comments:

Post a Comment