一些常用的tips(for winform)

转自:http://www.codeproject.com/useritems/tips.asp

Introduction

These are some tips for commonly faced problems in .NET . Some of these tips are mine and some of these i have got from diffeent sources.

1. How to create a form with resizing borders and no title bar?

Set the form's Text and ControlBox properties.
 
form1.Text = string. Empty;
form1.ControlBox = false;

2. How to use XP Themes with Windows Forms using the .NET?

You need to install XP on your machine and the theme should be set to XP.You also need to ensure that the FlatStyle property of the control is changed to System and then add following line of code in your Main() method
static void Main() 
{
Application.EnableVisualStyles();
Application.DoEvents();
Application. Run(new Form1());
}

3. How to set the default button for a form?

Default Button of a form means that button on form whose click event fires when Enter key is pressed. To make a button on form as default set the form's AcceptButton property. You can do this either through the designer, or through code such as
form1.AcceptButton = button1;

4. How to set the Cancel button for a form?

Cancel Button of a form means that button on form whose click event fires when ESC key is pressed. To make a button on form as Cancel set the form's CancelButton property. You can do this either through the designer, or through code such as
form1.CancelButton = button1;

5. How to prevent a form from being shown in the taskbar?

Set the form's ShowInTaskbar property to False to prevent it from being displayed in the Windows taskbar.

6. How to fill a ComboBox with the available fonts?

comboBox1.Items.AddRange (FontFamily.Families);

7. How to enable the mnemonics (underline) being displayed when an application is launched?

Usually the underline appears only after you press the Alt Key, but you can enable it by changing the Operating System Settings. On Windows XP, Right Click Desktop to bring up the Display Properties Dialog and then choose Appearance tab and then the Effects Button and uncheck the checkbox "Hide Underlined letters for keyboard navigation until I press the ALT Key".

8. How to disable the default ContextMenu of a TextBox?

To prevent the default context menu of a TextBox from showing up, assign a empty context menu as shown below:
textBox1.ContextMenu = new ContextMenu (); 

9. How to get the path for "My Documents" and other system folders?

Use the GetFolderPath method of the System.Environment class to retrieve this information.
MessageBox.Show(
Environment.GetFolderPath( Environment.SpecialFolder.Personal ) );

10. How to get the path to my running EXE?

The Application class has a static member ExecutablePath that has this information.
string appPath = Application.ExecutablePath; 

11. How to determine which operating system is running?

Use System.Environment's OSVersion static (shared) property.
OperatingSystem os = Environment.OSVersion;
MessageBox.Show(os.Version.ToString());
MessageBox.Show(os.Platform.ToString());

12. How to get a file's name from the complete path string?

Use System.IO.Path.GetFileName and System.IO.Path.GetFileNameWithoutExtension static methods.

13. How to get a file's extension from the complete path string?

Use System.IO.Path.GetExtension static method.

14. How to make the DateTimePicker show empty text if no date is selected?

Use following code in some Button Click event:
dateTimePicker1.CustomFormat=" ";
dateTimePicker1.Format=DateTimePickerFormat.Custom;

15. How to hide the status bar of Crystal Report in Report Viewer?

The following block makes the status bar of Crystal Report invisible.
foreach(object obj in this.crystalReportViewer1.Controls)
{
if( obj.GetType().ToString()== "System.Windows.Forms.StatusBar")
{
StatusBar sBar=(StatusBar)obj;
sBar.Visible=false;
}
}

16. How to generate PDF version of Crystal Report programmatically?

Following block generates 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;

// Set the PDF format options.
exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat;
exportOpts.FormatOptions = pdfFormatOpts;

// Set the disk file options and export.
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
diskOpts.DiskFileName = "C://Trial.pdf"; // This is the path where the file will be saved
exportOpts.DestinationOptions = diskOpts;

O_Report.Export ();

17. How to enter multiline text in textbox through code?

Sometimes it is needed to show data on different lines. The first idea that comes is set MULTILINE Property to true and use '\n' escape sequence for this. But this escape sequence is not supported in .NET textbox. Still it very easy to overcome this problem. To assign multiline text at design time, in the designer window use the LINES property of TextBox control. For achieving this at runtime, create an array of string and assign it to LINES property of Textbox as shown below.
string [] strAddress = {"Mukund Pujari","Global Transformation Technologies","Pune, India"};
textBox1.MultiLine=true;
textBox1.Lines=strAddress;
posted @ 2005-12-22 09:10  吴建明  阅读(1255)  评论(0编辑  收藏  举报