小小飞鹰

     中国人缺少的是步骤,太急。练太极!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WinForm编码技巧

Posted on 2007-11-20 09:30  小小飞鹰  阅读(504)  评论(0编辑  收藏  举报


-----------------------
操作命令键:通过回车键移到下一控件

//重写ProcessCmdKey方法,用Enter键代替TAB键
  protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
  {
      if(keyData == Keys.Enter)//通过回车键移到下一控件
      {
          SendKeys.Send("{TAB}");
          return true;
      }  
          return base.ProcessCmdKey(ref msg, keyData);
  }

// 命令键是始终比常规输入键具有优先权的键。命令键的示例包括快
//捷键和菜单快捷方式;在派生类中重写 ProcessCmdKey 方法时,控
//件应返回 true 以指示它已处理该键; 对于未由该控件处理的键,应
//返回调用基类的 ProcessCmdKey 方法的结果。
-----------------------
MDI中设置主窗口的背景色
  
   主窗口加载时调用以下方法:
  /// <summary>
  /// 主窗口背景着色
  /// </summary>
  private void SetStyle()
  {
      foreach (Control ctl in this.Controls)
      {
          if (ctl.GetType().FullName == "System.Windows.Forms.MdiClient")
          {
           ctl.BackColor = Color.SteelBlue;
          }
      }
  }

-----------------------
.net程序中打开外部程序
例:打开IE
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
   
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
   
startInfo.Arguments = "http://EugeneWU0808.cnblogs.com">http://EugeneWU0808.cnblogs.com";

Process.Start(startInfo);


-----------------------
年,月的一些判断

  #region 判断一个月的天数,以及是否是润年
  public static int DaysByMonthAndYear(string  yearDate,string monthDate)
  {
   int year  = Convert.ToInt32(yearDate);
   int month = Convert.ToInt32(monthDate);
   if(month==1 || month==3 || month==5 || month==7 ||  month==8  || month==10 || month==12)
   {
    return 31; //如果月数是1/3/5/7/8/10/12直接返回
   }
   else if (month==4 || month==6 || month==9 || month==11) //
   {
    return 30;
   }
   else if(month==2)
   {
   
    if((((year%4==0&&(year%100!=0)) || (year%400==0))))  //如果是润年的话
    {
     return 29;
    }
    else //平年
    {
     return 28;
    }
   }

   return 0;

  }
  #endregion

  #region 判断一个月在哪个季度
  public static int AMonthInWhichQuarter(string monthDate)
  {
   int month = Convert.ToInt32(monthDate);
   if(month==1 || month==2 || month==3)
   {
    return 1;
   }
   else if( month==4 || month==5 || month==6)
   {
    return 2;
   }
   else if (month==7 || month==8 || month==9)
   {
    return 3;
   }
   else if (month==10 || month==11 || month==12)
   {
    return 4;
   }
   return 0;


  }
  #endregion

-----------------------

textBox在KeyPress中英文定制
/// <summary>
  /// 中英文定制
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
  {
e.Handled = true;

   }
    当把e.Handled 设置为 true时,如果此时KeyChar是单个字符时可以达到不输入的目的,
但是当你输入的是中文时这句并不起作用,我猜可能是因为点两个字节的原因;因此,用此
方法可以实现文本框中只能输入中文的目的
-----------------------
判断某个字符串是否在另一个字符串(数组)
示例代码:
 string strNum="001,003,005,008";
 string[] strArray=strNum.Split(',');//按逗号拆分,拆分字符为char或char数组
 
 Console.WriteLine(Array.IndexOf(strArray,"004").ToString());

 

  //判断字符串是否为连续的中文字符(不包含英文及其他任何符号和数字):
  Regex.IsMatch("中文","^[\u4e00-\u9fa5]+$");
-----------------------
快速获取事件日志中注册的所有事件源

public static string[] GetLogSources(string logName)
{
if (logName == null || logName.Length == 0) {
return new string[0];
}
 
RegistryKey localMachineKey = null;
RegistryKey logKey = null;
string[] sources = null;
 
try {
// The detail information about a log can be found at the registry.
// And the registry key for a log is:
// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\
//
string path = string.Concat(@"SYSTEM\CurrentControlSet\Services\Eventlog\", logName);
 
localMachineKey = Registry.LocalMachine;
logKey = localMachineKey.OpenSubKey(path);
if (logKey != null) {
sources = logKey.GetValue("Sources") as string[];
}
}
finally {
if (logKey != null) {
logKey.Close();
}
 
if (localMachineKey != null) {
localMachineKey.Close();
}
}
 
return (sources == null) ? new string[0] : sources;
}
-----------------------