简体   繁体   English

C#中的复合Winform控件

[英]Composite Winform Control in C#

Composite Control 复合控制

Image http://www.freeimagehosting.net/uploads/a51136603b.png 图片http://www.freeimagehosting.net/uploads/a51136603b.png

Howdy, 你好,

I am hoping for some help. 我希望得到一些帮助。 I wanted to create a Composite Control, shown in the screenshot above. 我想创建一个复合控件,如上面的截图所示。 As seen in the screen shot, the Parent Control is Label 1; 如屏幕截图所示,父控件是标签1; it contains child controls Label2, Label3, and Label4, and a PictureBox containing a pre-defined image. 它包含子控件Label2,Label3和Label4,以及包含预定义图像的PictureBox。

I would like to turn the above into a composite control, in C#, with the following specifications : 我想将上述内容转换为C#中的复合控件,具有以下规范:

The Parent Label, Label1, should generate a Click Event. 父标签Label1应生成Click事件。 Child controls Label2, Label3, and Label4 are to be assigned ToolTips on MouseMove Event, while the last Child Control, PictureBox, should have both ToolTips on MouseMove Event and Click Event. 子项控件Label2,Label3和Label4将在MouseMove事件上分配工具提示,而最后一个子控件PictureBox应在MouseMove事件和Click事件上同时具有工具提示。

Likewise, the locations of Label2, Label3, Label4, and PictureBox within the Parent Label1 control must be set as properties, as are their texts and sizes. 同样,必须将Parent Label1控件中Label2,Label3,Label4和PictureBox的位置设置为属性,文本和大小也必须设置为属性。

How do I go about incorporating all these into one composite control named AilmentLabel ? 如何将所有这些合并到一个名为AilmentLabel的复合控件中? Any advise or information is greatly appreciated. 任何建议或信息都非常感谢。 Thank you so much. 非常感谢。

MSDN provides a very thorough walkthrough of how to do this. MSDN提供了如何执行此操作的非常详尽的演练。 You should read the walkthrough: "Authoring a Composite Control with C#" 您应该阅读演练: “使用C#编写复合控件”

It sounds like you want to create a 'UserControl'. 听起来你想要创建一个'UserControl'。 It's just like any other control but you can add controls to the surface. 它就像任何其他控件一样,但您可以向表面添加控件。 Then you would expose properties in the UserControl source that would propagate the changes thru to the appropriate control: 然后,您将公开UserControl源中的属性,这些属性会将更改传播到适当的控件:

public string Label1Text {
    get { return this.Label1.Text; }
    set { this.Label1.Text = value; }
}
...etc...

You can even expose Click events in this manner, and allow users to subscribe to them. 您甚至可以以这种方式公开Click事件,并允许用户订阅它们。 Then you can drag/drop this new control onto your other window surfaces. 然后,您可以将此新控件拖放到其他窗口表面上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM