.NET开发中的一些小技巧

news/2024/6/18 21:13:11

这篇文章来自是Mukund Pujari的《Some Cool Tips for .NET》,本人给大家翻译总结一下,我英语水平也就那么回事,不合适的地方还是请大家提出来。

1. 如何创建一个可改变大小没有标题栏的窗体?(How to create a form with resizing borders and no title bar?)

form1.Text = string. Empty;
form1.ControlBox = false;


2. 如何在.NET的Windows窗体上启用XP主题集?(How to use XP Themes with Windows Forms using the .NET?)

确认你的控件中FlatStyle属性已经修改为System,再修改Main方法。

static void Main()
{
  Application.EnableVisualStyles();
  Application.DoEvents();
  Application. Run(new Form1());
}


3. 如何为一个窗体设置一个默认按钮?(How to set the default button for a form?)

form1.AcceptButton = button1;

4. 如何为一个窗体设置一个取消按钮?(How to set the Cancel button for a form?)

form1.CancelButton = button1;

5. 如何阻止一个窗体标题显示在任务栏上?(How to prevent a form from being shown in the taskbar?)

设置窗体的ShowIntaskbar属性为False

6. 如何用现有可用字体绑定到ComboBox控件?(How to fill a ComboBox with the available fonts?)

comboBox1.Items.AddRange (FontFamily.Families);

7. 如何禁止TextBox控件默认的邮件菜单?(How to disable the default ContextMenu of a TextBox?)

textBox1.ContextMenu = new ContextMenu ();

8. 如何获取“我的文档”等一些系统文件夹路径?(How to get the path for "My Documents" and other system folders?)

Environment.SpecialFolder中包含了一些系统文件夹信息
MessageBox.Show(Environment.GetFolderPath( Environment.SpecialFolder.Personal ));

9. 如何获取应用程序当前执行的路径?(How to get the path to my running EXE?)

string appPath = Application.ExecutablePath;

10. 如何确定当前运行的系统?(How to determine which operating system is running?)

OperatingSystem os = Environment.OSVersion;
MessageBox.Show(os.Version.ToString());
MessageBox.Show(os.Platform.ToString());

11. 如何从完整的路径中获取文件名?(How to get a file's name from the complete path string?)

用System.IO.Path.GetFileName 和 System.IO.Path.GetFileNameWithoutExtension(无扩展名)的方法

12. 如何从完整的路径中获取文件扩展名?(How to get a file's extension from the complete path string?)

用System.IO.Path.GetExtension方法

13. 如何使没有选择日期的DateTimePicker控件为空文本?(How to make the DateTimePicker show empty text if no date is selected?)

dateTimePicker1.CustomFormat = " ";
dateTimePicker1.Format = DateTimePickerFormat.Custom;

14. 如何在Report Viewer中隐藏Crystal Report的状态栏?(How to hide the status bar of Crystal Report in Report Viewer?)

foreach(object obj in this.crystalReportViewer1.Controls)
{     
  if( obj.GetType()== typeof(System.Windows.Forms.StatusBar))
 {     
  StatusBar sBar=(StatusBar)obj;
  sBar.Visible=false;
 }     
}


15. 如何利用Crystal Report程序来生成PDF版本?(How to generate PDF version of Crystal Report programmatically?)

ReportDocument O_Report=new ReportDocument();
ExportOptions exportOpts = new ExportOptions();
PdfRtfWordFormatOptions pdfFormatOpts = new PdfRtfWordFormatOptions ();
DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
exportOpts = O_Report.ExportOptions;
// 设置PDF格式   
exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat;
exportOpts.FormatOptions = pdfFormatOpts;
// 设置文件选项和导出
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
diskOpts.DiskFileName = "C://Trial.pdf"; //设置PDF导出路径    
exportOpts.DestinationOptions = diskOpts;
O_Report.Export ();


16.通过代码如何输入多行文本?(How to enter multiline text in textbox through code? )

利用TextBox控件的LINES属性
string [] strAddress = {"Mukund Pujari","Global Transformation Technologies","Pune, India"};
textBox1.MultiLine=true;
textBox1.Lines=strAddress;

或者
textBox1.Text="Line 1/r/nLine2/r/nLine3.";

或者
用"System.Environment.NewLine"来替代换行符号

17. 如何在DataGrid中去掉CheckBox不确定状态?(How to remove the indeterminate status of checkbox in datagrid?)

DataGridTableStyle ts1 = new DataGridTableStyle(); //创建Table样式
ts1.MappingName = "Items"; //分配要应用样式的Data Table
DataGridColumnStyle boolCol = new DataGridBoolColumn(); // 创建CheckBox列
boolCol.MappingName = "ch"; //分配数据列名称
boolCol.AllowNull=false; // 修改AllowNull属性

18. 如何在用一个数据源DataTable绑定两个控件,确保变化不反映在两个控件中?( How to bind two controls to the same DataTable without having changes in one control also change the other control?)

我们在一个Form中放置一个ListBox和一个ComboBox控件,当数据源是一个DataTable而且绑定的ValueMember一致的时候我们选择ListBox中的一个Item时,ComboBox控件中的相同的Item也会被自动选中,我们可以采取建立新的上下文绑定对象来拒绝这样的同步操作
comboBox1.DataSource = dataset.Tables[ "Items" ];
comboBox1.ValueMember = "CustomerID";
comboBox1.DisplayMember = "CustomerID";

listBox1.BindingContext = new BindingContext(); // 设置新的上下文绑定对象
listBox1.DataSource = dataset.Tables[ "Items" ];
listBox1.ValueMember = "CustomerID";
listBox1.DisplayMember = "CustomerID";

19. 一个简单的创建链接字符串的方法。(An easy way to build connection string.)

记事本创建一个New.udl的文件,一个Microsoft 数据链接文件
双击打开,熟悉吧
按照向导创建完成一个数据库链接,测试成功
确定后,链接字符串写入这个文件,用记事本打开就看到了

20. 如何打开客户端E-Mail程序,Windows应用和Web应用?( How to open default E-mail client on your system with all parameters entered in it,like Outlook Express or Eudora, from your .NET windows or Web Application? )

Web Application:
A href="mailto:email@address1.com,email@address2.com?cc=email@address3.com&Subject=Hello&body=Happy New Year"

Windows Application:
引用System.Diagnostics.Process 命名空间
Process process = new Process();
process.StartInfo.FileName = "mailto:email@address1.com,email@address2.com?subject=Hello&cc=email@address3.com
&bcc=email@address4.com&body=Happy New Year" ;
process.Start();

21. VB.NET和C#有什么不同?( What is difference beween VB.NET and C#.NET? )

去微软下载一个文档吧,http://download.microsoft.com/download/6/3/5/6354bf47-c597-4029-89e9-2495e7539ab9/vbcsharpwp.exe

22. How to find whether your system has mouse or the number of buttons, whether it has wheel, or whether the mouse buttons are swapped or size of your monitor and many such information?

23. 如何使Windows Form上的Panel或者Label控件半透明?(How to make a Panel or Label semi-transparent on a Windows Form? )

通过设置控件背景色的alpha值
panel1.BackColor = Color.FromArgb(65, 204, 212, 230);
注意:在设计时手动输入这些值,不要用颜色选取

24. C#程序的主函数写[STA Thread] 属性是什么目的?(What is the purpose of the [STA Thread] attribute for the Main method of a C# program? )

http://community.csdn.net/Expert/topic/4132/4132313.xml?temp=.2285272


25. 如何触发Button的Click事件?(How to trigger a button click event? )

button1.PerformClick();

 

http://www.niftyadmin.cn/n/612072.html

相关文章

python声音捕获_使用Python检测和录制音频

感谢cryo的改进版本,我基于下面的测试代码:#Instead of adding silence at start and end of recording (values0) I add the original audio . This makes audio sound more natural as volume is >0. See trim()#I also fixed issue with the previ…

linux下解包bin二进制文件_linux下如何使用docker二进制文件安装

原标题:linux下如何使用docker二进制文件安装1,下载二进制文件https://download.docker.com/linux/static/stable/x86_64/docker-18.03.1-ce.tgz2,解压二进制文件tar xzvf docker-18.03.1-ce.tgz3,复制二进制文件到/usr/bin目录下cp docker/* /usr/bin/4,检查是否安…

从富文本中截取图片_搞来了一款科研神器OCR文本识别翻译神器

removing barriers on the way of knowledge---摘自sci-hub的首页知识无国界,希望也不再用于套路今天小编发现了一款可以OCR文本识别,同时自动翻译的科研神器,分分钟搞定PDF文本识别、sci论文翻译,用起来特别6(注意这个软件杀毒软…

交叉表示例

假设有张学生成绩表(CJ)如下 [姓名] [学科] [成绩] 张三 语文 80 张三 数学 90 张三 物理 85 李四 语文 85 李四 数学 92 李四 物理 82 王五 数学 60 想变成 [姓名] [语文] [数学] [物理] 张三 80 90 85 李四 85 92 82 王五 null 60 null declare sql varchar(4000) set sql s…

佳能g2810打印机扫描怎么用_佳能激光打印机怎么样 佳能激光打印机多少钱【详解】...

佳能激光打印机在我们的工作中提供了便捷,而且能够快速的打印出用户们想要的图像和资料内容,所以很多人选择购买该品牌打印机。可是佳能激光打印机 怎么样 呢?我们在选择佳能打印机的时候要注意,应该如何更好的选择&#xff0c…

电池pack结构_动力电池成本结构分析

导读:新能源车的发展既有赖于政策的推动,也需要动力电池持续降本的支持,本周专题我们研究了动力电池的成本结构。我们在动力电池成 本模型里将 PACK 成本拆分成材料成本和生产成本,其中材料成本又包括电芯材料、模组材料及 PACK 材…

内核aio_理解 Java 三种IO模型 BIO/ NIO/ AIO,面试必考问题!

↑ 点击上面 “时代Java”关注我们,关注新技术,学习新知识!Java 中的 BIO、NIO和 AIO 理解为是 Java 语言对操作系统的各种 IO 模型的封装。程序员在使用这些 API 的时候,不需要关心操作系统层面的知识,也不需要根据不…

90土壤分类中英文_彩虹之花——鸢尾的园艺分类及应用

引言:古希腊语中“鸢尾”一词意为彩虹,与美丽的自然现象相比,鸢尾花朵的色彩则更加丰富。鸢尾因花美丽和香气独特而受赞赏,鸢尾根提取物可用于香水,葡萄酒,伏特加酒和糖果的生产。全世界鸢尾的种类超过六万…