在本章中,我們將研究以下概念:
在應用程序中添加菜單和子菜單
在表單中添加剪切,複製和粘貼功能
錨定和對接控制在一種形式
模態形式
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
當使用Microsoft Visual Studio工具欄上的「開始」按鈕執行並運行上述代碼時,將顯示以下窗口:
點擊以下鏈接查看他們的詳細信息:
S.N. | Control & Description |
---|---|
1 | It provides a menu system for a form. 它爲表單提供了一個菜單系統。 |
2 | It represents a selectable option displayed on a MenuStrip orContextMenuStrip. The ToolStripMenuItem control replaces and adds functionality to the MenuItem control of previous versions. 它表示在MenuStrip或ContextMenuStrip上顯示的可選選項。 ToolStripMenuItem控件替換和添加以前版本的MenuItem控件的功能。 |
2 | It represents a shortcut menu. 它代表一個快捷菜單。 |
SN | 方法名稱和說明 |
---|---|
1 | Clear Removes 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. 指示是否有上是在指定的格式或可轉換成此格式的剪貼板中的數據。 |
3 | ContainsImage Indicates whether there is data on the Clipboard that is in the Bitmap format or can be converted to that format. 指示是否有關於那就是在Bitmap格式或可轉換成該格式剪貼板數據。 |
4 | ContainsText Indicates whether there is data on the Clipboard in the Text or UnicodeText format, depending on the operating system. 指示是否在文本或UnicodeText格式剪貼板中的數據,根據不同的操作系統。 |
5 | GetData Retrieves data from the Clipboard in the specified format. 從指定格式的剪貼板中檢索數據。 |
6 | GetDataObject Retrieves the data that is currently on the system Clipboard. 檢索是目前系統剪貼板中的數據。 |
7 | getImage Retrieves an image from the Clipboard. 檢索從剪貼板中的圖像。 |
8 | getText Retrieves text data from the Clipboard in the Text or UnicodeText format, depending on the operating system. 從文本或UnicodeText格式剪貼板中檢索文本數據,根據不同的操作系統。 |
9 | getText(TextDataFormat) Retrieves text data from the Clipboard in the format indicated by the specified TextDataFormat value. 從由指定TextDataFormat值指示的格式剪貼板中檢索文本數據。 |
10 | SetData Clears the Clipboard and then adds 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. 清除剪貼板,然後添加在文本或UnicodeText格式的文本數據,根據不同的操作系統。 |
以下是一個示例,其中顯示瞭如何使用Clipboard類的方法剪切,複製和粘貼數據。 執行以下步驟:
在表單上添加豐富的文本框控件和三個按鈕控件。
將按鈕的文本屬性分別更改爲「剪切」,「複製」和「粘貼」。
雙擊按鈕,在代碼編輯器中添加以下代碼:
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
當使用Microsoft Visual Studio工具欄上的「開始」按鈕執行並運行上述代碼時,將顯示以下窗口:
輸入一些文本並檢查按鈕的工作方式。
輸入一些文本並檢查按鈕的工作方式。
現在,當拉伸窗體時,Button和窗體右下角之間的距離保持不變。
例如,讓我們在表單上添加一個Button控件,並將其Dock屬性設置爲Bottom。 運行此窗體以查看Button控件相對於窗體的原始位置。
現在,當你拉伸窗體時,Button會調整窗體的大小。
您可以通過兩種方式調用模式窗體:
調用ShowDialog方法
調用Show方法
讓我們舉一個例子,我們將創建一個模態形式,一個對話框。 執行以下步驟:
將表單Form1添加到您的應用程序,並向Form1添加兩個標籤和一個按鈕控件
將第一個標籤和按鈕的文本屬性分別更改爲「歡迎使用教程點」和「輸入您的名稱」。 將第二個標籤的文本屬性保留爲空。
添加一個新的Windows窗體,Form2,並向Form2添加兩個按鈕,一個標籤和一個文本框。
將按鈕的文本屬性分別更改爲「確定」和「取消」。 將標籤的文本屬性更改爲「輸入您的姓名:」。
將Form2的FormBorderStyle屬性設置爲FixedDialog,爲其提供對話框邊框。
將Form2的ControlBox屬性設置爲False。
將Form2的ShowInTaskbar屬性設置爲False。
將OK按鈕的DialogResult屬性設置爲OK,將Cancel按鈕設置爲Cancel。
在Form2的Form2_Load方法中添加以下代碼片段:
Private Sub Form2_Load(sender As Object, e As EventArgs) _ Handles MyBase.Load AcceptButton = Button1 CancelButton = Button2 End Sub
在Form1的Button1_Click方法中添加以下代碼片段:
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
當使用Microsoft Visual Studio工具欄上的「開始」按鈕執行並運行上述代碼時,將顯示以下窗口:
點擊「輸入您的姓名」按鈕顯示第二個表單:
單擊確定按鈕將控制和信息從模態形式返回到先前的形式: