Visual Studio.Net鲜为人知的技巧
    我们每天都使用VS.Net进行C/S系统、B/S系统、Smart-Client应用的开发,无论你是熟练的程序员还是刚学.net的新手,都是安装了VS.Net进行开发,但你有没有想过怎样利用工具提供的一些便利功能,来提高我们工作的质量和效率呢。本文收集了VS.Net中一些浅为人知的技巧与功能,希望能对帮助你更好的利用工具。
1. 快速切换选项卡
    在应用的开发中,我们常常会开打多个窗体设计器、类文件与其它类型的文件,文件一多用鼠标切换时通常要进行点击几次,可能通过快捷键Ctrl+Tab或Shift+Ctrl+ Tab进行切换。也可以通过拖拽文件的标题对其进行重新排列。
2. 多文件查看
    在VS.Net中,你可以同时查看两或多个文件,只需在打开选项卡中把想要查看的文件拖至IDE的右边或下边,你就可以垂直或水平平铺的方式查看文档。   
 
   
   
    
 
   
3. 查看同一文档的不同部分
    当需要对同一文档的不同部分代码的时候,我们可以通过拆分代码窗口来查看代码的不同部分。将鼠标移动到代码窗口右上角滚动条的上方,出现双向箭头时向下拖至你想拆分的位置,现在可以通过移动滚动条来查看代码的不同部分。   
     
    

   
4.   管理重复使用的代码片断
    在编写代码时,有一些常用的代码片断会在不同的项目或解决方案中重复使用,如文件创建说明、数据库连接字串等。可以使用工具箱利用以下方法进行重用:
    选择要复用的代码片断;
    将选择的代码拖到工具箱的常规卡,工具箱在会显示“文本……”(可以右键点  击从菜单中选择重名命名进行改名); 
    使用时,在插入代码的位置单击鼠标,然后在工具箱上双击要插入的代码(也可以把代码片断拖至要插入的位置)。
 
  
   5. 使用渐进式搜索
    代码的搜索方法,VS.Net可以使用编辑菜单中的“搜索”进行特定字符串的搜索,也可以使用“查找符号”来查找特定的方法或属性,但“渐进式搜索”可能就少有人知了,“渐进式搜索”可以根据你键入的字符中在当前打开的代码中进行查找。使用快捷方式Ctrl+I,在代码窗口出现一个向下的箭头加望远镜图案,IDE下面的状态栏显示“渐进式搜索”字样,键入要查找的字符,自动定位至字符出现的位置,查找下一个字符出现位置可以再按Ctrl+I,上一个字符出现位置可以按Shift+Ctrl+I继续进行查找。 
 
 
6.   枚举容器中的控件,实现控件事件处理
    我们知道,要在应用程序中使用事件,必须提供一个事件处理程序(事件处理方法),这通常用委托来实现。但当想对某个容器中的同类控件的相同事件都实现相同的处理方法时,可能通过枚举容器中的控件并指定相关委托来实现事件的处理。或许你会说,干吗要说得这么复杂,我可以在控件的属性中指定事件处理方法来实现,但当容器中的控件很多,或者在设计过程中加入了新的控件,逐个指定毕竟是很麻烦的一件事。
    如下图所示,在应用中有两个文本框,我想指定两个文本框(用户名与密码)的Enter、Leave与Validating事件,当文本框获得焦点时改变其背景色,当离开时还原为系统文本颜色,并用Validating方法检查文本框内容是否为空,为空时用ErrorProvider显示错误信息。

    我通过方法AddEventHandler来枚举窗体中的控件,当它是文本框时指定事件的委托,代码如下: 
   
        /// <summary>
    /// <summary> 
 /// 枚举容器中的控件,并增加文本框的事件处理委托
        /// 枚举容器中的控件,并增加文本框的事件处理委托 
 /// </summary>
        /// </summary> 
 /// <param name="pnl">container</param>
        /// <param name="pnl">container</param> 
 private void AddEventHandler(Control pnl)
        private void AddEventHandler(Control pnl) 
 {
        { 
 foreach(Control ctrl in pnl.Controls)
            foreach(Control ctrl in pnl.Controls) 
 {
            { 
 if(ctrl is TextBox)
                if(ctrl is TextBox) 
 {
                { 
 ctrl.Enter+=new EventHandler(this.txt_Enter);
                    ctrl.Enter+=new EventHandler(this.txt_Enter); 
 ctrl.Leave+=new EventHandler(this.txt_Leave);
                    ctrl.Leave+=new EventHandler(this.txt_Leave); 
 ctrl.TextChanged+=new EventHandler(this.txt_TextChanged);
                    ctrl.TextChanged+=new EventHandler(this.txt_TextChanged); 
 ctrl.Validating+=new CancelEventHandler(this.txt_Validating);
                    ctrl.Validating+=new CancelEventHandler(this.txt_Validating); 
 }
                } 
 if(ctrl.HasChildren)
                if(ctrl.HasChildren) 
 AddEventHandler(ctrl);
                    AddEventHandler(ctrl); 
 }
            } 
 }
        } 
 
 
 
 
 /// <summary>
        /// <summary> 
 /// 实现文本框的Enter事件处理方法
        /// 实现文本框的Enter事件处理方法 
 /// </summary>
        /// </summary> 
 /// <param name="sender"></param>
        /// <param name="sender"></param> 
 /// <param name="e"></param>
        /// <param name="e"></param> 
 private void txt_Enter(object sender,System.EventArgs e)
        private void txt_Enter(object sender,System.EventArgs e) 
 {
        { 
 TextBox tb=(TextBox)sender;
            TextBox tb=(TextBox)sender; 
 tb.BackColor=Color.BlanchedAlmond;
            tb.BackColor=Color.BlanchedAlmond; 
 }
        } 
 
 
 /// <summary>
        /// <summary> 
 /// 实现文本框的Leave事件处理方法
        /// 实现文本框的Leave事件处理方法 
 /// </summary>
        /// </summary> 
 /// <param name="sender"></param>
        /// <param name="sender"></param> 
 /// <param name="e"></param>
        /// <param name="e"></param> 
 private void txt_Leave(object sender,System.EventArgs e)
        private void txt_Leave(object sender,System.EventArgs e) 
 {
        { 
 TextBox tb=(TextBox)sender;
            TextBox tb=(TextBox)sender; 
 
             
 tb.BackColor=Color.FromKnownColor(KnownColor.Window);
            tb.BackColor=Color.FromKnownColor(KnownColor.Window); 
 }
        } 
 
 
 /// <summary>
        /// <summary> 
 /// 实现文本框的Validating事件处理方法
        /// 实现文本框的Validating事件处理方法 
 /// </summary>
        /// </summary> 
 /// <param name="sender"></param>
        /// <param name="sender"></param> 
 /// <param name="e"></param>
        /// <param name="e"></param> 
 private void txt_Validating(object sender,System.ComponentModel.CancelEventArgs e)
        private void txt_Validating(object sender,System.ComponentModel.CancelEventArgs e) 
 {
        { 
 TextBox tb=(TextBox)sender;
            TextBox tb=(TextBox)sender; 
 errorProvider1.SetError(tb,"");
            errorProvider1.SetError(tb,""); 
 if(tb.Text.Length==0)
            if(tb.Text.Length==0) 
 {
            { 
 errorProvider1.SetError(tb,"Please input any text!");
                errorProvider1.SetError(tb,"Please input any text!"); 
 tb.Focus();
                tb.Focus(); 
 }
            } 
 }
        } 
       
   
   
   
        public Form1()
    public Form1() 
 {
        { 
 //
            // 
 // Windows 窗体设计器支持所必需的
            // Windows 窗体设计器支持所必需的 
 //
            // 
 InitializeComponent();
            InitializeComponent(); 
 
         
 AddEventHandler(this);
            AddEventHandler(this); 
 }
        } 
       
   
   
       
            
 
           
        
   
 /// <summary>
    /// <summary> 
 /// 枚举容器中的控件,并增加文本框的事件处理委托
        /// 枚举容器中的控件,并增加文本框的事件处理委托 
 /// </summary>
        /// </summary> 
 /// <param name="pnl">container</param>
        /// <param name="pnl">container</param> 
 private void AddEventHandler(Control pnl)
        private void AddEventHandler(Control pnl) 
 {
        { 
 foreach(Control ctrl in pnl.Controls)
            foreach(Control ctrl in pnl.Controls) 
 {
            { 
 if(ctrl is TextBox)
                if(ctrl is TextBox) 
 {
                { 
 ctrl.Enter+=new EventHandler(this.txt_Enter);
                    ctrl.Enter+=new EventHandler(this.txt_Enter); 
 ctrl.Leave+=new EventHandler(this.txt_Leave);
                    ctrl.Leave+=new EventHandler(this.txt_Leave); 
 ctrl.TextChanged+=new EventHandler(this.txt_TextChanged);
                    ctrl.TextChanged+=new EventHandler(this.txt_TextChanged); 
 ctrl.Validating+=new CancelEventHandler(this.txt_Validating);
                    ctrl.Validating+=new CancelEventHandler(this.txt_Validating); 
 }
                } 
 if(ctrl.HasChildren)
                if(ctrl.HasChildren) 
 AddEventHandler(ctrl);
                    AddEventHandler(ctrl); 
 }
            } 
 }
        } 
 
 
 
 
 /// <summary>
        /// <summary> 
 /// 实现文本框的Enter事件处理方法
        /// 实现文本框的Enter事件处理方法 
 /// </summary>
        /// </summary> 
 /// <param name="sender"></param>
        /// <param name="sender"></param> 
 /// <param name="e"></param>
        /// <param name="e"></param> 
 private void txt_Enter(object sender,System.EventArgs e)
        private void txt_Enter(object sender,System.EventArgs e) 
 {
        { 
 TextBox tb=(TextBox)sender;
            TextBox tb=(TextBox)sender; 
 tb.BackColor=Color.BlanchedAlmond;
            tb.BackColor=Color.BlanchedAlmond; 
 }
        } 
 
 
 /// <summary>
        /// <summary> 
 /// 实现文本框的Leave事件处理方法
        /// 实现文本框的Leave事件处理方法 
 /// </summary>
        /// </summary> 
 /// <param name="sender"></param>
        /// <param name="sender"></param> 
 /// <param name="e"></param>
        /// <param name="e"></param> 
 private void txt_Leave(object sender,System.EventArgs e)
        private void txt_Leave(object sender,System.EventArgs e) 
 {
        { 
 TextBox tb=(TextBox)sender;
            TextBox tb=(TextBox)sender; 
 
             
 tb.BackColor=Color.FromKnownColor(KnownColor.Window);
            tb.BackColor=Color.FromKnownColor(KnownColor.Window); 
 }
        } 
 
 
 /// <summary>
        /// <summary> 
 /// 实现文本框的Validating事件处理方法
        /// 实现文本框的Validating事件处理方法 
 /// </summary>
        /// </summary> 
 /// <param name="sender"></param>
        /// <param name="sender"></param> 
 /// <param name="e"></param>
        /// <param name="e"></param> 
 private void txt_Validating(object sender,System.ComponentModel.CancelEventArgs e)
        private void txt_Validating(object sender,System.ComponentModel.CancelEventArgs e) 
 {
        { 
 TextBox tb=(TextBox)sender;
            TextBox tb=(TextBox)sender; 
 errorProvider1.SetError(tb,"");
            errorProvider1.SetError(tb,""); 
 if(tb.Text.Length==0)
            if(tb.Text.Length==0) 
 {
            { 
 errorProvider1.SetError(tb,"Please input any text!");
                errorProvider1.SetError(tb,"Please input any text!"); 
 tb.Focus();
                tb.Focus(); 
 }
            } 
 }
        } 
       然后在Form1的构造函数中增加AddEventHandler(this)语句:
 public Form1()
    public Form1() 
 {
        { 
 //
            // 
 // Windows 窗体设计器支持所必需的
            // Windows 窗体设计器支持所必需的 
 //
            // 
 InitializeComponent();
            InitializeComponent(); 
 
         
 AddEventHandler(this);
            AddEventHandler(this); 
 }
        } 
       程序运行时文本框获得焦点时显示不同的背景色:  
  
  
 
   
   失去焦点时如果文本框内容长度为零,显示提示信息: 
           
同样的道理,你可以使用上面的方法对容器中的其它控件指定不同的事件处理方法 。
。 
           
 。
。 
            
                    
                     
                    
                 
                    
                 
 
        

 
             
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号