我们每天都使用VS.Net进行C/S系统、B/S系统、Smart-Client应用的开发,无论你是熟练的程序员还是刚学.net的新手,都是安装了VS.Net进行开发,但你有没有想过怎样利用工具提供的一些便利功能,来提高我们工作的质量和效率呢。本文收集了VS.Net中一些浅为人知的技巧与功能,希望能对帮助你更好的利用工具。
1. 快速切换选项卡
在应用的开发中,我们常常会开打多个窗体设计器、类文件与其它类型的文件,文件一多用鼠标切换时通常要进行点击几次,可能通过快捷键Ctrl+Tab或Shift+Ctrl+ Tab进行切换。也可以通过拖拽文件的标题对其进行重新排列。
2. 多文件查看
在VS.Net中,你可以同时查看两或多个文件,只需在打开选项卡中把想要查看的文件拖至IDE的右边或下边,你就可以垂直或水平平铺的方式查看文档。
![a.jpg](https://i-blog.csdnimg.cn/blog_migrate/30b584d0a3b4c52686788d0311c57ea0.jpeg)
3. 查看同一文档的不同部分
当需要对同一文档的不同部分代码的时候,我们可以通过拆分代码窗口来查看代码的不同部分。将鼠标移动到代码窗口右上角滚动条的上方,出现双向箭头时向下拖至你想拆分的位置,现在可以通过移动滚动条来查看代码的不同部分。
![2.jpg](https://i-blog.csdnimg.cn/blog_migrate/678a4a98a719fd4984fbc7d6c6738d1e.jpeg)
4. 管理重复使用的代码片断
在编写代码时,有一些常用的代码片断会在不同的项目或解决方案中重复使用,如文件创建说明、数据库连接字串等。可以使用工具箱利用以下方法进行重用:
选择要复用的代码片断;
将选择的代码拖到工具箱的常规卡,工具箱在会显示“文本……”(可以右键点
击从菜单中选择重名命名进行改名);
使用时,在插入代码的位置单击鼠标,然后在工具箱上双击要插入的代码(也可以把代码片断拖至要插入的位置)。
![3.jpg](https://i-blog.csdnimg.cn/blog_migrate/a1a6e35e193ce95e6aa1d0d16bf16f81.jpeg)
5. 使用渐进式搜索
代码的搜索方法,VS.Net可以使用编辑菜单中的“搜索”进行特定字符串的搜索,也可以使用“查找符号”来查找特定的方法或属性,但“渐进式搜索”可能就少有人知了,“渐进式搜索”可以根据你键入的字符中在当前打开的代码中进行查找。使用快捷方式Ctrl+I,在代码窗口出现一个向下的箭头加望远镜图案,IDE下面的状态栏显示“渐进式搜索”字样,键入要查找的字符,自动定位至字符出现的位置,查找下一个字符出现位置可以再按Ctrl+I,上一个字符出现位置可以按Shift+Ctrl+I继续进行查找。
![4.jpg](https://i-blog.csdnimg.cn/blog_migrate/24b55c83b36bb45de5c4363bda94c21c.jpeg)
6. 枚举容器中的控件,实现控件事件处理
我们知道,要在应用程序中使用事件,必须提供一个事件处理程序(事件处理方法),这通常用委托来实现。但当想对某个容器中的同类控件的相同事件都实现相同的处理方法时,可能通过枚举容器中的控件并指定相关委托来实现事件的处理。或许你会说,干吗要说得这么复杂,我可以在控件的属性中指定事件处理方法来实现,但当容器中的控件很多,或者在设计过程中加入了新的控件,逐个指定毕竟是很麻烦的一件事。
如下图所示,在应用中有两个文本框,我想指定两个文本框(用户名与密码)的Enter、Leave与Validating事件,当文本框获得焦点时改变其背景色,当离开时还原为系统文本颜色,并用Validating方法检查文本框内容是否为空,为空时用ErrorProvider显示错误信息。
![5.jpg](https://i-blog.csdnimg.cn/blog_migrate/e7cd578e0fc0615ae5bf001404d2d049.jpeg)
我通过方法AddEventHandler来枚举窗体中的控件,当它是文本框时指定事件的委托,代码如下:
/**/
/// <summary>
/// 枚举容器中的控件,并增加文本框的事件处理委托
/// </summary>
/// <param name="pnl">container</param>
private
void
AddEventHandler(Control pnl)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
foreach(Control ctrl in pnl.Controls)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(ctrl is TextBox)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ctrl.Enter+=new EventHandler(this.txt_Enter);
ctrl.Leave+=new EventHandler(this.txt_Leave);
ctrl.TextChanged+=new EventHandler(this.txt_TextChanged);
ctrl.Validating+=new CancelEventHandler(this.txt_Validating);
}
if(ctrl.HasChildren)
AddEventHandler(ctrl);
}
}
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <summary>
/// 实现文本框的Enter事件处理方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
txt_Enter(
object
sender,System.EventArgs e)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
TextBox tb=(TextBox)sender;
tb.BackColor=Color.BlanchedAlmond;
}
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <summary>
/// 实现文本框的Leave事件处理方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
txt_Leave(
object
sender,System.EventArgs e)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
TextBox tb=(TextBox)sender;
tb.BackColor=Color.FromKnownColor(KnownColor.Window);
}
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <summary>
/// 实现文本框的Validating事件处理方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
txt_Validating(
object
sender,System.ComponentModel.CancelEventArgs e)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
TextBox tb=(TextBox)sender;
errorProvider1.SetError(tb,"");
if(tb.Text.Length==0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
errorProvider1.SetError(tb,"Please input any text!");
tb.Focus();
}
}
public
Form1()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
AddEventHandler(this);
}
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
然后在Form1的构造函数中增加AddEventHandler(this)语句:
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
程序运行时文本框获得焦点时显示不同的背景色:
![6.JPG](https://i-blog.csdnimg.cn/blog_migrate/e2123b23b81e3406fcbb917c8892e985.jpeg)
![6.JPG](https://i-blog.csdnimg.cn/blog_migrate/e2123b23b81e3406fcbb917c8892e985.jpeg)
失去焦点时如果文本框内容长度为零,显示提示信息:
![7.JPG](https://i-blog.csdnimg.cn/blog_migrate/b89e7320f0cbe9914ee968472bd1cbad.jpeg)
同样的道理,你可以使用上面的方法对容器中的其它控件指定不同的事件处理方法
。
![emsmile.gif](/Emoticons/emsmile.gif)
评论: