C# WinForm 小技巧

1.窗体页面按键判断
   (1)单键判断
      if (e.KeyCode == Keys.Delete)
       {
         //处理逻辑
       } 
   (2)组合建判断:  
      if (e.Modifiers.CompareTo(Keys.Control) == 0 && e.KeyCode == Keys.Delete)
      {
       //处理逻辑
      }

2.C#WinForm的密码输入框禁止粘贴和右键
       private void LoginForm_Load(object sender, EventArgs e)
        {
            txtPassword.ContextMenu = new ContextMenu();
            txtPassword.KeyPress += new KeyPressEventHandler(txtPassword_KeyPress);
        }

        void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 22)
            {
                e.Handled = true;
            }
        }

3.    拷贝文件夹(递归)

        //要拷贝的文件路径
          string dataFileName="";
        //指定文件路径的目录信息
          DirectoryInfo source = new DirectoryInfo(Path.GetDirectoryName(dataFileName));
        //本地文件路径
         string localFilePath = "C:\\temp\\";
        //本地文件目录
        DirectoryInfo localDirectory = new DirectoryInfo(localFilePath);
     
         /// <summary>
        /// 拷贝文件夹()
        /// </summary>
        /// <param name="source">要拷贝指定文件目录信息</param>
        /// <param name="destination">本地文件目录信息</param>
        private void CopyDirectory(DirectoryInfo source, DirectoryInfo destination)
        {            
            if (!destination.Exists) {
                destination.Create();
            }
            FileInfo[] files = source.GetFiles();
            foreach (FileInfo file in files)
            {
                file.CopyTo(Path.Combine(destination.FullName, file.Name));
            }
            DirectoryInfo[] dirs = source.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {                           
                string destinationDir = Path.Combine(destination.FullName, dir.Name);                         
                CopyDirectory(dir, new DirectoryInfo(destinationDir));
            }
        }

 4. 获取资源文件(Xml)--要在同一命名空间下获取
     XmlDocument myXml = new XmlDocument();           
     string fileName = "AdminClient.Resources.bpmsCodes.xml";
     Assembly asm = Assembly.GetExecutingAssembly();//读取嵌入式资源
     Stream strm = asm.GetManifestResourceStream(fileName);
     myXml.Load(strm);//读取指定的XML文档

5.设置窗体的初始位置
      this.StartPosition = FormStartPosition.Manual; //必须先设置这个属性
      this.Location = = new Point(this.pointX, this.pointY + this.InputControl.Height + 30);
 

转载于:https://www.cnblogs.com/chunfeng/archive/2011/06/09/2076132.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值