In C#,Control
Classes are the base class for all controls in Windows Forms applications. It provides the basic functions and properties of the control, all inherited fromControl
shared by subclasses of the class.
This meansControl
Classes are the basis for building user interface elements in Windows Forms applications.
Here are some key features and methods of the Control class
property
-
Size
: Gets or sets the width and height of the control. -
Location
: Gets or sets the position of the control in its container. -
Visible
: Get or set whether the control is visible. -
Enabled
: Get or set whether the control is enabled. -
Text
: Get or set the text of the control. -
BackColor
: Get or set the background color of the control. -
ForeColor
: Gets or sets the foreground color of the control, usually the text color. -
Font
: Get or set the font of the control. -
Parent
: Get or set the container for the control, usually another control or form.
method
-
Invalidate()
: Invalidate the entire surface of the control, triggering redraw. -
Update()
: Repaint the control immediately, usually invokingInvalidate()
Use later. -
Refresh()
: Repaint the control immediately, frequent use is not recommended as it can cause performance issues. -
Hide()
: Hide the control, but does not release the resource. -
Show()
: Show controls. -
Focus()
: Set the keyboard input focus on the control. -
Select()
: Select the control if it is a control that can be selected, such as a text box.
event
-
Paint
: Fired when the control needs to be redrawn. -
Click
: Triggered when the user clicks on the control. -
MouseEnter
andMouseLeave
: Triggered when the mouse enters or leaves the control. -
MouseMove
: Triggered when the mouse moves on the control. -
KeyDown
、KeyUp
andKeyPress
: Events related to keyboard input.
Constructor
-
Control()
:Control
The constructor of a class, usually used when creating a custom control.
inherit
Many commonly used controls, such asButton
、TextBox
、Label
Wait, all fromControl
Class inherited.
Here's how to use it in your codeControl
A simple example of a class:
- Example sentence:
public class MyForm : Form { private Button myButton;// Used to store references to button controls public MyForm() { myButton = new Button(); = "Click Me";//Set the text displayed on the button to "Click Me" = new Point(100, 100);//Set the position of the button on the form. Point(100, 100) means that the upper left corner of the button will be located at the (100, 100) position of the form coordinates. += MyButton_Click;//Added an event handler for the button's Click event. When the button is clicked, the MyButton_Click method is called. (myButton);//Add myButton to the form's Controls collection. The Controls collection is a container for all controls on the form. By adding the button to this collection, the button will be displayed on the form. } private void MyButton_Click(object sender, EventArgs e)//sender represents the object that triggers the event, and e represents the EventArgs object that contains event data. { ("You clicked the button!");//Feedback when the user clicks the button, feedback a message box } }
The difference between three redrawing methods: Invalidate, Update, and Refresh
Invalidate() method
- Example sentence:
(); //Repaint the entire control or form(true); // Repaint and erase the background(rect); // Only redraw the specified rectangle area
-
Invalidate()
Methods are used to notify the Windows Forms application that the specified control area needs to be redrawn. - This method will cause the control
OnPaint
The event is triggered, thus calling the correspondingPaint
Event handler to redraw the control. -
Invalidate()
The method can be optionalbool
Parameters, when set totrue
When , it causes the background to be erased, which may cause flickering.
Update() method
- Example sentence:
(); ();
-
Update()
Methods are usually calledInvalidate()
After using the method, it will force immediate processingWM_PAINT
message, not waiting for other messages in the message queue. - This ensures that the control is redrawn immediately, rather than waiting until other messages in the message queue are processed.
-
Update()
Methods are often used to ensure that the control is repainted immediately after a series of updates, such as in an animation or continuous update scene.
Refresh() method
- Example sentence:
();
-
Refresh()
The method is the easiest and most straightforward method of redrawing, which will immediately redraw the control or form. - and
Invalidate()
andUpdate()
different,Refresh()
The method does not depend onOnPaint
Event, it calls directlyOnPaint
method, so it won't triggerPaint
event. -
Refresh()
Methods are often used for scenes that require immediate update of display, but it can cause performance issues as it does not provide an opportunity to optimize redraw.
Summary of the difference
-
Invalidate()
is the most commonly used method of redrawing, which provides flexibility to allow controls to redraw when appropriate, and to control whether the background is erased through parameters. (Partial repaint, reduce the number of refreshes) -
Update()
Usually withInvalidate()
Use together to ensure that the redraw is processed immediately, rather than waiting for other messages. (Workspace repaint, global repaint, higher than Invalidate refresh times) -
Refresh()
The fastest way to repaint is provided, but it can cause performance issues as it does not provide opportunities for optimization. (Workspace redraw, repaint subcontrol, forced, more refresh times than Update)
The above is personal experience. I hope you can give you a reference and I hope you can support me more.