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); //通过代码调用按钮单击事件,其他事件调用是类似的! }