早上好,如何讓我的用戶控制元件只能添加到某個型別的控制元件(splitcontainer)中?(我試過谷歌搜索,但找不到任何關于我需要的東西)
謝謝您的幫助..
uj5u.com熱心網友回復:
您需要注意以下幾點:
- 控制元件的工具箱項行為
- 控制元件的設計器行為
- 控制元件的運行時運行時行為(可選)
當您將控制元件從設計圖面上的工具箱中洗掉時,您應該防止將其放置在不需要的控制元件上;它是分配給控制元件的ToolBoxItem類的CreateComponents方法的責任。
在控制元件添加到設計圖面后(例如在所需的父級中),您需要防止將其拖到不需要的控制元件上;它是分配給控制元件的ControlDesigner類的CanBeParentedTo方法的責任。
最后,您甚至可能希望防止在運行時以編程方式將其添加到不需要的控制元件中;在這種情況下,它是控制元件的OnParentChanged方法的責任。
示例 - 將控制元件限制為僅托管在 SplitterContainer 中
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
[ToolboxItem(typeof(MyControlToolBoxItem))]
[Designer(typeof(MyControlDesigner))]
public class MyControl : Control
{
protected override void OnParentChanged(EventArgs e)
{
if (Parent != null && !(Parent is SplitterPanel))
throw new Exception("You can add this control only to a SplitterContainer.");
base.OnParentChanged(e);
}
}
public class MyControlDesigner : ControlDesigner
{
public override bool CanBeParentedTo(IDesigner parentDesigner)
{
if (parentDesigner.Component is SplitterPanel)
return true;
else
return false;
}
}
public class MyControlToolBoxItem : ToolboxItem
{
protected override IComponent[] CreateComponentsCore(IDesignerHost host,
System.Collections.IDictionary defaultValues)
{
if (defaultValues.Contains("Parent"))
{
var parent = defaultValues["Parent"];
if (parent != null && parent is SplitterPanel)
return base.CreateComponentsCore(host, defaultValues);
}
var svc = (IUIService)host.GetService(typeof(IUIService));
if (svc != null)
svc.ShowError("You can drop this control only over a SplitterContainer.");
return null;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/391698.html
上一篇:接收json作為字串始終為空
