一些常用的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;
代码介绍 MetroForWinForm(win8风格模版) using System; using System.Drawing; using System.Globalization; using System.Windows.Forms; using MetroFramework.Forms; namespace MetroFramework.Demo { public partial class MainForm : MetroForm { public MainForm() { InitializeComponent(); metroStyleManager.Theme = MetroThemeStyle.Default; metroStyleManager.Style = MetroColorStyle.Teal; } private void metroTileSwitch_Click(object sender, EventArgs e) { var m = new Random(); int next = m.Next(0, 13); metroStyleManager.Style = (MetroColorStyle)next; } private void metroTile1_Click(object sender, EventArgs e) { metroStyleManager.Theme = metroStyleManager.Theme == MetroThemeStyle.Light ? MetroThemeStyle.Dark : MetroThemeStyle.Light; } private void metroButton1_Click(object sender, EventArgs e) { MetroTaskWindow.ShowTaskWindow(this, "SubControl in TaskWindow", new TaskWindowControl(), 10); } private void metroButton2_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "Do you like this metro message box?", "Metro Title", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk); } private void metroButton5_Click(object sender, EventArgs e) { metroContextMenu1.Show(metroButton5, new Point(0, metroButton5.Height)); } private void metroButton6_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `OK` only button", "MetroMessagebox", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void metroButton10_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `OK` and `Cancel` button", "MetroMessagebox", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); } private void metroButton7_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Yes` and `No` button", "MetroMessagebox", MessageBoxButtons.YesNo, MessageBoxIcon.Question); } private void metroButton8_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Yes`, `No` and `Cancel` button", "MetroMessagebox", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); } private void metroButton11_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Retry` and `Cancel` button. With warning style.", "MetroMessagebox", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning); } private void metroButton9_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Abort`, `Retry` and `Ignore` button. With Error style.", "MetroMessagebox", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error); } private void metroButton12_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample `default` MetroMessagebox ", "MetroMessagebox"); } private void metroButton4_Click(object sender, EventArgs e) { var testform = new TestForm1(); testform.ShowDialog(); } private void metroButton4_Click_1(object sender, EventArgs e) { metroTextBox2.Focus(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值