具有Checklistbox的usercontrol的属性窗口中的自定义属性。 [英] Custom property in property window for usercontrol with Checklistbox.
问题描述
你好,
我已经建立了一个用户控件,我希望在其属性窗口中有一个自定义属性。
有两项要求需要填写:
1.财产应显示 检查清单框 以便用户可以在设计时选择所需的值。
2.此属性的值应该来自 - 表单上有多少类似的用户控件。
例如:如果我在表单上插入一个usercontrol(即 usercontrol_1 ),然后我在同一表格上再添加两个类似用户控件的实例(即 usercontrol_2 & usercontrol_3 )。
现在当我设置usercontrol1的自定义属性时,它应该有一个下拉列表框,其值为usercontrol_2& usercontrol_3。简而言之,我想将一个用户控件与另一个类似的用户控件链接起来。
注意:
一旦我在表单上添加新的usercontrol,属性的checklistbox就会自动填充。
如果有人有解决方案请帮忙。
谢谢&问候,
Siddharth
Hello,
I have made a user control for which i want to have a custom property in its "Properties" window.
There are two requirements to be fullfilled :
1. Property should show a check list box so that user can select the desired values at the design time.
2. The values for this property should come from - how many similar user controls are present on the form.
for example : If i insert one usercontrol on the form (i.e usercontrol_1), then I add two more instance of similar usercontrols(i.e usercontrol_2 & usercontrol_3) on the same form.
Now when I set the custom property of usercontrol1, it should have a dropdown checklistbox with the values usercontrol_2 & usercontrol_3. So in short, I want to link one user control with another similar usercontrol.
Note:
the checklistbox of the property should automatically populate as soon as I add new usercontrol on the form.
Please help if someone have solution for this.
Thanks & regards,
Siddharth
推荐答案
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Windows.Forms.Design;
namespace WindowsFormsApplication12
{
public partial class UserList : UserControl //Made one usercontrol name userlist.
{
public UserList()
{
InitializeComponent();
}
Control ParentForm; // declared one control
List<Control> objControlList = new List<Control>();
private void UserList_Load(object sender, EventArgs e)
{
ParentForm = this.Parent; // Parent form equals to the form or container on which usercontrol is placed.
GetAllControls(ParentForm); // Method declared below: Gives list of all 'userlist' usercontrol present on the Parentform
UserControlEditor.PropertyCopyControlList = objControlList;
label1.Text = Convert.ToString(UserControlEditor.PropertyCopyControlList.Count);
}
private void GetAllControls(Control container) //Gives list of all 'userlist' usercontrol present on the Parentform
{
foreach (Control c in container.Controls)
{
GetAllControls(c);
if (c is UserList) objControlList.Add(c);
}
}
bool[] objbool = new bool[5];
[Description("Items in the lisbox"), Editor(typeof(UserControlEditor), typeof(UITypeEditor))]
public bool[] NoOfUserlistsOnForm // Made a custom property to have a checklist box in Properties window to set value for this property .
{
get
{
return objbool;
}
set
{
objbool = value;
{
for (int i = 0; i < (objControlList.Count); i++)
{
if (objbool[i] != false)
{
listBox1.Items.Add(objControlList[i].Name);
}
}
}
}
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
class UserControlEditor : UITypeEditor // Declared a class to have a checklist box in properties window of usercontrol
{
public UserControlEditor()
{
}
public static List<Control> CopyControlList = new List<Control>();
public static List<Control> PropertyCopyControlList
{
get { return CopyControlList; }
set { CopyControlList = value; }
}
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
{
//use IWindowsFormsEditorService object to display a control in the dropdown area
IWindowsFormsEditorService frmsvr = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (frmsvr == null)
return null;
CheckedListBox Clbox = new CheckedListBox();
//Here I have to populate the checklistbox with the code - Clbox.Items.Add(objectName)
// But how get the ControlList (which is declared in the userlist class) to this class(UserControlEditor).
for (int i = 0; i < PropertyCopyControlList.Count; i++)
{
Clbox.Items.Add(PropertyCopyControlList[i].Name);
}
var es = provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService)) as System.Windows.Forms.Design.IWindowsFormsEditorService;
es.DropDownControl(Clbox);
if (es != null)
{
var data = value as bool[];
if (data != null)
{
for (int i = 0; i < Math.Min(data.Length, Clbox.Items.Count); i++)
{
Clbox.SetItemChecked(i, data[i]);
}
}
es.DropDownControl(Clbox);
}
var result = new bool[5];
for (int i = 0; i < Clbox.Items.Count; i++)
{
result[i] = Clbox.GetItemChecked(i);
}
return result;
}
}
}
这篇关于具有Checklistbox的usercontrol的属性窗口中的自定义属性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!