WinForm winth C # programming tips

Source: Internet
Author: User

1. How to Create a form with no title bar size changed? (How to create a form with resizing borders and no title bar ?)
 
Form1.Text = string. Empty;
Form1.ControlBox = false;
 
2. How to enable the XP topic set on. NET Windows Forms? (How to use XP Themes with Windows Forms using the. NET ?)
 
Make sure that the FlatStyle attribute in your control has been changed to System, and then modify the Main method.
 
Static void Main ()
{
Application. EnableVisualStyles ();
Application. DoEvents ();
Application. Run (new Form1 ());
}
 
3. How do I set a default button for a form? (How to set the default button for a form ?)
 
Form1.AcceptButton = button1;
 
4. How do I set a Cancel button for a form? (How to set the Cancel button for a form ?)
 
Form1.CancelButton = button1;
 
5. How can I prevent a form title from being displayed on the taskbar? (How to prevent a form from being shown in the taskbar ?)
 
Set the ShowIntaskbar attribute of the form to False.
 
6. How to bind existing fonts to the ComboBox control? (How to fill a ComboBox with the available fonts ?)
 
ComboBox1.Items. AddRange (FontFamily. Families );
 
7. How do I disable the TextBox Control's default mail menu? (How to disable the default ContextMenu of a TextBox ?)
 
TextBox1.ContextMenu = new ContextMenu ();
 
8. How do I obtain some system folder paths such as my documents? (How to get the path for "My Documents" and other system folders ?)
 
Environment. SpecialFolder contains some System Folder Information.
MessageBox. Show (Environment. GetFolderPath (Environment. SpecialFolder. Personal ));
 
9. How to obtain the PATH currently executed by the application? (How to get the path to my running EXE ?)
 
String appPath = Application. ExecutablePath;
 
10. How do I determine the currently running system? (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 the file name from the complete path? (How to get a file's name from the complete path string ?)
 
Using System. IO. Path. GetFileName and System. IO. Path. GetFileNameWithoutExtension (without extension)
 
12. How to get the file extension from the complete path? (How to get a file's extension from the complete path string ?)
 
Use System. IO. Path. GetExtension
 
13. How do I enter multiple lines of text through code? (How to enter multiline text in textbox through code? )
 
Use the lines property of the TextBox Control
String [] strAddress = {"Mukund Pujari", "Global Transformation Technologies", "Pune, India "};
TextBox1.MultiLine = true;
TextBox1.Lines = strAddress;
 
Or
TextBox1.Text = "Line 1 \ r \ nLine2 \ r \ nLine3 .";
 
Or
Use "System. Environment. NewLine" to replace line breaks.
 
14. How to remove the uncertain CheckBox status from the DataGrid? (How to remove the indeterminate status of checkbox in datagrid ?)
 
DataGridTableStyle ts1 = new DataGridTableStyle (); // create a Table style
Ts1.MappingName = "Items"; // you can specify the Data Table to be applied.
Maid (); // create a CheckBox Column
BoolCol. MappingName = "ch"; // assign a data column name
BoolCol. AllowNull = false; // modify the AllowNull attribute
 
15. How can I bind two controls with a data source DataTable to ensure that the changes are not reflected in the two controls? (How to bind two controls to the same DataTable

Without having changes in one control also change the other control ?)
 
We place a ListBox and a ComboBox control in a Form. When the data source is a able and the bound ValueMember is consistent, we select

When an Item is specified, the same Item in the ComboBox control will be automatically selected. We can create a new context binding object to reject such synchronization operations.
ComboBox1.DataSource = dataset. Tables ["Items"];
ComboBox1.ValueMember = "CustomerID ";
ComboBox1.DisplayMember = "CustomerID ";
 
ListBox1.BindingContext = new BindingContext (); // sets the new context binding object.
ListBox1.DataSource = dataset. Tables ["Items"];
ListBox1.ValueMember = "CustomerID ";
ListBox1.DisplayMember = "CustomerID ";
 
16. A simple method for creating database connection strings. (An easy way to build connection string .)
Create a New. udl file (a Microsoft Data Link file), double-click it, and follow the Wizard to create a database link. The test is successful. Click OK.

String to write this file. Open it in notepad and you will see the database connection string
 
17. How to Make the Panel or Label control on Windows Form translucent? (How to make a Panel or Label semi-transparent on a Windows Form? )
 
By setting the alpha value of the control background color
Panel1.BackColor = Color. FromArgb (65,204,212,230 );
Note: manually enter these values during design. Do not select them by color.
 
18. How to trigger the Button Click event? (How to trigger a button click event? )
 
Button1.w.mclick ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.