Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop C # composite control

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to develop C# composite controls". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

When we do Windows Forms development under the .NET platform, we often need to combine existing controls for a special purpose. For example, controls that combine Label and TextBox are very easy to lay out on the form (remember that there is such a control in Delphi 6), while controls that combine specific patterns and text are very suitable for displaying the company's logo. The .NET platform provides us with great tools and techniques to create such custom controls. Let's create a control that automatically displays time.

It is worth noting that the .NET platform provides us with three control development technologies, namely: inherited control (Extended Control), composite control (Composite Control) and custom control (Custom Control). What we are concerned about now is the second kind-- C# composite control development technology.

1. Create a control project.

Create a new "Windows Control Library" project in Visual C # .NET, named "TimeLabel"

2. Change the namespace and control class name.

The default namespace is TimeLabel (like the project name), and the control class name is UserControl1. Please change the namespace to be consistent with your development habits, such as mine is LeoYang.Controls.TimeLabel, of course, you can also choose to keep the default namespace; * * change the control class name to a meaningful name, such as TimeLabel, so that when the control appears on the toolbox, it will be displayed as TimeLabel instead of UserControl1. Note that once the class name is changed, the corresponding Construction method name should be changed accordingly, such as:

Public UserControl1 () {… } / / will be changed to: public TimeLabel () {… }

3. Add an existing control.

Since we are creating new controls based on existing controls, we should first add available existing controls to the control design interface. Please double-click Label in the toolbox, and then double-click Timer, and the two existing controls are added to our design interface one by one

4. Publish the control properties.

Because we want to write the time to the Text property of Label, we need to expose the label1.Text attribute. Add the following code to the TimeLabel class:

Public string LabelText {get {return label1.Text;}}

Note that we don't need to let the control consumer change the value of label1.Text here, so LabelText is a read-only property.

In addition, for aesthetic reasons, we also expose a LabelBackColor property to the control user, which is used to obtain and set the BackColor property of Label. The code is as follows:

Public Color LabelBackColor {get {return label1.BackColor;} set {label1.BackColor=value;}}

Of course, you can also add some properties as needed, such as font, control size, etc., so that control users can use the control more flexibly.

At this point, I need to add that a very important thing in the creation of a C# composite control is that any properties that make up a control (Constituent Control) must be exposed by adding properties of the composite control, rather than directly exposing the component control at the public level. For example, we should not directly expose the Label control by setting the access level to public (the default is private). The purpose of this is to give us a better grasp of the data security of the control, so that only the properties that are most needed are exposed to the control users.

This is the end of the content of "how to develop C# composite controls". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report