C#笔记(1)窗体
1. 隐藏TabPage
在使用TabControl控件时,希望隐藏其中某个选项卡(即TabPage)。设置该TabPage的父容器为null 即可,如TabPage.Parent = null 。如需显示该TabPage,设置其父容器为对应的TabControl即可;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WinFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { tabPage3.Parent = null;//使tabPage3不可见 } private void button1_Click(object sender, EventArgs e) { tabPage3.Parent = tabControl2;//使tabPage3可见 tabPage1.Parent = null;//使tabPage1不可见 tabControl2.SelectedTab = tabPage3;//停留在tabPage3 } } }
2. 鼠标右键菜单
1) 把ContextMenuStrip控件拖放到设计界面上(此处命名为cmMenu)。
2)右键cmMenu,点击Edit Items,添加菜单项,比如添加“打开”,“保存”等
3) 将想绑定控件(此处为Form)的ContextMenuStrip属性设置为设计的ContextMenuStrip(即cmMenu)。
运行:
3. 定时器
示例:用定时器每隔1s刷新数组,数字每次+1
代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WinFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public int number = 0; System.Timers.Timer timer; private void Form1_Load(object sender, EventArgs e) { timer = new System.Timers.Timer(1000); timer.Elapsed += new System.Timers.ElapsedEventHandler(changeNumber); timer.AutoReset = true;//AutoReset属性设置是否重复计时(设置为false只执行一次,设置为true可以多次执行) timer.Enabled = false; } private void changeNumber(object obj, EventArgs e) { number++; this.Invoke(new Action(delegate { this.textBox1.Text = number.ToString(); })); } private void toolStripMenuItem1_click(object sender, EventArgs e) { timer.Start(); } private void toolStripMenuItem2_click(object sender, EventArgs e) { timer.Stop(); } } }
namespace WinFormsApp1 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (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(); this.cmMenu = new System.Windows.Forms.ContextMenuStrip(this.components); this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem(); this.textBox1 = new System.Windows.Forms.TextBox(); this.cmMenu.SuspendLayout(); this.SuspendLayout(); // // cmMenu // this.cmMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItem1, this.toolStripMenuItem2}); this.cmMenu.Name = "contextMenuStrip1"; this.cmMenu.Size = new System.Drawing.Size(101, 48); // // toolStripMenuItem1 // this.toolStripMenuItem1.Name = "toolStripMenuItem1"; this.toolStripMenuItem1.Size = new System.Drawing.Size(100, 22); this.toolStripMenuItem1.Text = "开始"; this.toolStripMenuItem1.Click += new System.EventHandler(this.toolStripMenuItem1_click); // // toolStripMenuItem2 // this.toolStripMenuItem2.Name = "toolStripMenuItem2"; this.toolStripMenuItem2.Size = new System.Drawing.Size(100, 22); this.toolStripMenuItem2.Text = "停止"; this.toolStripMenuItem2.Click += new System.EventHandler(this.toolStripMenuItem2_click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(149, 74); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(161, 23); this.textBox1.TabIndex = 1; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); this.ContextMenuStrip = this.cmMenu; this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.cmMenu.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.ContextMenuStrip cmMenu; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2; private System.Windows.Forms.TextBox textBox1; } }