May 13, 2021 vb.net
1. Add menus and sub-menus to the application
2. Add cut, copy, and paste to your form
In this chapter, we'll look at the following concepts:
Add menus and sub-menus to the application
Add cut, copy, and paste to your form
Anchoring and docking are controlled in one form
Modal form
Traditionally, menu, MainMenu, ContextMenu, and MenuItem classes have been used to add menus, submenu, and context menus to Windows applications.
Now, MenuStrip, ToolStripMenuItem, ToolStripDropDown and ToolStripDropDownMenu controls replace and add functionality to previous versions of menu-related controls.
However, the old control class is reserved for backward compatibility and future use.
Let's start by creating a typical Windows main menu bar and sub-menu with older controls, which are still commonly used in older applications.
The following is an example of how to create a menu bar using menu items: files, edits, views, and items.
The file menu has sub-menus to create, open, and save.
Let's double-click the form and put the following code in the open window.
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'defining the main menu bar Dim mnuBar As New MainMenu() 'defining the menu items for the main menu bar Dim myMenuItemFile As New MenuItem("&File") Dim myMenuItemEdit As New MenuItem("&Edit") Dim myMenuItemView As New MenuItem("&View") Dim myMenuItemProject As New MenuItem("&Project") 'adding the menu items to the main menu bar mnuBar.MenuItems.Add(myMenuItemFile) mnuBar.MenuItems.Add(myMenuItemEdit) mnuBar.MenuItems.Add(myMenuItemView) mnuBar.MenuItems.Add(myMenuItemProject) ' defining some sub menus Dim myMenuItemNew As New MenuItem("&New") Dim myMenuItemOpen As New MenuItem("&Open") Dim myMenuItemSave As New MenuItem("&Save") 'add sub menus to the File menu myMenuItemFile.MenuItems.Add(myMenuItemNew) myMenuItemFile.MenuItems.Add(myMenuItemOpen) myMenuItemFile.MenuItems.Add(myMenuItemSave) 'add the main menu to the form Me.Menu = mnuBar ' Set the caption bar text of the form. Me.Text = "tutorialspoint.com" End Sub End Class
When you execute and run the code above using the Start button on the Microsoft Visual Studio toolbar, the following window is displayed:
Windows Forms contains a rich set of classes to create your own custom menus with a modern look, feel, and feel. MenuStrip, ToolStripMenuItem, ContextMenuStrip controls are used to effectively create menu bars and context menus.
Click on the link below to see their details:
S.N. | Control & Description |
---|---|
1 |
It provides a menu system for a form.
It provides a menu system for forms. |
2 |
It represents a selectable option displayed on a MenuStrip or ContextMenuStrip . The ToolStripMenuItem control replaces and adds functionality to the MenuItem control of previous versions.
It represents the options displayed on MenuStrip or Context MenuStrip.
The ToolStripMenuItem control replaces and adds the functionality of previous versions of the MenuItem control.
|
2 |
It represents a shortcut menu.
It represents a shortcut menu.
|
The ClipBoard class exposes methods for adding cut, copy, and paste functionality to an application.
The ClipBoard class provides a way to place and retrieve data on the system clipboard.
It has the following common methods:
SN | Method name and description |
---|---|
1 |
Clear
Removes all data from the Clipboard.
Delete all data from the clipboard. |
2 |
ContainsData
Indicates whether there is data on the Clipboard that is in the specified format or can be converted to that format.
Indicates whether there is data on the clipboard that is in the specified format or that can be converted to this format. |
3 |
ContainsImage
Indicates whether there is data on the Clipboard that is in the Bitmap format or can be converted to that format.
Indicates whether there is data about that that in bitmap format or clipboards that can be converted into that format. |
4 |
ContainsText
Indicates whether there is data on the Clipboard in the Text or UnicodeText format, depending on the operating system.
Indicates whether the data is in the text or UnicodeText format clipboard, depending on the operating system. |
5 |
GetData
Retrieves data from the Clipboard in the specified format.
Retrieving data from the clipboard in the specified format. |
6 |
GetDataObject
Retrieves the data that is currently on the system Clipboard.
Retrieval is the data in the current system clipboard. |
7 |
getImage
Retrieves an image from the Clipboard.
Retrieves the image from the clipboard. |
8 |
getText
Retrieves text data from the Clipboard in the Text or UnicodeText format, depending on the operating system.
Retrieving text data from text or the UnicodeText format clipboard, depending on the operating system. |
9 |
getText(TextDataFormat)
Retrieves text data from the Clipboard in the format indicated by the specified TextDataFormat value.
Retrieving text data from the format clipboard indicated by the specified TextDataFormat value. |
10 |
SetData
Clears the Clipboard and then adds data in the specified format.
Clear the clipboard and add the data in the specified format. |
11 |
setText
(String)
Clears the Clipboard and then adds text data in the Text or UnicodeText format, depending on the operating system.
Clear the clipboard and then add text data in text or UnicodeText format, depending on the operating system. |
The following is an example that shows how to cut, copy, and paste data using the Clipboard class. F ollow these steps:
Add rich text box controls and three button controls to the form.
Change the text properties of the button to Cut, Copy, and Paste, respectively.
Double-click the button to add the following code to the code editor:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) _ Handles MyBase.Load ' Set the caption bar text of the form. Me.Text = "tutorialspoint.com" End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) _ Handles Button1.Click Clipboard.SetDataObject(RichTextBox1.SelectedText) RichTextBox1.SelectedText = "" End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) _ Handles Button2.Click Clipboard.SetDataObject(RichTextBox1.SelectedText) End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) _ Handles Button3.Click Dim iData As IDataObject iData = Clipboard.GetDataObject() If (iData.GetDataPresent(DataFormats.Text)) Then RichTextBox1.SelectedText = iData.GetData(DataFormats.Text) Else RichTextBox1.SelectedText = " " End If End Sub End Class
When you execute and run the code above using the Start button on the Microsoft Visual Studio toolbar, the following window is displayed:
Enter some text and check how the buttons work.
Enter some text and check how the buttons work.
Now, when you stretch a form, the distance between Button and the lower right corner of the form remains the same.
The docking of the control means that it is connected to one of the edges of its container.
In docking, controls the full filling of certain areas of the container.
The Dock property of the Control class does this.
The Dock property gets or sets which control boundaries are docked to its parent control and determines how to resize the control using its parent control.
You can set the control's Dock property value from the Properties window:
For example, let's add a Button control to the form and set its Dock property to Botom. R un this form to see where the Button control is relative to the form.
Now, when you stretch the form, Button resizes the form.
You can call a pattern form in two ways:
Call the ShowDialog method
Call the Show method
Let's give an example, we'll create a modal form, a dialog box. F ollow these steps:
Add form Form1 to your application and add two labels and a button control to Form1
Change the text properties of the first label and button to "Welcome to tutorial points" and "Enter your name" respectively. Leave the text properties of the second label blank.
Add a new Windows Form, Form2, and add two buttons, a label, and a text box to Form2.
Change the text properties of the button to OK and Cancel, respectively. C hange the text property of the label to Enter Your Name: .
Set The FormStyle property of Form2 to TheFixedDialog, providing it with a dialog border.
Set The ControlBox property of Form2 to False.
Set the ShowInTaskbar property of Form2 to False.
Set the DiamondResult property of the OK button to OK and the Cansl button to Cancel.
Add the following Form2_Load code snippets to Form2's methods:
Private Sub Form2_Load(sender As Object, e As EventArgs) _ Handles MyBase.Load AcceptButton = Button1 CancelButton = Button2 End Sub
Add the following Button1_Click code snippets to Form1's methods:
Private Sub Button1_Click(sender As Object, e As EventArgs) _ Handles Button1.Click Dim frmSecond As Form2 = New Form2() If frmSecond.ShowDialog() = DialogResult.OK Then Label2.Text = frmSecond.TextBox1.Text End If End Sub
When you execute and run the code above using the Start button on the Microsoft Visual Studio toolbar, the following window is displayed:
Click the "Enter your name" button to display the second form:
Clicking the OK button returns control and information from the modal form to the previous form: