alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【226】C# 相关功能实现代码

目录:

  1. 实现代码的等待操作

  2. 实现文件夹/文件打开操作

  3. 建立事件模板,然后调用

  4. 用代码在Form中写控件,同时可以编写控件数组

  5. 用代码执行事件


  1. 实现代码的等待操作

  System.Threading.Thread.Sleep(Int32):将当前线程挂起指定的毫秒数。

1
2
3
4
5
6
for (int i = 0; i < 100; i++)
{
    System.Threading.Thread.Sleep(50);
    label1.Text = i.ToString();
    label1.Refresh();
}

  2. 实现文件夹/文件打开操作

  System.Diagnostics.Process.Start(String, String):用指定的程序打开指定路径的文件。

1
2
3
4
5
6
7
8
9
10
11
12
// 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");

  3. 建立事件模板,然后调用

  由于事件的监视及管理是由Application对象进行的,程序员不需要知道用户何时响应事件或者是响应了什么事件,只需要为事件添加响应方法即可。添加方法”+=“,取消方法”-=“。参数sender为事件发出者;e为事件的附加数据,事件不同,e也不同。

  示例一:四个事件调用一个方法

1
2
3
4
5
6
7
8
9
10
11
12
13
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);     //通过再单击Tab键,可以实现函数的自动生成
}
private void textBox_MouseMove(object sender, MouseEventArgs e)     //建立事件模板
{
    TextBox tb = sender as TextBox;
    tb.BackColor = Color.Red;
}

  示例二:TextBox_KeyPress 只允许数字输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
    }
}

  4. 用代码在Form中写控件,同时可以编写控件数组

  首先用Label建立数组,接下来遍历数组,给数组的每个要素声明Label,接下来用Controls的Add方法将用代码写的控件添加到控件集中,同时设置控件的位置和长宽。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void Form1_Load(object sender, EventArgs e)
{
    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 = 714;
        lbs[i].Top = 30 * i + 14;       //设置控件的位置
        lbs[i].Width = 400;         //设置控件的宽度
        lbs[i].Text = "hhhhh";         //设置文本内容
        //批量写入事件
        lbs[i].MouseMove += new MouseEventHandler(Label_MouseMove);
    }
}
 
void Label_MouseMove(object sender, MouseEventArgs e)
{
    Label l = sender as Label;
    l.BackColor = Color.GreenYellow;
}

  5. 用代码执行事件

  首先是双击控件,生成一个button1_Click(object sender,EventArgs e)的函数,通过代码直接调用这个函数,既可以调用这个事件,说到底就是调用函数。

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

 

posted on   McDelfino  阅读(399)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2015-08-31 【171】IDL读取HDF文件
点击右上角即可分享
微信分享提示