且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在组合框项目中添加按钮控件?

更新时间:2023-11-28 18:30:04

正如您所问的v.2.0。 ComboBox可能意味着 System.Windows.Forms.ComboBox 。不,此控件不适用于保存其他控件。其列表框部分的项目可以是任何类型,但在UI中呈现的只是以 ToString 获得的字符串。因此,即使您将按钮实例添加到组合框中,它也不会显示为按钮,因此您无法执行所需的操作。



我不喜欢认为你想要实现的是一个好的设计理念。但是,如果从头开始创建自定义控件,则可以实现此类外观和行为。其中一种可能性是:您的控件列表框部分可能来自 ScrollableControl ;通过这种方式,您可以更轻松地实现列表框元素的滚动。



-SA
As you are asking about v.2.0. "ComboBox" probably means System.Windows.Forms.ComboBox. No, this control is not designed to hold other controls. The items of its list box part can be of any type, but presented in UI is just the string obtained as ToString. Therefore, even if you add button instance to the combo box, it won't be shown as a button, so you cannot do what you want.

I don't think what you want to achieve is a good design idea. However, you can achieve such look and behavior if you create a custom control from scratch. One of the possibility is this: your "list box-like" part of your control could be derived from ScrollableControl; this way, you could easier implement the scrolling of the elements of the "list box".

—SA


现在我知道你想要的是一个ComboBox项目中的图形元素:你可以对ComboBox进行子类化并使用自定义绘画(GDI +)来绘制图像,如下所示:[ ^ ]



单独使用图形元素进行点击检测需要一些工作。



除非你对WinForms非常先进并且创建了sub -classed控件来自标准MS提供的控件,我不推荐这个。
Now that I know that what you want is a graphic element inside a ComboBox Item: you can sub-class the ComboBox and use custom painting (GDI+) to draw an image as illustrated here: [^]

Hit detection on the graphic element alone would require some work.

Unless you are quite advanced with WinForms and creating sub-classed Controls from standard MS provided Controls, I wouldn't recommend this.