使用 C# 组合框放置图像和字符串
我需要为 Windows 窗体应用程序创建一个下拉菜单或组合框,其中包含一个小图像,然后是它旁边的一串文本。基本上,您可以将下拉列表中的每个“行”视为需要有一个图标,然后在图标右侧显示图标的名称。我在做这件事时遇到了困难——事实上,我完全没有成功。有谁知道完成这项任务的方法?任何帮助将不胜感激。谢谢!
I need to create a dropdown menu, or combobox, for a Windows Forms application which contains a small image and then a string of text next to it. Basically, you can think of each 'row' in the dropdown as needing to have an icon and then the name of the icon to the right of the icon. I am having trouble doing this -- in fact, I've been completely unsuccessful. Does anyone know of a way to accomplish this task? Any help will be greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我能够想出一些非常简单的代码来执行此操作(请参阅下面的代码片段)。该代码创建一个下拉控件,它在同一行中显示一个小彩色方块和该颜色的名称(参见照片)。感谢您在最初发布时提供的链接!希望这个控件将来可以帮助其他人。
图片:
代码:
I was able to come up with some very simple code to perform this (see snippet below). The code creates a control that is a dropdown control which shows a small colored square and that color's name in the same row (see photo). Thanks for the links provided for this back when it was originally posted! Hopefully this control can help someone else out in the future.
Image:
Code:
非常有帮助..
一些优化:
和...
Very helpful..
some optimisations :
and ...
我解决了问题,我这样做了:
I resolved the problem, i did this:
注意:此代码来自用户 que dal 的优化
如果您希望拥有一个不仅仅是颜色名称的字符串,请将 DropDownItem 更改为具有 2 个参数:字符串和颜色,然后只需更改画笔设置颜色的方式,如下所示:
然后您必须更改下拉列表项目如下:
希望这有帮助:)
NOTE: This code is from user que dal's optimization
If you wish to have a string that isn't just the name of the color, change DropDownItem to have 2 arguments, the string and the color, then just change how the brush sets the color, as such:
You must then change the dropdown item as such:
Hope this was helpful :)
不确定图像,但这应该适用于字符串:
Not sure about images but this should work for the strings: