如何在c#中为用户控件设置图像 [英] how to set image for user control in c#

查看:86
本文介绍了如何在c#中为用户控件设置图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


i制作了一个用户控件,但是当我在c#中将它添加到ToolBox时,它没有像toolBox中的其他项目那样的图片,我该怎么办呢?

With Respect太空人

Hi i made a user control but when i add it to ToolBox in c# it doesn't have picture like other items in toolBox, how can i do it?
With Respect "Spaceman"

推荐答案

轻松! (当你知道如何......:笑:)。

只需设置ToolboxBitmap属性:

Easy! (When you know how... :laugh: )
Just set the ToolboxBitmap property:
[System.Drawing.ToolboxBitmap(@"C:\Projects\Controls\SQLControl.bmp")]
partial class ctrlSQL : UserControl
    {





注意:它必须是16x16 bmp: http:// msdn .microsoft.com / zh-cn / library / 4wk1wc0a.aspx [ ^ ]


解决方案1存在我在评论中解释的问题。更好的解决方案将是本页示例中显示的第三个解决方案:



http://msdn.microsoft.com/en-us/library/4wk1wc0a.aspx [ ^ ]。



请参阅: http://msdn.microsoft.com/en-us/library/system.drawing.toolboxbitmapattribute.aspx [ ^ ]。



应将16x16位图放入应用程序资源中。第一个例子(使用来自.NET FCL的控件类型)将起作用,但它没有什么实际意义。

因此,第三个例子说明了唯一具有实际意义的用法: http://msdn.microsoft.com/en-us/library/eth0sx9d.aspx [ ^ ]。



如果你使用这种方式,所讨论的位图总是带有定义所讨论控件的程序集,无论它如何部署;它可以使用应用程序构建或部署在IL代码中;解决方案的完整性无法破解。



真正的问题是MSDN对此功能的描述非常糟糕且有些误导,尤其是上面首先引用的页面。 br $>


-SA
The Solution 1 has the problem I explained in my comment to it. Better solution would be the 3rd one shown in the example on this page:

http://msdn.microsoft.com/en-us/library/4wk1wc0a.aspx[^].

Please see: http://msdn.microsoft.com/en-us/library/system.drawing.toolboxbitmapattribute.aspx[^].

The 16x16 bitmap should be put in the application resource. The first example (use of a control type from .NET FCL) will work, but it makes little practical sense.
So, the third example illustrate the only practically significant usage: http://msdn.microsoft.com/en-us/library/eth0sx9d.aspx[^].

If you use this way, the bitmap in question always come with the assembly defining the control in question, no matter how it is deployed; it could be built with the application or deployed in IL code; integrity of solution cannot be broken.

The real problem is that the MSDN description of this feature is really bad and somewhat misleading, especially the page referenced first, above.

—SA


这篇关于如何在c#中为用户控件设置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆