《C#入门经典(中文第四版)》第二部分Winform窗体学习笔记
---------------------------------------------------------------------------------------------------------
A0 ………… 通用
A1 ………… Form 类
A2 ………… Control 类
A3 ………… MessageBox 类
A4 ………… Button 类
A5 ………… Label 类
LinkLabel 类
A6 ………… TextBox 类
A7 ………… RichTextBox 类
A8 ………… ListBox 类
A9 ………… CheckedListBox 类
G1 ………… ComboBox 类
G2 ………… RadioButton 类
G3 ………… CheckBox 类
G4 ………… TabControl 类
G5 ………… PictureBox 类
G6 ………… Panel 类
G7 ………… MenuStrip 类
G8 ………… ToolStripMenuItem 类
G9 ………… ToolStrip 类
G10 ………… ToolTip 类
U1 ………… ToolStripButton 类
U2 ………… StatusStrip 类
U3 ………… Timer 类
U4 ………… ProgressBar 类
U5 ………… OpenFileDialog 类
U6 ………… SaveFileDialog 类
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A0个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 按Ctrl键同时拉动控件,可以复制一个控件。
2. 在Form上点右键》查看代码,或是按F7。F4显示属性。
3. 实现文件夹打开显示功能,类似右键》打开所在文件夹。
System.Diagnostics命名空间:
Process类:提供对本地和远程进程的访问并使您能够启动和停止本地系统进程。
1. 用Explorer.exe打开文件夹:
System.Diagnostics.Process.Start("Explorer.exe",@"D:\DOCUMENTS\");
System.Diagnostics.Process.Start("Explorer.exe",@"D:\DOCUMENTS");
2. 用notepad.exe打开记事本:
System.Diagnostics.Process.Start("notepad.exe",@"F:\Desktop\1.txt");
3. 用Word的快捷方式打开Word文件:
System.Diagnostics.Process.Start(@"F:\Desktop\Word 2010", @"F:\Desktop\1.docx");
4. 用Firefox打开网址:www.baidu.com:
System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", "www.baidu.com");
4. 建立事件模板,然后调用:由于事件的监视及管理是由Application对象进行的,程序员不需要知道用户何时响应事件或者是响应了什么事件,只需要为事件添加响应方法即可。添加方法”+=“,取消方法”-=“。参数sender为事件发出者;e为事件的附加数据,事件不同,e也不同。
public Form1()
{
InitializeComponent();
textBox2.MouseMove += new MouseEventHandler(textBox_MouseMove); //调用事先建立的模板
textBox3.MouseMove += new MouseEventHandler(textBox_MouseMove); //四个TextBox可以实现相同的功能
textBox4.MouseMove += new MouseEventHandler(textBox_MouseMove); //通过双击Tab键可以自动实现后半部分
textBox5.MouseMove += new MouseEventHandler(textBox_MouseMove);
}
private void textBox_MouseMove(object sender, MouseEventArgs e) //建立事件模板
{
TextBox tb = sender as TextBox;
tb.BackColor = Color.Red;
}
public Form1()
{
InitializeComponent();
textBox1.KeyPress += new KeyPressEventHandler(textBox_KeyPress); //单击tab键出现一行
textBox2.KeyPress += new KeyPressEventHandler(textBox_KeyPress); //双击tab键出现N行
textBox3.KeyPress += new KeyPressEventHandler(textBox_KeyPress);
textBox4.KeyPress += new KeyPressEventHandler(textBox_KeyPress);
}
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8 && e.KeyChar != 13)
{
e.Handled = true;
}
}
5. 用代码在Form中写控件,同时可以编写控件数组,例如:
Label[] lbs = new Label[5]; //建立标签控件数组
for (int i = 0; i < lbs.Length; i++)
{
lbs[i] = new Label(); //在声明下Label类
this.Controls.Add(lbs[i]); //将Label加到控件集中
lbs[i].Left = 14;
lbs[i].Top = 30 * i + 14; //设置控件的位置
lbs[i].Width = 400; //设置控件的宽度
}
首先用Label建立数组,接下来遍历数组,给数组的每个要素声明Label,接下来用Controls的Add方法将用代码写的控件添加到控件集中,同时设置控件的位置和长宽。
6. 用代码执行事件:首先是双击控件,生成一个button1_Click(object sender,EventArgs e)的函数,通过代码直接调用这个函数,既可以调用这个事件,说到底就是调用函数。
private void button1_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = musicPath + @"music\1.mp3";
}
private void timer1_Tick(object sender, EventArgs e)
{
button1_Click(button1, e); //通过代码调用按钮单击事件,其他事件调用是类似的!
}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A1个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. Form 属性:
public void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
if (button1.Text == "开始")
{
this.AddOwnedForm(frm1); //将窗体加入到OwnedForm里面
frm1.Show();
button1.Text = "停止";
}
else
{
foreach (Form frm in this.OwnedForms)
{
frm.Close(); //将所有OwnedForms都删掉
}
button1.Text = "开始";
}
}
2. Form 方法:
3. Form 事件:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult a;
a = MessageBox.Show("你真的要关闭吗(⊙_⊙?)", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (a == DialogResult.No) //DialogResult枚举的值, 根据上面MessageBox中的按钮来定
{
e.Cancel = true; //单击No, 取消关闭.
}
}
textBox1.Focus();
4. Form扩展:
添加的窗体Form2,还是一个类,因此不能直接用,还要先new一个才能用,类似下面的例子,要想加载Form2,首先要new一个Form2,然后在用Show方法。
1 Form2 form2 = new Form2();
2 form2.Show();
private static string bt1; //定义字段
public string BT1 //定义属性获取textBox1的text值
{
get
{
return bt1; //返回字段的值
}
}
public void button1_Click(object sender, EventArgs e)
{
bt1 = textBox1.Text.Trim(); //给字段赋值
}
Form1 frm1 = new Form1(); //先实例化一个Form1
frm1.Show();
MessageBox.Show(frm1.BT1); //调用frm1的public属性BT1
5. 窗体间的交互:
实现图示:
单独点击红色按钮时候,显示下面的帮助文件!
再次点击红色按钮时,帮助文本框关闭,同时用帮助文本框自带的“×”也可以关闭窗体!
实现代码:
主窗体部分:
Form2 fr2; //定义帮助窗体 public static bool IsHelpClosed = true; //定义全局的static变量,是否手动关闭窗体 private void button4_Click(object sender, EventArgs e) { if (!IsHelpClosed) //若没有手动关闭窗体,则此时关闭窗体 { fr2.Close(); } else //若手动关闭了窗体,此时要打开窗体 { fr2 = new Form2(); fr2.Show(); fr2.Left = this.Left + this.Width + 10; fr2.Top = this.Top; } }
帮助窗体部分:
private void Form2_Load(object sender, EventArgs e) { Form1.IsHelpClosed = false; //窗体加载的时候,给其赋值为未手动关闭 } private void Form2_FormClosed(object sender, FormClosedEventArgs e) { Form1.IsHelpClosed = true; //窗体手动关闭时候,给其赋值为手动关闭 }
※ 参考: C# WinForm 窗体之间传递参数问题总结
6. 实现不同窗体中的控制
如上面例子,在我打开帮助文本框的时候,按钮显示“关闭帮助”;当我关闭帮助文本框的时候,按钮显示“帮助”。问题在于关闭帮助文本框的时候,如何能操作到主窗体的帮助按钮修改为“帮助”呢,这就要要在床架 Form2 的时候后,定义关闭窗体事件!
private void button3_Click(object sender, EventArgs e) { if (!IsHelpClosed) { help.Close(); button3.Text = "帮助"; } else { help = new Helper01(); help.FormClosed += new System.Windows.Forms.FormClosedEventHandler(helpChange); //写事件 help.Show(); help.Left = Left + Width + 10; help.Top = Top + Height / 2 - help.Height / 2; button3.Text = "关闭帮助"; } } private void helpChange(object sender, FormClosedEventArgs e) //事件函数,保证与委托的参数一致 { button3.Text = "帮助"; }
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A2个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 定义控件的基类,控件是带有可视化表示形式的组件。
2. Control 属性:
publicvoid SetFontUnderline()
{
if (richTextBox1.SelectionFont.Underline)
{
Font newFont = new Font(richTextBox1.SelectionFont,
richTextBox1.SelectionFont.Style & ~FontStyle.Underline);
richTextBox1.SelectionFont = newFont;
}
else
{
Font newFont = new Font(richTextBox1.SelectionFont,
richTextBox1.SelectionFont.Style | FontStyle.Underline);
richTextBox1.SelectionFont = newFont;
}
}
publicvoid SetFontSize(float size)
{
Font newFont = new Font(richTextBox1.SelectionFont.Name, size);
richTextBox1.SelectionFont = newFont;
}
3. Control 方法:
pTag.left = button1.PointToScreen(System.Drawing.Point.Empty).X; //按钮左的横坐标
pTag.bottom = button1.PointToScreen(System.Drawing.Point.Empty).Y + button1.Height; //按钮下的纵坐标
4. Control 事件:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A3个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 显示可包含文本、按钮和符号(通知并指示用户)的消息框。
2. MessageBox 方法:
privatevoid validateUserEntry()
{
// Checks the value of the text.
if(serverName.Text.Length == 0)
{
// Initializes the variables to pass to the MessageBox.Show method.
string message = "You did not enter a server name. Cancel this operation?";
string caption = "Error Detected in Input";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
// Displays the MessageBox.
result = MessageBox.Show(message, caption, buttons);
if (result == System.Windows.Forms.DialogResult.Yes)
{
// Closes the parent form.
this.Close();
}
}
}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A4个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. Button 属性:
2. Button 方法:
3. Button 事件:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A5个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. Label 属性:
1 Label[] lbs = new Label[5]; //建立标签控件数组
2 for (int i = 0; i < lbs.Length; i++)
3 {
4 lbs[i] = new Label(); //在声明下Label类
5 this.Controls.Add(lbs[i]); //将Label加到空间集中
6 lbs[i].Left = 14;
7 lbs[i].Top = 30 * i + 14; //设置控件的位置
8 lbs[i].Width = 400; //设置控件的宽度
9 }
10 lbs[0].Text = "1、仅限汉字输入;";
11 lbs[1].Text = "2、字体为“微软雅黑,小四”;";
12 lbs[2].Text = "3、汉字的字数不大于11个。";
13 lbs[3].Text = "4、";
14 lbs[4].Text = "5、";
---------------------------------------------------------------------------------------------------------
●·● LinkLabel 类:
1. LinkLabel 使用举例:
主要是通过点击链接进入到新的页面,包括点击网址进入到网页以及点击邮件地址进入发邮件的页面等。
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("http://www.nmemc.gov.cn/"); //直接以默认浏览器打开网址 } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("MailTo:[email protected]"); //直接打开Outlook,并准备发送邮件 }
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A6个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. TextBox 属性:
2. TextBox 事件:
private void textBoxSize_KeyPress(object sender, KeyPressEventArgs e)
{
// Remove all characters that are not numbers, backspace, or enter.
if ((e.KeyChar < 48 || e.KeyChar > 57) &&
e.KeyChar != 8 && e.KeyChar != 13)
{
e.Handled = true;
}
else if (e.KeyChar == 13)
{
// Apply size if the user hits enter
TextBox txt = (TextBox)sender;
if (txt.Text.Length > 0)
ApplyTextSize(txt.Text);
e.Handled = true;
this.richTextBoxText.Focus();
}
}
public Form1()
{
InitializeComponent();
this.buttonOK.Enabled = false;
// Tag values for testing if the data is valid
this.textBoxAddress.Tag = false;
this.textBoxAge.Tag = false;
this.textBoxName.Tag = false;
this.textBoxOccupation.Tag = false;
// Subscriptions to events
// 调用textBoxEmpty_Validating方法,通过调用可以让这四个TextBox都实现此事件
this.textBoxName.Validating += new CancelEventHandler(textBoxEmpty_Validating);
this.textBoxAddress.Validating += new CancelEventHandler(textBoxEmpty_Validating);
this.textBoxOccupation.Validating += new CancelEventHandler(textBoxOccupation_Validating);
this.textBoxAge.Validating += new CancelEventHandler(textBoxEmpty_Validating);
}
// 定义方法,与Validating事件语法相同
void textBoxEmpty_Validating(object sender, CancelEventArgs e)
{
// We know the sender is a TextBox, so we cast the sender object to that.
TextBox tb = (TextBox)sender;
// If the text is empty we set the background color of the
// Textbox to red to indicate a problem. We use the tag value
// of the control to indicate if the control contains valid
// information.
if (tb.Text.Length == 0)
{
tb.BackColor = Color.Red;
tb.Tag = false;
// In this case we do not want to cancel further processing,
// but if we had wanted to do this, we would have added this line:
// e.Cancel = true;
}
else
{
tb.BackColor = System.Drawing.SystemColors.Window;
tb.Tag = true;
}
// Finally, we call ValidateOK which will set the value of
// the OK button.
ValidateOK();
}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A7个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 表示 Windows 多格式文本框控件。
2. RichTextBox 属性:
3. RichTextBox 方法:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dia = new OpenFileDialog();
dia.InitialDirectory = @"F:\Desktop";
dia.Filter = "RTR(*.rtf)|*.rtf";
if (dia.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(dia.FileName);
}
}
private void button3_Click(object sender, EventArgs e)
{
SaveFileDialog dia = new SaveFileDialog(); //定义保存对话框
dia.Filter = "RTF|*.rtf|WORD|*.doc"; //文件格式过滤
if (dia.ShowDialog() == DialogResult.OK) //选择OK键执行
{
richTextBox1.SaveFile(dia.FileName); //存储缩写文件名的文件
}
}
4. RichTextBox 事件:
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(e.LinkText); //跳转到指定网址
System.Diagnostics.Process.Start("www.baidu.com"); //自定义网址
MessageBox.Show(e.LinkText); //输出link的字符串
}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A8个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 表示允许单项或多项选择的类表框控件。
2. ListBox 属性:
3. ListBox 方法:
4. ListBox 事件:
StringBuilder sb = new StringBuilder();
foreach (object o in this.listBox1.SelectedItems)
{
sb.AppendLine(o.ToString());
}
MessageBox.Show(sb.ToString());
StringBuilder sb = new StringBuilder();
foreach (string s in listBox1.SelectedItems)
{
sb.AppendLine(s);
}
MessageBox.Show(sb.ToString());
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A9个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 显示一个 ListBox,其中在每项的左边显示一个复选框。
2. CheckedListBox 属性:
3. CheckedListBox 方法:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Lists
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Add a tenth element to the CheckedListBox.
this.checkedListBoxPossibleValue.Items.Add("Ten");
}
private void buttonMove_Click(object sender, EventArgs e)
{
// Check if there are any checked items in the CheckedListBox.
if (this.checkedListBoxPossibleValue.CheckedItems.Count > 0)
{
// Clear the ListBox we'll move the selections to
this.listBoxSelected.Items.Clear();
foreach (int i in checkedListBoxPossibleValue.CheckedIndices)
{
MessageBox.Show(i.ToString());
}
// Loop through the CheckedItems collection of the CheckedListBox
// and add the items in the Selected ListBox
foreach (string item in this.checkedListBoxPossibleValue.CheckedItems)
{
this.listBoxSelected.Items.Add(item.ToString());
}
// Clear all the checks in the CheckedListBox
for (int i = 0; i < this.checkedListBoxPossibleValue.Items.Count; i++)
this.checkedListBoxPossibleValue.SetItemChecked(i, false);
}
}
}
}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第G1个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. ComboBox 属性:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第G2个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 当与其他 RadioButton 控件成对出现时,使用户能够从一组选项中选择一个选项。(放在Panel控件里面)
2. RadioButton 属性:
3. RadioButton 事件:
private void button1_Click(object sender, EventArgs e)
{
foreach (object o in groupBox1.Controls)
{
if (o is RadioButton)
{
if (((RadioButton)o).Checked)
{
MessageBox.Show(((RadioButton)o).Text);
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (RadioButton c in groupBox1.Controls) //遍历盒子中的所有RadioButton控件
{
if (c.Checked) //若是被选中,则输出选中控件的Name
MessageBox.Show(c.Name);
}
}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第G3个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 表示用户可以选择和清除的控件。多选按钮。
2. CheckBox 属性:
StringBuilder sb = new StringBuilder();
foreach (Control ctr in this.Controls)
{
if (ctr is CheckBox)
{
CheckBox ck = ctr as CheckBox;
if (ck.Checked)
{
sb.Append(ck.Text);
}
}
}
this.label1.Text = sb.ToString();
StringBuilder sb = new StringBuilder();
foreach (Control ctr in panel1.Controls)
{
if (ctr is CheckBox)
{
CheckBox ck = ctr as CheckBox;
if (ck.Checked)
{
sb.AppendLine(ck.Text);
}
}
}
MessageBox.Show(sb.ToString());
private void button2_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder(); //建立一个StringBuilder,方便Append
foreach (CheckBox c in groupBox2.Controls) //遍历盒子中的CheckBox
{
if (c.Checked)
{
sb.AppendLine(c.Name); //如果选中,则附加Name
}
}
MessageBox.Show(sb.ToString()); //ToString输出
}
3. CheckBox 事件:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第G4个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 管理相关的选项卡页集。
2. TabControl 属性:
3. TabControl 方法:
private void button1_Click(object sender, EventArgs e)
{
int a;
a = tabControl1.SelectedTab.TabIndex;
label1.Text = a.ToString();
tabControl1.DeselectTab(a);
}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第G5个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 表示用于显示图像的 Windows 图片框控件。
2. PictureBox 属性:
pbox.BackColor = Color.Transparent;
3. PictureBox 方法:
4. PictureBox 事件:
※ Properties\Resources.resx可以存储图片资源文件,也可以直接调用。如右图所示:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DrawPeachBlossom
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int flag = 0;//定义一个标识,用来标识画桃花的哪个部分
private void pictureBox2_Click(object sender, EventArgs e)
{
flag = 0;//标识绘制花骨朵
}
private void pictureBox3_Click(object sender, EventArgs e)
{
flag = 1;//标识绘制花蕾
}
private void pictureBox4_Click(object sender, EventArgs e)
{
flag = 2;//标识绘制开花效果
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Point myPT = new Point(e.X, e.Y);//获取鼠标单击位置
PictureBox pbox = new PictureBox();//实例化PictureBox控件
pbox.Location = myPT;//指定PictureBox控件的位置
pbox.BackColor = Color.Transparent;//设置PictureBox控件的背景色
pbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;//设置PictureBox控件的图片显示方式
switch (flag)//判断标识
{
case 0:
pbox.Size = new System.Drawing.Size(20, 18);//设置PictureBox控件大小
pbox.Image = Properties.Resources._2;//设置PictureBox控件要显示的图像
break;
case 1:
pbox.Size = new System.Drawing.Size(30, 31);//设置PictureBox控件大小
pbox.Image = Properties.Resources._3;//设置PictureBox控件要显示的图像
break;
case 2:
pbox.Size = new System.Drawing.Size(34, 30);//设置PictureBox控件大小
pbox.Image = Properties.Resources._1;//设置PictureBox控件要显示的图像
break;
}
if (e.Button == MouseButtons.Left)//判断是否单击了鼠标左键
{
pictureBox1.Controls.Add(pbox);//将PictureBox控件添加到树枝上
}
}
}
}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第G6个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 为所有Panel元素提供基类。为RadioButton和CheckBox等提供一个独立的面板。
2. Panel 属性:
相关窗体控件:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第G7个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 提供窗体的菜单系统。
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第G8个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 表示 MenuStrip 或 ContextMenuStrip 上显示的可选选项。
2. ToolStripMenuItem 属性:
newToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.Z;
//组合按键
newToolStripMenuItem.ShortcutKeys = newToolStripMenuItem.ShortcutKeys & ~Keys.Shift;
//减少按键
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第G9个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 为 Windows 工具栏对象提供容器。
2. ToolStrip 属性:
在ToolStrip中可以使用许多控件:http://book.51cto.com/art/200901/105510.htm
---------------------------------------------------------------------------------------------------------
╔═════════╗
╠════╣ 第G10个 ╠══════════════════════════════════════════════════╣
╚═════════╝
1. (tip)表示一个长方形的小弹出窗口,该窗口在用户将指针悬停在一个控件上时显示有关该控件用途的简短说明。
2. ToolTip 属性:
3. ToolTip 方法:
//换行使用 Environment.NewLine 来实现 string tooltip = "line1" + Environment.NewLine + "Line2" + Environment.NewLine + "Line3";
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第U1个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 表示包含文本和图像的可选的 ToolStripItem。
2. ToolStripButton 属性:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第U2个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 表示 Windows 状态栏控件。
2. 可以直接建立下面四个控件:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第U3个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 实现按用户定义的时间间隔引发事件的计时器。此计时器最宜用于 Windows 窗体应用程序中,并且必须在窗口中使用。
2. Timer 属性:
3. Timer 方法:
4. Timer 事件:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第U4个 ╠══════════════════════════════════════════════════╣
╚════════╝
使用思想:对于消耗时间多的运算,需要通过增加进度条来打消用户的焦虑心情,对于进度条的添加主要是在运行开始的时候进度条开始走动,运行完毕后,进度条读完,对于消耗时间多的运算主要是循环上面浪费时间,因此首先需要计算出循环的次数,接下来就可以按照每个循环增加一次了,有些可以直接读取循环次数,对于不能直接读取的,可以先建立一个循环只是用来计算次数的,由于没有其他运算,因此速度很快,几乎可以忽略的!(c# 进度条进度的控制_百度知道)
1. 表示 Windows 进度栏控件。
2. ProgressBar 属性:
//通过设置ProgressBar的最大值,然后没执行一句话,value增加1,即达到了目的!
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
int lines;
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select count(*) from city";
lines = Convert.ToInt32(cmd.ExecuteScalar());
}
progressBar1.Maximum = lines;
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from city";
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
using (StreamWriter streamWriter = File.AppendText(ofd.FileName))
{
streamWriter.WriteLine(reader.GetInt32(0).ToString() + "|" + reader.GetString(1) + "|" + reader.GetInt32(2).ToString());
progressBar1.Value++;
}
}
}
}
}
3. Progress 方法:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第U5个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 提示用户打开文件。
2. OpenFileDialog 属性:
3. OpenFileDialog 方法:
if (dia.ShowDialog() == DialogResult.OK) //单击OK触发
{
richTextBox1.LoadFile(dia.FileName); //将点击文件的内容加载进来
}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第U6个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 提示用户选择文件的保存位置。无法继承此类。
2. SaveFileDialog 属性:
3. SaveFileDialog 方法:
ColorDialog 类:
FontDialog 类:
---------------------------------------------------------------------------------------------------------
newToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.Z;