1.MenuStrip
MenuStrip
作為一個(gè)容器可以包含多個(gè)菜單項(xiàng)。MenuStrip
的重要屬性包括:
-
Name
:菜單的名字 -
Dock
:菜單的??课恢?/li> -
Items
:菜單項(xiàng)的集合
ToolStripMenuItem
ToolStripMenuItem
是 MenuStrip
中的菜單項(xiàng),可以有以下屬性和功能:
-
ShortcutKeys
:為菜單項(xiàng)設(shè)置快捷鍵,例如Alt+F
,Ctrl+N
等 -
DropDownItems
:菜單項(xiàng)的子菜單集合 - 圖標(biāo):菜單項(xiàng)可以通過(guò)關(guān)聯(lián)
ImageList
控件來(lái)顯示圖標(biāo)
菜單項(xiàng)的響應(yīng)
菜單項(xiàng)可以注冊(cè) Click
事件來(lái)響應(yīng)用戶的點(diǎn)擊操作。有兩種方式來(lái)添加菜單項(xiàng)的響應(yīng):
- 手動(dòng)添加:在設(shè)計(jì)界面上手動(dòng)添加菜單項(xiàng),并為每個(gè)菜單項(xiàng)注冊(cè)
Click
事件 - 代碼添加:在
Load
事件中通過(guò)代碼添加菜單項(xiàng),并為其注冊(cè)Click
事件
代碼示例
using System;
using System.Windows.Forms;
namespace WFFormUse
{
public partial class FrmMenuStrip : Form
{
public FrmMenuStrip()
{
InitializeComponent();
}
private void FrmMenuStrip_Load(object sender, EventArgs e)
{
//代碼添加菜單項(xiàng)
ToolStripMenuItem miStudent = new ToolStripMenuItem();
miStudent.Name = "miStudent";
miStudent.Text = "學(xué)生管理(&M)";
//它下面還有子菜單
ToolStripMenuItem miAddStudent = new ToolStripMenuItem();
miAddStudent.Name = "miAddStudent";
miAddStudent.Text = "新增學(xué)生";
miAddStudent.Click += MiAddStudent_Click;
miStudent.DropDownItems.Add(miAddStudent);//添加子菜單
StudentMenus.Items.Add(miStudent); //添加主菜單
}
private void MiAddStudent_Click(object sender, EventArgs e)
{
MForms.FrmAddStudent fAddStudent = new MForms.FrmAddStudent();
fAddStudent.MdiParent = this;//設(shè)置當(dāng)前窗體的父窗體
fAddStudent.Show();//Mdi容器不支持ShowDialog()
}
//退出系統(tǒng)
private void miExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
//新增學(xué)生
private void miAddStudent_Click(object sender, EventArgs e)
{
MForms.FrmAddStudent fAddStudent = new MForms.FrmAddStudent();
fAddStudent.MdiParent = this;//設(shè)置當(dāng)前窗體的父窗體
fAddStudent.Show();//Mdi容器不支持ShowDialog()
}
//新增班級(jí)
private void miAddClass_Click(object sender, EventArgs e)
{
MForms.FrmAddClass fAddClass = new MForms.FrmAddClass();
fAddClass.MdiParent = this;
fAddClass.Show();
}
private void StudentMenus_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
}
}
2.ContextMenuStrip
ContextMenuStrip
用于創(chuàng)建和顯示上下文菜單,也就是通常所說(shuō)的右鍵菜單。它通常與窗體或其他控件相關(guān)聯(lián),當(dāng)用戶在相關(guān)聯(lián)的控件上右擊時(shí),上下文菜單就會(huì)顯示出來(lái)。
屬性
ContextMenuStrip
的一些重要屬性包括:
-
Items
:菜單項(xiàng)的集合,用于存放菜單項(xiàng)。 -
Name
:菜單的名字。
除了這些,ContextMenuStrip
還有其他一些屬性,例如 BackColor
、ForeColor
、Font
等,用于設(shè)置菜單的背景色、前景色、字體等。
方法
-
Show
:顯示上下文菜單。該方法有多個(gè)重載版本,可以指定顯示位置等參數(shù)。 -
Hide
:隱藏上下文菜單。 -
Dispose
:釋放ContextMenuStrip
控件所使用的資源。
官方文檔:https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.forms.contextmenustrip?view=windowsdesktop-7.0&viewFallbackFrom=net-7.0
示例:
namespace WinFormsTest
{
public partial class frmContextMenuStrip : Form
{
public frmContextMenuStrip()
{
InitializeComponent();
}
private void ContextMenuStrip_Load(object sender, EventArgs e)
{
}
private void loginToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmUser fAddStudent = new FrmUser();
fAddStudent.MdiParent = this;//設(shè)置當(dāng)前窗體的父窗體
fAddStudent.Show();//Mdi容器不支持ShowDialog()
}
private void redToolStripMenuItem_Click(object sender, EventArgs e)
{
button2.BackColor = Color.Red;
}
private void greenToolStripMenuItem_Click(object sender, EventArgs e)
{
button2.BackColor = Color.Green;
}
private void 改字體顏色ToolStripMenuItem_Click(object sender, EventArgs e)
{
button1.BackColor = Color.Green;
}
}
}
3.ToolStrip
ToolStrip
通常用于創(chuàng)建工具欄,以便用戶能夠快速訪問(wèn)常用的命令或功能。ToolStrip
可以包含多個(gè)不同類(lèi)型的項(xiàng),例如按鈕、標(biāo)簽、下拉按鈕等。
屬性
ToolStrip
的重要屬性包括:
-
Items
:項(xiàng)的集合,用于存放ToolStrip
中的各種項(xiàng)。 -
DisplayStyle
:用于設(shè)置項(xiàng)中圖片和文本的顯示方式。
ToolStrip 中的項(xiàng)
ToolStrip
中可以包含以下類(lèi)型的項(xiàng):
-
ToolStripButton
:按鈕,可以注冊(cè)Click
事件來(lái)響應(yīng)用戶的點(diǎn)擊操作。 -
ToolStripLabel
:標(biāo)簽,也可以注冊(cè)Click
事件。 -
ToolStripSplitButton
:拆分按鈕,包含一個(gè)按鈕部分和一個(gè)下拉部分,可以注冊(cè)ButtonClick
事件來(lái)響應(yīng)按鈕部分的點(diǎn)擊操作,也可以注冊(cè)Click
事件來(lái)響應(yīng)下拉項(xiàng)的點(diǎn)擊操作。 -
ToolStripDropDownButton
:下拉按鈕,可以包含多個(gè)下拉項(xiàng),可以注冊(cè)DropDownItemClicked
事件來(lái)響應(yīng)下拉項(xiàng)的點(diǎn)擊操作。 -
ToolStripComboBox
:下拉組合框,可以注冊(cè)SelectedIndexChanged
事件來(lái)響應(yīng)選中項(xiàng)的改變。 -
ToolStripTextBox
:文本輸入框,可以注冊(cè)TextChanged
事件來(lái)響應(yīng)文本的改變。 -
ToolStripProgressBar
:進(jìn)度條,通常不需要注冊(cè)事件。
示例
4.StatusStrip
StatusStrip
控件是通常用于在窗口的底部顯示狀態(tài)信息、進(jìn)度條等。
常用屬性:
- Dock: 控制
StatusStrip
的??课恢茫J(rèn)值為Bottom,即??吭诖翱诘牡撞?。 - Items: 表示
StatusStrip
中包含的所有子控件的集合。
用途:
- 顯示賬號(hào)信息。
- 顯示角色信息。
- 顯示操作位置信息。
- 顯示進(jìn)度條。
- 顯示版本信息。
示例:
namespace WinFormsTest
{
public partial class frmStatusStrip : Form
{
public frmStatusStrip()
{
InitializeComponent();
}
private void frmStatusStrip_Load(object sender, EventArgs e)
{
// 創(chuàng)建StatusStrip控件
StatusStrip statusStrip = new StatusStrip();
statusStrip.Dock = DockStyle.Bottom;
// 添加賬號(hào)信息
ToolStripStatusLabel accountLabel = new ToolStripStatusLabel();
accountLabel.Text = "賬號(hào): user1";
statusStrip.Items.Add(accountLabel);
// 添加角色信息
ToolStripStatusLabel roleLabel = new ToolStripStatusLabel();
roleLabel.Text = "角色: 管理員";
statusStrip.Items.Add(roleLabel);
// 添加操作位置信息
ToolStripStatusLabel locationLabel = new ToolStripStatusLabel();
locationLabel.Text = "操作位置: 主界面";
statusStrip.Items.Add(locationLabel);
// 添加進(jìn)度條
ToolStripProgressBar progressBar = new ToolStripProgressBar();
progressBar.Value = 50;
statusStrip.Items.Add(progressBar);
// 添加版本信息
ToolStripStatusLabel versionLabel = new ToolStripStatusLabel();
versionLabel.Text = "版本: v1.0";
statusStrip.Items.Add(versionLabel);
// 將StatusStrip控件添加到窗口的Controls集合中
this.Controls.Add(statusStrip);
}
}
}
【W(wǎng)inForm詳細(xì)教程】源代碼獲取方式
精彩推薦:
【C#進(jìn)階一】C#中的數(shù)組(Array)、集合(ArrayList,Queue,Stack, HashList)、List<T>、字典(Dictionary<K,T>)和雙向鏈表LinkedList
【C#進(jìn)階八】C#中的序列化與反序列化下(二進(jìn)制序列化、XML序列化及JSON序列化)
【C#進(jìn)階】C#語(yǔ)法中一些常用知識(shí)點(diǎn)總結(jié)
【W(wǎng)inForm詳細(xì)教程一】WinForm中的窗體、Label、TextBox及Button控件、RadioButton和CheckBox、ListBox
【W(wǎng)inForm詳細(xì)教程三】WinForm中的NumericUpDown、PictureBox、RichTextBox及三種Timer控件
【W(wǎng)inForm詳細(xì)教程四】WinForm中的ProgressBar 、ImageList和ListView控件文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-766499.html
【C#進(jìn)階】C#中的委托、事件、回調(diào)函數(shù)、匿名函數(shù)和lambda表達(dá)式
希望有所幫助,同時(shí)歡迎關(guān)注我,后面將更新更多相關(guān)內(nèi)容!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-766499.html
到了這里,關(guān)于【W(wǎng)inForm詳細(xì)教程五】WinForm中的MenuStrip 、ContextMenuStrip 、ToolStrip、StatusStrip控件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!