C# Control Programming Detailed Button Control

Abstract: The button control explained in the control programming series, and the use of the button control is explained in detail based on my own learning experience.

Programming language: C#

Programming environment: Visual Studio 2019

Table of contents

Detailed explanation of button control properties (members)

layout:

design

Exterior

Behavior

Detailed explanation of button control events

summary

each message


Detailed explanation of button control properties (members)

layout:

  • Autosize: bool type, indicating whether the size of the control is automatically adjusted according to the content (button display text), initially false.
  • Dock: DockStyle enumeration type, which defines the border of the control bound to the container control. The enumeration values ​​are None (not bound), Top (top of the container), Bottom (bottom of the container), Left (left side of the container), Right (container Right), Fill (fill the entire container), initially None.
  • Location: Point type, which defines the coordinates of the upper left corner of the control relative to the upper left corner of its container, initially (0, 0). (If created by toolbox dragging, the initial coordinates are the upper left corner of the placed control)
  • Size: Size type, defines the size (width, height) of the control, the initial value is (75, 23).

design

  • Name: string type, which defines the member variable name of the control, initially "button1".
  • GenerateMember: bool type, indicating whether to generate the member variable of the control, initially ture.
  • Locked: bool type, indicating whether to lock the coordinates and dimensions of the control during design, initially false. (Note: Setting this to true will prevent the control from being dragged, but the coordinates and dimensions of the control can still be modified by writing code)
  • Modifiers: Enumeration type, which defines the accessibility level of the control. The enumeration values ​​are Private (accessible to the same type), Public (public access), Protected (accessible to the same type and derived classes), Internal (accessible to the same assembly), Protected Internal (accessible to the same assembly and derived assemblies), initially Private. (Note: To access this control in other classes, you need to modify this property, which cannot be modified by writing code)

Exterior

  • BackColor: Color type, which defines the background color of the control, initially the system color Control.
  • BackgroundImage: Image type, which defines the background image of the control and is initially empty.
  • BackgroundImageLayout: ImageLayout enumeration type, which defines the layout of the background image of the control. The enumeration values ​​include None (left display), Tile (repeated display), Center (centered display), Stretch (stretched to fill the display), Zoom (press scaling display), the initial is Tile. (Note: If the RightToLeft property is Yes, then None means display on the right)
  • Cursor: Cursor type, which defines the cursor displayed when the mouse moves to the control, initially Default.
  • FlatStyle: FlatStyle enumeration type, defines the appearance of the control when the mouse clicks the control, the initial is Standard.
  • Font: Font type, which defines the font and font size of the text displayed by the control. The initial is Arial, 9pt.
  • ForeColor: Color type, defines the color of the text displayed by the control, initially the system color ControlText.
  • Image: Image type, which defines the image displayed by the control and is initially empty. (Note: Image is the relationship between the top layer and the bottom layer of BackgroundImage)
  • ImageAlign: ContentAlignment enumeration type, defines the alignment of the control display image, initially MiddleCenter (horizontal, vertical center).
  • RightToLeft: RightToLeft enumeration type, which defines whether the control is drawn from right to left. The enumeration values ​​include Yes (draw from right to left), No (draw from left to right), Inherit (inherit the current, usually draw from left to right) ), initially Inherit.
  • Text: string type, which defines the text displayed by the control, initially "button1".
  • TextAlign: ContentAlignment enumeration type, defines the alignment of the control display text, initially MiddleCenter (horizontal, vertical center).
  • TextImageRelation: TextImageRelation enumeration type, which defines the relative position of the image and text on the control. The initial value is Overlay (the text is placed on top of the image), and other enumeration values ​​are understood literally.

Behavior

  • ContextMenuStrip: Define the shortcut menu displayed when the user right-clicks the control, initially empty. (Note: To create this menu, you need to write code to create or drag in the ContextMenuStrip control and then select it here, see my other article for details) Usage of ContextMenuStrip (right-click menu) in C#Windows Form Design _C# is actually not difficult blog - CSDN Blog
  • DialogResult: DialogResult enumeration type, which defines the dialog box result generated in the modal form when the button is clicked, and is initially None. (Note: This property is only valid when the form to which it belongs is a modal form (the form displayed by the ShowDialog() method), and it is used when the dialog box is required to provide a function similar to the return value)
  • Enabled: bool type, indicating whether to enable the control, initially true. (Note: set this property to false, the button will be grayed out and not clickable)
  • Visible: bool type, indicating whether to display the control, initially true, and false to hide the control.

Detailed explanation of button control events

  • Click: Mouse click event, which is the most commonly used event for button controls, occurs when the button is clicked. (Double click the button in the design form to directly register the event and automatically go to the method body of the event)

summary

        The button control is the most used and relatively simple control in C# control programming. The main function is completed through the Click event. For each control, you can learn by continuously understanding the usage of attributes and events. It is recommended to try each attribute and event. This is a cumulative process.

each message

        What is achieved on paper is always shallow, and I know that this matter must be done.

Guess you like

Origin blog.csdn.net/lucgh/article/details/130051203