form1.cs
来自「Sams Teach Yourself C# Web Programming i」· CS 代码 · 共 274 行
CS
274 行
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Menus_and_More
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class fclsMenusAndMore : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem mnuFileMenu;
private System.Windows.Forms.MenuItem mnuHelpMenu;
private System.Windows.Forms.MenuItem mnuQuit;
private System.Windows.Forms.MenuItem mnuAskBeforeClosing;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem mnuClearTextbox;
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.ToolBar tbrMainToolbar;
private System.Windows.Forms.ImageList imgMyPictures;
private System.Windows.Forms.ToolBarButton tbbQuit;
private System.Windows.Forms.ToolBarButton tbbInvisible;
private System.Windows.Forms.ToolBarButton tbbSeparator;
private System.Windows.Forms.ToolBarButton tbbDropdown;
private System.Windows.Forms.StatusBar sbrMyStatusBar;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.ComponentModel.IContainer components;
public fclsMenusAndMore()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(fclsMenusAndMore));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.mnuFileMenu = new System.Windows.Forms.MenuItem();
this.mnuQuit = new System.Windows.Forms.MenuItem();
this.mnuHelpMenu = new System.Windows.Forms.MenuItem();
this.mnuAskBeforeClosing = new System.Windows.Forms.MenuItem();
this.txtInput = new System.Windows.Forms.TextBox();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.mnuClearTextbox = new System.Windows.Forms.MenuItem();
this.tbrMainToolbar = new System.Windows.Forms.ToolBar();
this.imgMyPictures = new System.Windows.Forms.ImageList(this.components);
this.tbbQuit = new System.Windows.Forms.ToolBarButton();
this.tbbInvisible = new System.Windows.Forms.ToolBarButton();
this.tbbSeparator = new System.Windows.Forms.ToolBarButton();
this.tbbDropdown = new System.Windows.Forms.ToolBarButton();
this.sbrMyStatusBar = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuFileMenu,
this.mnuHelpMenu});
//
// mnuFileMenu
//
this.mnuFileMenu.Index = 0;
this.mnuFileMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuQuit,
this.mnuAskBeforeClosing});
this.mnuFileMenu.Text = "&File";
//
// mnuQuit
//
this.mnuQuit.Index = 0;
this.mnuQuit.Shortcut = System.Windows.Forms.Shortcut.CtrlQ;
this.mnuQuit.Text = "&Quit";
this.mnuQuit.Click += new System.EventHandler(this.mnuQuit_Click);
//
// mnuHelpMenu
//
this.mnuHelpMenu.Index = 1;
this.mnuHelpMenu.Text = "&Help";
//
// mnuAskBeforeClosing
//
this.mnuAskBeforeClosing.Checked = true;
this.mnuAskBeforeClosing.Index = 1;
this.mnuAskBeforeClosing.Text = "Ask before closing";
this.mnuAskBeforeClosing.Click += new System.EventHandler(this.menuItem1_Click);
//
// txtInput
//
this.txtInput.ContextMenu = this.contextMenu1;
this.txtInput.Location = new System.Drawing.Point(96, 122);
this.txtInput.Name = "txtInput";
this.txtInput.TabIndex = 0;
this.txtInput.Text = "";
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuClearTextbox});
//
// mnuClearTextbox
//
this.mnuClearTextbox.Index = 0;
this.mnuClearTextbox.Text = "Clear text box";
this.mnuClearTextbox.Click += new System.EventHandler(this.mnuClearTextbox_Click);
//
// tbrMainToolbar
//
this.tbrMainToolbar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.tbbQuit,
this.tbbSeparator,
this.tbbInvisible,
this.tbbDropdown});
this.tbrMainToolbar.DropDownArrows = true;
this.tbrMainToolbar.ImageList = this.imgMyPictures;
this.tbrMainToolbar.Name = "tbrMainToolbar";
this.tbrMainToolbar.ShowToolTips = true;
this.tbrMainToolbar.Size = new System.Drawing.Size(292, 25);
this.tbrMainToolbar.TabIndex = 1;
this.tbrMainToolbar.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
this.tbrMainToolbar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.tbrMainToolbar_ButtonClick);
//
// imgMyPictures
//
this.imgMyPictures.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imgMyPictures.ImageSize = new System.Drawing.Size(16, 16);
this.imgMyPictures.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgMyPictures.ImageStream")));
this.imgMyPictures.TransparentColor = System.Drawing.Color.Transparent;
//
// tbbQuit
//
this.tbbQuit.ImageIndex = 0;
this.tbbQuit.Text = "Quit";
this.tbbQuit.ToolTipText = "Quit this application";
//
// tbbInvisible
//
this.tbbInvisible.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.tbbInvisible.Text = "Invisible";
//
// tbbSeparator
//
this.tbbSeparator.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// tbbDropdown
//
this.tbbDropdown.DropDownMenu = this.contextMenu1;
this.tbbDropdown.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
//
// sbrMyStatusBar
//
this.sbrMyStatusBar.Location = new System.Drawing.Point(0, 251);
this.sbrMyStatusBar.Name = "sbrMyStatusBar";
this.sbrMyStatusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1});
this.sbrMyStatusBar.ShowPanels = true;
this.sbrMyStatusBar.Size = new System.Drawing.Size(292, 22);
this.sbrMyStatusBar.TabIndex = 2;
this.sbrMyStatusBar.Text = "Menus and Toolbars Example";
//
// statusBarPanel1
//
this.statusBarPanel1.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
this.statusBarPanel1.Text = "Panel Text";
this.statusBarPanel1.Width = 68;
//
// fclsMenusAndMore
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.sbrMyStatusBar,
this.tbrMainToolbar,
this.txtInput});
this.Menu = this.mainMenu1;
this.Name = "fclsMenusAndMore";
this.Text = "Menus and More";
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new fclsMenusAndMore());
}
private void mnuQuit_Click(object sender, System.EventArgs e)
{
if (mnuAskBeforeClosing.Checked)
{
if (MessageBox.Show("Do you really wish to exit?","Quit Verification",MessageBoxButtons.YesNo) == DialogResult.No)
return;
}
this.Close();
}
private void button1_Click(object sender, System.EventArgs e)
{
long lngAnswer;
lngAnswer = 100 / long.Parse(txtInput.Text);
MessageBox.Show("100/" + txtInput.Text + " is " + lngAnswer);
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
mnuAskBeforeClosing.Checked = (!mnuAskBeforeClosing.Checked);
}
private void mnuClearTextbox_Click(object sender, System.EventArgs e)
{
txtInput.Text = "";
}
private void tbrMainToolbar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch (tbrMainToolbar.Buttons.IndexOf(e.Button))
{
case 0:
this.Close();
break;
case 2:
txtInput.Visible = (!tbbInvisible.Pushed);
break;
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?