In this part of my Visual Basic tutorial I’ll cover most of the topics I missed before. We’ll cover MenuStrip, ToolStrip, Many Properties, Undo, Copy, Cut, Paste, SelectAll and a bunch of other topics.
Like always all of the code and a transcript of the video follows below. For everyone asking yes the C# / Xamarin / Game tutorial will start in the next week or so. Thank you for voting 🙂
If you like videos like this consider donating a $1 on Patreon.
[googleplusone]
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
I. Menustrip 1. Drag the menustrip up 2. Right click the menu and Insert Standard Items 3. Click Items in the Property Panel to change the items A. Click add and put &View in text, move to to the 3rd item, and change the name to ViewToolStripMenuItem B. Click DropDownItems a. Change Text to Hide Toolbar and Name to HideToolbarToolStripMenuItem b. Change ShortcutKeys to Ctrl + Alt + X 4. Click View and Add Font to the dropdown and name it FontToolStripMenuItem 5. Click to the right of Font and add Arial, Times, and Courier A. Set Checked and CheckOnClick to True for Arial and set the other 2 to True for only CheckOnClick B. Name them ArialFontToolStripMenuItem, TimesFontToolStripMenuItem, and CourierFontToolStripMenuItem C. Double click ArialFontToolStripMenuItem and in the code If ArialFontToolStripMenuItem.Checked = True Then TimesFontToolStripMenuItem.Checked = False CourierFontToolStripMenuItem.Checked = False End If II. ToolStrip 1. Get the Visual Studio Image Library if you want standard icons A. https://www.microsoft.com/en-us/download/details.aspx?id=35825 B. I got VS2013 Image Library.zip C. Make sure you extract the file 2. Drag the ToolStrip onto your form A. Change the name to TspMain B. Right click the ToolStrip and click Insert Standard Items 5. Add another ToolStrip, call it TspFormat and mark Visible as False A. Click Items a. Click Add b. Click Image -> Select FontColor c. Name it FontColorToolStripButton 6. Add another with the image FontSize and the name FontSizeToolStripButton 7. Add a separator to the top ToolStrip 8. Add an icon with image FontDialogControl and name it OpenFormatToolStripButton A. Double click this icon and add this code If TspFormat.Visible = True Then TspFormat.Visible = False Else TspFormat.Visible = True End If 9. Drag a TextBox on the form and set Multiline and Scrollbars to True and vertical 10. Double click the new icon ----- Form1.vb ----- Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub Private Sub ArialFontToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ArialFontToolStripMenuItem.Click If ArialFontToolStripMenuItem.Checked = True Then TimesFontToolStripMenuItem.Checked = False CourierFontToolStripMenuItem.Checked = False End If End Sub Private Sub TimesFontToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles TimesFontToolStripMenuItem.Click If TimesFontToolStripMenuItem.Checked = True Then ArialFontToolStripMenuItem.Checked = False CourierFontToolStripMenuItem.Checked = False End If End Sub Private Sub CourierFontToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CourierFontToolStripMenuItem.Click If CourierFontToolStripMenuItem.Checked = True Then ArialFontToolStripMenuItem.Checked = False TimesFontToolStripMenuItem.Checked = False End If End Sub Private Sub OpenFormatToolStripButton_Click(sender As Object, e As EventArgs) Handles OpenFormatToolStripButton.Click If TspFormat.Visible = True Then TspFormat.Visible = False Else TspFormat.Visible = True End If End Sub Private Sub NewToolStripButton_Click(sender As Object, e As EventArgs) Handles NewToolStripButton.Click ' Clears the TextBox TextBox1.Text = String.Empty ' Put focus on the TextBox TextBox1.Focus() End Sub Private Sub OpenToolStripButton_Click(sender As Object, e As EventArgs) Handles OpenToolStripButton.Click Dim OpenFileDialogEx As New OpenFileDialog() With { .Filter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*", .FilterIndex = 2, .Title = "Open Important File" } Dim fileSelected As String If OpenFileDialogEx.ShowDialog = System.Windows.Forms.DialogResult.OK Then Try fileSelected = OpenFileDialogEx.FileName Catch ex As Exception MessageBox.Show("Error Getting File", "Error") End Try End If End Sub Private Sub SaveToolStripButton_Click(sender As Object, e As EventArgs) Handles SaveToolStripButton.Click Dim fileToSave As String = "" ' OverwritePrompt protects you if the file already exists Dim SaveFileDialogEx As New SaveFileDialog() With { .Filter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*", .FilterIndex = 2, .Title = "Save Important File", .DefaultExt = "txt", .FileName = fileToSave, .OverwritePrompt = True } Dim fileSelected As String If SaveFileDialogEx.ShowDialog = System.Windows.Forms.DialogResult.OK Then Try fileSelected = SaveFileDialogEx.FileName Catch ex As Exception MessageBox.Show("Error Saving File", "Error") End Try End If End Sub ' For Undo menu clicked Private Sub UndoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UndoToolStripMenuItem.Click ' Undo the last change If TypeOf Me.ActiveControl Is TextBox Then CType(Me.ActiveControl, TextBox).Undo() End If End Sub ' For Cut menu clicked Private Sub CutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CutToolStripMenuItem.Click ' Cut selected text and save to the clipboard If TypeOf Me.ActiveControl Is TextBox Then CType(Me.ActiveControl, TextBox).Cut() End If End Sub ' For Cut Toolbar click Private Sub CutToolStripButton_Click(sender As Object, e As EventArgs) Handles CutToolStripButton.Click ' Have the other Cut subroutine handle CutToolStripMenuItem_Click(sender, e) End Sub ' For Copy menu click Private Sub CopyToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CopyToolStripMenuItem.Click ' Copy selected text and save to the clipboard If TypeOf Me.ActiveControl Is TextBox Then CType(Me.ActiveControl, TextBox).Copy() End If End Sub ' For Copy Toolbar click Private Sub CopyToolStripButton_Click(sender As Object, e As EventArgs) Handles CopyToolStripButton.Click ' Let other Copy sub handle CopyToolStripMenuItem_Click(sender, e) End Sub ' For Paste menu click Private Sub PasteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PasteToolStripMenuItem.Click ' Paste text from the clipboard If TypeOf Me.ActiveControl Is TextBox Then CType(Me.ActiveControl, TextBox).Paste() End If End Sub ' For Paste Toolbar click Private Sub PasteToolStripButton_Click(sender As Object, e As EventArgs) Handles PasteToolStripButton.Click PasteToolStripMenuItem_Click(sender, e) End Sub ' For Select All menu click Private Sub SelectAllToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SelectAllToolStripMenuItem.Click ' Select all text in the TextBox If TypeOf Me.ActiveControl Is TextBox Then CType(Me.ActiveControl, TextBox).SelectAll() End If End Sub End Class |
Leave a Reply