Building custom controls windows forms




















The System. Forms namespace contains a large family of controls that add both form and function to a Windows-based user interface. Each control inherits a common set of members from the Control class. To these, it adds the methods, properties, and events that give the control its own distinctive behavior and appearance. Figure Windows Forms control hierarchy.

Figure shows the inheritance hierarchy of the Windows Forms controls. NET 2. NET 1. Table provides a summary of the more frequently used controls in this hierarchy. Represents a button on a form. Consists of a check box with text or an image beside it. The check box can also be represented as a button by setting:. Hybrid control that consists of a textbox and a drop-down list.

It combines properties from both the TextBox and the ListBox. The DataGridView is the foremost control to represent relational data. It supports binding to a database. The DataGridView was introduced in. Container control that holds a collection of images used by other controls such as the ToolStrip , ListView , and TreeView. May contain simple text or objects.

Its methods, properties, and events allow items to be selected, modified, added, and sorted. May take a grid format where each row represents a different item and subitems. It also permits items to be displayed as icons. Used to hold images in a variety of standard formats. Displays the familiar progress bar that gives a user feedback regarding the progress of some event such as file copying. Provides a status bar that is used to provide contextual status information about current form activities.

Can be designed to accept single- or multi-line input. Properties allow it to mask input for passwords, scroll, set letter casing automatically, and limit contents to read-only. This chapter lacks the space to provide a detailed look at each control. Instead, it takes a selective approach that attempts to provide a flavor of the controls and features that most benefit the GUI developer. Notable omissions are the DataGridView control, which is included in the discussion of data binding in Chapter 12, "Data Binding with Windows Forms Controls," and the menu controls that were discussed in Chapter 6, "Building Windows Forms Applications.

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time. Pearson Education, Inc. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. Privacy policy. Windows Forms controls are reusable components that encapsulate user interface functionality and are used in client-side, Windows-based applications.

Not only does Windows Forms provide many ready-to-use controls, it also provides the infrastructure for developing your own controls.

You can combine existing controls, extend existing controls, or author your own custom controls. For more information, see Types of custom controls. The Desktop Guide documentation for. NET 6 and. NET 5 including. NET Core 3. On mouse enter t the button you get the highlighted effect with Hand cursor. This post covered the steps to create custom controls in C WinForms. The code logic is same for Creating Custom controls in VB. NET also. Hope this article was helpful for you.

If you have any queries or feedback to share about Creating Custom controls in. For an extended button that tracks clicks, you can override the OnPaint method to call the base implementation of OnPaint , and then draw the click count in one corner of the Button control's client area.

Another way to create a control is to create one substantially from the beginning by inheriting from Control. The Control class provides all of the basic functionality required by controls, including mouse and keyboard handling events, but no control-specific functionality or graphical interface. Creating a control by inheriting from the Control class requires much more thought and effort than inheriting from UserControl or an existing Windows Forms control.

Because a great deal of implementation is left for you, your control can have greater flexibility than a composite or extended control, and you can tailor your control to suit your exact needs. To implement a custom control, you must write code for the OnPaint event of the control, as well as any feature-specific code you need. You can also override the WndProc method and handle windows messages directly. An example of a custom control is a clock control that duplicates the appearance and behavior of an analog clock.

Custom painting is invoked to cause the hands of the clock to move in response to Tick events from an internal Timer component.

Although the Windows Forms infrastructure has been optimized to host Windows Forms controls, you can still use ActiveX controls. There's support for this task in Visual Studio. Windowless controls aren't supported in Windows Forms. If you need to implement a custom design-time experience, you can author your own designer. For composite controls, derive your custom designer class from the ParentControlDesigner or the DocumentDesigner classes.

For extended and custom controls, derive your custom designer class from the ControlDesigner class. Use the DesignerAttribute to associate your control with your designer.



0コメント

  • 1000 / 1000