類型屬性欄中以byte類型顯示和修改,但是又可以通過點擊類型的詳細信息設置每一個bit的值,最後設置到byte上。
如下圖:byteEditor是byte類型,但是設置時可以通過協議編輯器以bit進行設置。每一bit對應一個bool類型的數據,此處是枚舉。
byteEditor對應的類型繼承了實現形式System.ComponentModel.Design.ArrayEditor類。其他類型的XXXEditor也是類似的。
實現方式是通過完全實現XXXEditor的virtual方法,通過查看每一個方法的執行順序,修改其內部實現。
本文代碼未進行優化。
設置前:
設置時:成員 名稱也是可以設置的
代碼:
byte設置
[DisplayName("byteEditor")]
[Browsable(true)]
[Description("byteEditor1")]
[Category("擴展")]
[Editor(typeof(ByteEditor), typeof(System.Drawing.Design.UITypeEditor))]
public byte ByteEditor
{
get;
set;
}
bit設置:
public class ByteEditor : System.ComponentModel.Design.ArrayEditor
{
BitValue[] byteArray = new BitValue[8];
public ByteEditor(Type type)
: base(type)
{
}
protected override object SetItems(object editValue, object[] value)
{
editValue = BitConverter.GetBytes((byte)editValue);
return base.SetItems(editValue, value);
}
protected override object[] GetItems(object editValue)
{
for (int i = 0; i < 8; i++)
{
byteArray[i] = new BitValue()
{
jishufangshi = (jishufangshiEnum)
Enum.Parse(
typeof(jishufangshiEnum),
((byte)(0x01 & ((int)(byte)editValue) >> i)).ToString()
)
};
}
return base.GetItems(byteArray);
}
protected override Type CreateCollectionItemType()
{
return typeof(BitValue);
}
protected override string GetDisplayText(object value)
{
return base.GetDisplayText(value);
}
protected override System.Collections.IList GetObjectsFromInstance(object instance)
{
throw new Exception("The method or operation is not implemented.");
}
protected override bool CanRemoveInstance(object value)
{
throw new Exception("The method or operation is not implemented.");
}
protected override object CreateInstance(Type itemType)
{
return CreateInstance(itemType);
//throw new Exception("The method or operation is not implemented.");
}
//public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
//{
// return base.GetEditStyle(context);
//}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return base.GetEditStyle(context);
//throw new Exception("The method or operation is not implemented.");
}
protected override void ShowHelp()
{
throw new Exception("The method or operation is not implemented.");
}
protected override void CancelChanges()
{
base.CancelChanges();
//throw new Exception("The method or operation is not implemented.");
}
protected override Type[] CreateNewItemTypes()
{
return base.CreateNewItemTypes();
//throw new Exception("The method or operation is not implemented.");
}
protected override void DestroyInstance(object instance)
{
base.DestroyInstance(instance);
//throw new Exception("The method or operation is not implemented.");
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
return base.EditValue(context, provider, value);
//throw new Exception("The method or operation is not implemented.");
}
//public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
//{
// return base.EditValue(context, provider, value);
//}
protected override bool CanSelectMultipleInstances()
{
return base.CanSelectMultipleInstances();
//throw new Exception("The method or operation is not implemented.");
}
protected override CollectionForm CreateCollectionForm()
{
CollectionForm collectionForm = base.CreateCollectionForm();
collectionForm.Text = "協議編輯器";
collectionForm.Size = new System.Drawing.Size(586, 400);
foreach (System.Windows.Forms.Control table in collectionForm.Controls)
{
if (!(table is System.Windows.Forms.TableLayoutPanel)) { continue; }
foreach (System.Windows.Forms.Control c1 in table.Controls)
{
//如果屬性值發生改變,則通知編輯器已改變
if (c1 is PropertyGrid)
{
PropertyGrid propertyGrid = (PropertyGrid)c1;
propertyGrid.HelpVisible = true;
propertyGrid.ToolbarVisible = false;
//propertyGrid.PropertyValueChanged += new PropertyValueChangedEventHandler(GotModifiedHandler);
//propertyGrid.SelectedObjectsChanged += new EventHandler(propertyGrid_SelectedObjectsChanged);
}
else if (c1 is TableLayoutPanel)
{
foreach (System.Windows.Forms.Control c2 in c1.Controls)
{
if (!(c2 is Button)) { continue; }
Button button = (Button)c2;
//如果增加或刪除Collection中的Item了,通知編輯器已改變
if (button.Name == "addButton" || button.Name == "removeButton")
{
//隱藏增加和刪除按鈕
button.Visible = false;
}
if (button.Name.Equals("okbutton", StringComparison.OrdinalIgnoreCase))
{
button.Click += new EventHandler(button_Click);
}
}
}
else if (c1 is Button)
{
Button btn = c1 as Button;
if (btn != null && (btn.Name == "downButton" || btn.Name == "upButton"))
{
btn.Visible = false;
}
}
}
}
return collectionForm;
}
void button_Click(object sender, EventArgs e)
{
var btn = sender as Button;
var form = btn.Parent.Parent.Parent as CollectionForm;
if (form != null)
{
if (byteArray[0] != null)
{
int val = (int)this.byteArray[0].jishufangshi +
((int)byteArray[1].jishufangshi << 1) +
((int)byteArray[2].jishufangshi << 1);
//(context.Instance as T1).ByteEditor = (byte)val;
form.EditValue = (byte)val;
}
}
}
}
public enum jishufangshiEnum
{
jishu = 0,
bujisu = 1,
}
public class BitValue
{
[Category("jiben")]
[DisplayName("計數方式")]
[Description("nan")]
[Browsable(true)]
public jishufangshiEnum jishufangshi
{
get;
set;
}
public override string ToString()
{
return "jishufangshi" + (((int)jishufangshi).ToString());
}
}