zoukankan      html  css  js  c++  java
  • 【226】C# 相关功能实现代码

    目录:

      1. 实现代码的等待操作

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

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

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

      5. 用代码执行事件


      1. 实现代码的等待操作

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

    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. 用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:Desktop1.txt"); 
    
    // 3. 用Word的快捷方式打开Word文件:
      System.Diagnostics.Process.Start(@"F:DesktopWord 2010", @"F:Desktop1.docx");
    
    // 4. 用Firefox打开网址:www.baidu.com:
      System.Diagnostics.Process.Start(@"C:Program Files (x86)Mozilla Firefoxfirefox.exe", "www.baidu.com");
    

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

      由于事件的监视及管理是由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);     //通过再单击Tab键,可以实现函数的自动生成
            } 
            private void textBox_MouseMove(object sender, MouseEventArgs e)     //建立事件模板
            {
                TextBox tb = sender as TextBox;
                tb.BackColor = Color.Red;
            }
    

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

            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方法将用代码写的控件添加到控件集中,同时设置控件的位置和长宽。

            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)的函数,通过代码直接调用这个函数,既可以调用这个事件,说到底就是调用函数。

            private void button1_Click(object sender, EventArgs e)
            {
                axWindowsMediaPlayer1.URL = musicPath + @"music1.mp3";
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                button1_Click(button1, e);    //通过代码调用按钮单击事件,其他事件调用是类似的!
            }
    
  • 相关阅读:
    Django+nginx+uwsgi静态文件permission denied!
    部署Django项目到Centos6.9服务器
    Django Rest Framework 使用summernote上传图片(二)
    Django Rest Framework 使用summernote上传图片(一)
    基于DRF的企业级流程管理系统
    js获取cookie里保存的中文
    web Worker使js实现‘多线程’?
    css3折叠效果
    移动端适配方案研究
    css before,after伪元素妙用
  • 原文地址:https://www.cnblogs.com/alex-bn-lee/p/5825931.html
Copyright © 2011-2022 走看看