控件的应用
1. 菜单
1.1 MaskedTextBox控件
新建Form, 增加3个Label 和 3个MaskedtextBox
在Form_load代码中增添如下
1 private void Form1_Load(object sender, EventArgs e) 2 { 3 maskedTextBox1.Mask = "00/00/0000"; 4 maskedTextBox2.Mask = "00"; 5 maskedTextBox3.Mask = "000-0000-0000"; 6 }
可以给每个MaskedtextBox的MaskInputRejected事件添加处理机制, 例如其中的一个:
1 private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) 2 { 3 MessageBox.Show("Error"); 4 }
MaskedTextBox控件是增强型的textBox控件, 使用掩码区分正确和不正确的用户输入. 通过Mask属性可实现:
<1>必需的输入字符
<2>可选的输入字符
<3>掩码中的给定位置所需的输入类型
<4>输入字符的特殊处理:例如, 将字母字符转换为大写字母
1.2 NumericUpDown
在Form上添加Label和NumericUpDown
1 private void numericUpDown1_ValueChanged(object sender, EventArgs e) 2 { 3 this.label1.Text = ""; 4 string str = "当前选择值为:"; 5 this.label1.Text = string.Format("{0}{1}",str,this.numericUpDown1.Value.ToString()); 6 }
1.3 ProgressBar
1 private void button1_Click(object sender, EventArgs e) 2 { 3 int count = 1000; //测试值总数1000 4 progressBar1.Minimum = 0; //最小值 5 progressBar1.Value = 0; //初始值 6 progressBar1.Maximum = 1000; //最大值 7 for (int i = 0; i < count; i++) 8 { 9 this.progressBar1.Value++; 10 Application.DoEvents(); 11 label1.Text = "总量" + count.ToString() + " ,剩余" + Convert.ToString(count-i-1); //这种数值转换也值得学习 12 } 13 }
1.4 DateTimePicker
private void dateTimePicker1_ValueChanged(object sender, EventArgs e) { this.label1.Text = "今天是今年的第" + this.dateTimePicker1.Value.DayOfYear.ToString() + "天"; } private void Form1_Load(object sender, EventArgs e) { this.label1.Text = "今天是今年的第" + this.dateTimePicker1.Value.DayOfYear.ToString()+"天"; }
1.5 TabControl
在Form上添加TabControl, 在其TabPage属性中修改其Text属性, Name还是默认的
先放置几个GroupBox, 再在其中添加几个控件, 值得注意的是一个groupBox中只能选择一个radiobutton, 布局如下
1 private void button1_Click(object sender, EventArgs e) //下一步按钮 2 { 3 if (tabControl1.SelectedIndex == 0) 4 { 5 tabControl1.SelectedIndex = 1; 6 } 7 8 if (radioButton1.Checked) 9 { 10 this.label4.Text = "用户是 男"; 11 } 12 else 13 this.label4.Text = "用户是 女"; 14 } 15 16 17 18 private void button2_Click(object sender, EventArgs e) //返回 19 { 20 if (tabControl1.SelectedIndex == 1) 21 { 22 tabControl1.SelectedIndex = 0; 23 } 24 this.label4.Text = " "; 25 }
点击下一步按钮就会到第二个选项卡, Label会显示是男是女
1.6 LinkLabel
类似超链接
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("http://zc.qq.com/chs/index.html"); }
点击申请新用户, 浏览器会自动打开如下网址 "http://zc.qq.com/chs/index.html"
1.7 WebBrowser
在地址栏输入baidu之后, 回车
在Form上添加Menustrip,分别为1234, 添加ToolStrip分别为 后退 前进 主页 刷新四个按钮, 和一个ToolStripTextBox
1 private void toolStripButton1_Click(object sender, EventArgs e) //后退 2 { 3 webBrowser1.GoBack(); 4 } 5 6 private void toolStripButton2_Click(object sender, EventArgs e) //前进 7 { 8 webBrowser1.GoForward(); 9 } 10 11 private void toolStripButton3_Click(object sender, EventArgs e) //刷新 12 { 13 if (!webBrowser1.Url.Equals("about:blank")) 14 { 15 webBrowser1.Refresh(); 16 } 17 } 18 19 private void toolStripButton4_Click(object sender, EventArgs e) //主页 20 { 21 webBrowser1.GoHome(); 22 } 23 24 private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) 25 { 26 toolStripTextBox1.Text = webBrowser1.Url.ToString(); //TextBox刷新为当前Url 27 } 28 29 private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e) 30 { 31 if (e.KeyCode == Keys.Enter) //回车键 32 { 33 Navigate(toolStripTextBox1.Text); 34 } 35 } 36 37 38 private void Navigate(String address) 39 { 40 if (!address.StartsWith("http://") && !address.StartsWith("https://")) 41 { 42 address = "http://" + address + ".com"; 43 } 44 try 45 { 46 webBrowser1.Navigate(new Uri(address)); 47 } 48 catch(System.UriFormatException) 49 { 50 return; 51 } 52 }
1.8 HScrollBar
1 private void hScrollBar1_Scroll(object sender, ScrollEventArgs e) 2 { 3 pictureBox1.BackColor = Color.FromArgb(255, hScrollBar1.Value, hScrollBar2.Value, hScrollBar3.Value); 4 } 5 6 private void hScrollBar2_Scroll(object sender, ScrollEventArgs e) 7 { 8 pictureBox1.BackColor = Color.FromArgb(255, hScrollBar1.Value, hScrollBar2.Value, hScrollBar3.Value); 9 } 10 11 private void hScrollBar3_Scroll(object sender, ScrollEventArgs e) 12 { 13 pictureBox1.BackColor = Color.FromArgb(255, hScrollBar1.Value, hScrollBar2.Value, hScrollBar3.Value); 14 }
1.9