1. Basic controls
1. Button (button)
Functions: Execute a command or trigger an event
Basic Usage:
// Create buttonButton btn = new Button(); = "Click Me"; = new Point(50, 50); += Btn_Click; // Bind click event // Add to form(btn); // Event handling methodprivate void Btn_Click(object sender, EventArgs e) { ("The button was clicked!"); }
Common properties:
-
Text
: Text displayed by the button -
Enabled
: Whether to enable the button -
Visible
: Is it visible -
BackColor
: Background color -
ForeColor
: Text color
2. Label (label)
Functions: Display static text or image
Basic Usage:
Label lbl = new Label(); = "This is a tag"; = new Point(50, 100); = true; // Automatically resize to fit text (lbl);
Common properties:
-
Text
: The displayed text -
AutoSize
: Whether to automatically resize -
ForeColor
: Text color -
BackColor
: Background color -
Font
: Font settings
3. TextBox (text box)
Functions: Allow users to enter or edit text
Basic Usage:
TextBox txt = new TextBox(); = new Point(50, 150); = 200; = "Default Text"; (txt); // Get the text box contentstring inputText = ;
Common properties:
-
Text
: Text content -
ReadOnly
: Read only -
Multiline
: Whether to enter multiple lines -
PasswordChar
: Characters displayed when password is entered -
MaxLength
: Maximum input length
4. CheckBox (checkbox)
Function: Allow users to select multiple options
Basic Usage:
CheckBox chk = new CheckBox(); = "I agree to the terms"; = new Point(50, 200); += Chk_CheckedChanged; // Binding status change event (chk); // Event handling methodprivate void Chk_CheckedChanged(object sender, EventArgs e) { ("Checkbox Status: " + ); }
Common properties:
-
Checked
: Whether to select -
Text
: The displayed text -
ThreeState
: Whether the third state is supported (not sure)
5. RadioButton (Radio Button)
Functions: Allows the user to select one from a set of options
Basic Usage:
RadioButton radio1 = new RadioButton(); = "Option 1"; = new Point(50, 250); RadioButton radio2 = new RadioButton(); = "Option 2"; = new Point(50, 280); // Put radio buttons into the same container (such as Panel), and they will be mutually exclusive automaticallyPanel panel = new Panel(); = new Point(50, 200); = 100; (radio1); (radio2); (panel);
Common properties:
-
Checked
: Whether to select -
Text
: The displayed text
2. Container controls
1. Panel (panel)
Functions: Container as other controls
Basic Usage:
Panel panel = new Panel(); = new Point(50, 50); = new Size(200, 100); = ; // Add controls to the panelButton btn = new Button(); = "Buttons in the Panel"; = new Point(10, 10); (btn); (panel);
Common properties:
-
BackColor
: Background color -
BorderStyle
:Border style -
Dock
: Docking method
2. GroupBox (group box)
Functions: Group display related controls
Basic Usage:
GroupBox grp = new GroupBox(); = "User Information"; = new Point(50, 50); = new Size(200, 150); // Add control to grouping boxTextBox txtName = new TextBox(); = new Point(10, 20); (txtName); (grp);
Common properties:
-
Text
: Grouped title -
FlatStyle
: Appearance style
3. TabControl (tab control)
Functions: Provide multiple tab pages
Basic Usage:
TabControl tabCtrl = new TabControl(); = new Point(50, 50); = new Size(400, 300); // Add tab pageTabPage tabPage1 = new TabPage("Page 1"); TabPage tabPage2 = new TabPage("Page 2"); // Add controls to the tab pageTextBox txt1 = new TextBox(); = new Point(10, 10); (txt1); TextBox txt2 = new TextBox(); = new Point(10, 10); (txt2); // Add a tab page to a tab control(tabPage1); (tabPage2); (tabCtrl);
3. List and Select Controls
1. ListBox (list box)
Functions: Display the item list, allowing users to select
Basic Usage:
ListBox listBox = new ListBox(); = new Point(50, 50); = new Size(200, 100); // Add a project("Project 1"); ("Project 2"); ("Project 3"); (listBox); // Get the selected itemstring selectedItem = ?.ToString();
Common properties:
-
Items
: Project collection -
SelectedIndex
: Selected index -
SelectedItem
: Selected items -
SelectionMode
: Select mode (single choice/multiple choice)
2. ComboBox (combobox)
Functions: drop-down list, you can select or enter
Basic Usage:
ComboBox comboBox = new ComboBox(); = new Point(50, 50); = new Size(200, 20); // Add a project("Option 1"); ("Option 2"); ("Option 3"); (comboBox); // Get the selected itemstring selectedText = ?.ToString();
Common properties:
-
DropDownStyle
: drop-down style (simple/drop-down list) -
SelectedIndex
: Selected index -
SelectedItem
: Selected item
3. ListView (list view)
Functions: Display project list, support multiple view modes
Basic Usage:
ListView listView = new ListView(); = new Point(50, 50); = new Size(400, 200); = ; // Set view mode // Add column("ID", 50); ("name", 150); ("describe", 200); // Add a projectListViewItem item1 = new ListViewItem("1"); ("Project 1"); ("This is the description of item 1"); (item1); (listView);
Common properties:
-
View
: View mode (large icons/small icons/lists/details) -
Columns
: Column collection -
Items
: Project collection
4. Data input control
1. DateTimePicker (DateTimePicker)
Functions: Select date and time
Basic Usage:
DateTimePicker dtp = new DateTimePicker(); = new Point(50, 50); = ; // Set date format (dtp); // Get the selected dateDateTime selectedDate = ;
Common properties:
-
Value
: Selected date and time -
Format
: Date format (short/long/customized) -
MinDate
: Minimum optional date -
MaxDate
: Maximum optional date
2. NumericUpDown (numeric increase and decrease control)
Functions: Enter or adjust the value
Basic Usage:
NumericUpDown numUpDn = new NumericUpDown(); = new Point(50, 50); = 0; // Minimum value = 100; // Maximum value = 50; // Initial value (numUpDn); // Get the current valueint currentValue = (int);
Common properties:
-
Value
: Current value -
Minimum
: Minimum value -
Maximum
: Maximum value -
Increment
: Increase and decrease step length
V. Dialog boxes and notification controls
1. MessageBox (Message Box)
Functions: Show message or get user confirmation
Basic Usage:
// Show message("This is a message box"); // Show the confirmation dialog boxDialogResult result = ("Are you sure you want to continue?", "confirm", ); if (result == ) { // The user clicked "Yes"}
Common parameters:
-
text
: The message text displayed -
caption
:title -
buttons
: Button type -
icon
: Icon Type
2. OpenFileDialog (Open the file dialog box)
Functions: Let the user select the file
Basic Usage:
OpenFileDialog openFileDialog = new OpenFileDialog(); = "Text file (*.txt)|*.txt|All files (*.*)|*.*"; = "Select File"; if (() == ) { string selectedFile = ; ("Selected file: " + selectedFile); }
Common properties:
-
Filter
: File Type Filter -
Title
: Dialog Box Title -
FileName
: Selected file name -
InitialDirectory
: Initial Directory
3. SaveFileDialog (Save File Dialog)
Functions: Let the user choose the location where the file is saved
Basic Usage:
SaveFileDialog saveFileDialog = new SaveFileDialog(); = "Text file (*.txt)|*.txt|All files (*.*)|*.*"; = "Save File"; if (() == ) { string savePath = ; ("Save to: " + savePath); }
6. Advanced controls
1. DataGridView (Data Grid View)
Functions: Display and edit table data
Basic Usage:
DataGridView dataGridView = new DataGridView(); = new Point(50, 50); = new Size(600, 300); = true; // Automatically generate columns // Bind the data sourceDataTable table = new DataTable(); ("ID", typeof(int)); ("Name", typeof(string)); ("Age", typeof(int)); (1, "Zhang San", 25); (2, "Li Si", 30); = table; (dataGridView);
Common properties:
-
DataSource
: Data source -
AutoGenerateColumns
: Whether to automatically generate columns -
EditMode
: Editing mode
2. Timer (timer)
Functions: Perform timing tasks
Basic Usage:
Timer timer = new Timer(); = 1000; // 1 second interval += Timer_Tick; // Bind timed event (); // Start the timer // Timed event handling methodprivate void Timer_Tick(object sender, EventArgs e) { ("Timer Trigger"); }
Common properties:
-
Interval
: Interval time (milliseconds) -
Enabled
: Whether to enable
3. WebBrowser (web browser control)
Functions: Embed a web browser in the application
Basic Usage:
WebBrowser webBrowser = new WebBrowser(); = new Point(50, 50); = new Size(800, 600); // Navigate to URL(""); (webBrowser);
Common properties:
-
Url
: Current URL -
DocumentText
: HTML document content -
Document
: Document object model
7. Control layout skills
1. Use Dock properties
Panel panel = new Panel(); = ; // Dock at the top = 50; = ; (panel);
2. Use the Anchor property
Button btn = new Button(); = "Button"; = new Point(50, 50); = | ; // Anchored in the lower right corner (btn);
3. Use TableLayoutPanel
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel(); = ; = 2; = 2; // Add controls to table layoutButton btn1 = new Button(); = "Button 1"; (btn1, 0, 0); Button btn2 = new Button(); = "Button 2"; (btn2, 1, 0); (tableLayoutPanel);
8. Control event handling
1. Common events
-
Click
: Click event -
TextChanged
: Text change event -
SelectedIndexChanged
: Selected to change event -
ValueChanged
: Value change event -
Load
: Loading event
2. Event binding
Button btn = new Button(); = "Click Me"; += (sender, e) => { ("Button was clicked"); }; (btn);
9. Control style and appearance
1. Set the font
Label lbl = new Label(); = "Custom Font"; = new Font("Microsoft Yahe", 12, );
2. Set the color
Button btn = new Button(); = "Colorful Buttons"; = ; = ;
3. Set borders
Panel panel = new Panel(); = ; // Solid line border
- Use meaningful control names: Easy to maintain code
- Reasonable use of layout controls: For example, TableLayoutPanel, FlowLayoutPanel
- Event processing separation: Put event processing logic in a separate method
- Data binding: Try to use data binding instead of manually operating the controls
- Exception handling: Verify user input and handle exceptions
The above is the detailed content of the detailed usage tutorial for the main controls in WinForms. For more information on the use of WinForms controls, please pay attention to my other related articles!