在用户控件中访问组件属性 [英] Accessing component properties in user control

查看:84
本文介绍了在用户控件中访问组件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中编写了一个非常简单的用户控件。它看起来像一个ComboBox,当你点击下拉菜单时,它会激活一个OpenFileDialog或FolderBrowserDialog。当用户选择文件或文件夹时,其路径显示在我的控件的文本部分。

控件只有一个停靠在右侧的按钮和一个文本框停靠以填充控件的其余部分。

它有一个ImageList,里面有几个与Button关联的图像。通常当你在IDE中选择一个图像时,你会在属性窗口中看到一个下拉列表,并且可以看到图像,这样可以很容易地选择一个。

我想在我的用户控件中提供下拉菜单如果可能的话。我可以暴露Button的所有属性,但是找不到一种方法来公开ImageIndex并提供漂亮的下拉菜单。

有人能告诉我如何实现这个目标吗?



我的尝试:



我调查使用TypeConvertor属性中的编辑,但每次我认为我越来越接近我发现我不是!

I have written a very simple user control in C#. It looks like a ComboBox and when you click the drop down it fires up either an OpenFileDialog or FolderBrowserDialog. When the user selects a file or folder its path is shown in the text portion of my control.
The control just has a button docked to the right and a text box docked to fill the rest of the control.
It has an ImageList with several images associated with the Button. Normally when you choose an image in the IDE you get a drop down in the Properties Window and can see the images which makes it easy to choose one.
I would like that drop down available in my user control if possible. I can expose all of the properties of the Button but can't find a way to expose just the ImageIndex with the nice drop down.
Can anybody give me a steer on how to achieve this please?

What I have tried:

I have investigate using "TypeConvertor" and "Editor" in the properties but every time I think I am getting close I discover I'm not!

推荐答案

好的,解决了!

首先,用户控件必须有自己的ImageList属性,所以我只添加了一个'get'访问器。然后该属性变为:



OK, solved!
Firstly the User Control must have its own ImageList property so I added that with a 'get' accessor only. Then the property became:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true), Description("The Image Index for the button"), DefaultValue(0)]
[TypeConverter(typeof(ImageIndexConverter))]
[Editor("System.Windows.Forms.Design.ImageIndexEditor", typeof(UITypeEditor))]
[RelatedImageList("ImageList")]
public int ImageIndex
{
	get { return btnPick.ImageIndex; }
	set { btnPick.ImageIndex = value; }
}





QED!



QED!


谢谢Gerry。我意识到,只要按下按钮,我就会错过一些重要信息:-(

我有一个基本控件,看起来像一个ComboBox,当然我可以访问其中的所有属性。它包含一个关联的ImageList。

然后我有一个从该基本控件派生的文件/文件夹选择器,并希望使用Button本身使用的相同编辑器访问派生控件中Button的ImageIndex。我取得了一些进展:



Thanks Gerry. I realised as soon as I pressed the button I had missed some important information :-(
I have a base control, looks like a ComboBox and of course I can access all the properties in that. The Button it contains has an associated ImageList.
I then have a File/Folder picker derived from that base control and want to access the ImageIndex of the Button in the derived control using the same editor that Button itself uses. I have made some progress:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[Browsable(true), Description("The Image Index for the button"), DefaultValue(0)]
		[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.ImageIndexConverter))]		
		public int ImageIndex
		{
			get { return btnPick.ImageIndex; }
			set { btnPick.ImageIndex = value; }
		}





这确实在IDE属性窗口中提供了一个下拉选择器,但它确实如此空!

我想我需要一个[Editor ...]属性,但不知道哪一个是合适的。



This does provide a drop down picker in the IDE properties window but it's empty!
I think I need an attribute but don't know which one is appropriate.


这篇关于在用户控件中访问组件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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