日期:2011-07-27  浏览次数:20983 次

这篇文章来自是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(); //创