C# 使用UITypeEditor自定义控件时遇到的问题

qq_37643403 2020-06-16 03:11:43
是这样的,小弟现在在写一个程序,期间用到了PropertyGrid控件;现在有一个需求,需要我在PropertyGrid控件中添加一个下拉多选框,小弟查阅网上资料知道可以使用UITypeEditor自定义控件。到这里都很顺利,但是我有一个需求就是需要跟数据库交互,我打算的是,使用PropertyGrid的ValueChanged事件,当在UI上修改属性对应的值是,就会触发该事件然后在这个事件处理函数中,将修改的值更新到数据库中。我在其他的属性都没有问题,但是在这个使用了UITypeEditor的属性上出了问题,下面是该属性的代码:
[Editor(typeof(SignalColorEditor), typeof(UITypeEditor)),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
CategoryAttribute("信号灯"),
DisplayNameAttribute("信号灯颜色")]
public SignalColor SignalColor
{
get { return this._signalColor; }
set
{
if (this._signalColor != value)
{
this._signalColor = value;
}
}
}
当我在UI上修改这个复选框时,并不会触发PropertyGrid的ValueChanged事件,有没有大哥知道是为什么吗?希望知道的大哥能够不吝赐教,小弟感激不尽!
...全文
560 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_37643403 2020-06-16
  • 打赏
  • 举报
回复
SignalPorperty也是一个自定义的类
public class SignalProperty
{
.....

}
qq_37643403 2020-06-16
  • 打赏
  • 举报
回复
SignalColor是一个自定义的类,没有ValueChanged代码;倒是PropertyGrid有ValueChanged代码:

propertyGrid1.SelectedObject = VisioMySql.VisioQuerySignalData("xhj", _name);这个返回的是一个SignalPorperty类的对象


private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "ChangedItem", e.ChangedItem.ToString());
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "OldValue", e.OldValue);
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "NowValue", e.ChangedItem.Value.ToString());
MessageBox.Show(messageBoxCS.ToString(), "PropertyValueChanged Event");

VisioMySql.DBname = OpenedStationName;
string changedItemName = e.ChangedItem.PropertyDescriptor.Name.ToString();
SignalProperty signalProperty = (SignalProperty)propertyGrid1.SelectedObject;
string deviceName = signalProperty.Name;
string deviceType = signalProperty.DeviceType;//用来确定表的
string tableName = string.Empty;

//"System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry "ID
// string propstr = "System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry ";
string changedItem = e.ChangedItem.Value.ToString();
Type type = e.ChangedItem.PropertyDescriptor.PropertyType;//类型
switch (deviceType)
{
case "Signal":
tableName = "xhj";
break;
case "Switch":
tableName = "dc";
break;
case "IsolationJoint":
tableName = "jyj";
break;
case "Block":
if (VisioReadSetCustomerProperty.ShapeCellExist(SelectedShapes[0], "是否弯股"))
{
tableName = "wg";
break;
}
else
{
tableName = "dg";
break;
}
default:break;
}
VisioMySql.VisioUpdataToDB(tableName,changedItemName,changedItem,deviceName,type);


}
qq_37643403 2020-06-16
  • 打赏
  • 举报
回复
然后这个是下拉多选框的代码
public partial class PropertySignalColor : UserControl
{
private bool canceling;
private SignalColor _oldSignalColor;
private CheckBox Color_H;
private CheckBox Color_L;
private CheckBox Color_U;
private CheckBox Color_2U;
private CheckBox Color_B;
private CheckBox Color_A;
private CheckBox Color_2L;
private CheckBox Color_LU;
private CheckBox Color_YB;
private CheckBox Color_USU;
private CheckBox Color_BS;
private CheckBox Color_HS;
private CheckBox Color_US;
private CheckBox Color_LS;
private SignalColor _newSignalColor;
public PropertySignalColor(SignalColor signalColor)
{
_oldSignalColor = signalColor;
_newSignalColor = signalColor;
SignalColor _signalColor = new SignalColor();
InitializeComponent();
}

public SignalColor SignalColor
{
get { return _newSignalColor; }

}

/// <summary>
/// 重写键盘接收处理ESC
/// </summary>
/// <param name="keyData"></param>
/// <returns></returns>
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Escape)
{
_oldSignalColor = _newSignalColor;
canceling = true;
}
return base.ProcessDialogKey(keyData);
}

private void InitializeComponent()
{
this.Color_H = new System.Windows.Forms.CheckBox();
this.Color_L = new System.Windows.Forms.CheckBox();
this.Color_U = new System.Windows.Forms.CheckBox();
this.Color_2U = new System.Windows.Forms.CheckBox();
this.Color_B = new System.Windows.Forms.CheckBox();
this.Color_A = new System.Windows.Forms.CheckBox();
this.Color_2L = new System.Windows.Forms.CheckBox();
this.Color_LU = new System.Windows.Forms.CheckBox();
this.Color_YB = new System.Windows.Forms.CheckBox();
this.Color_USU = new System.Windows.Forms.CheckBox();
this.Color_BS = new System.Windows.Forms.CheckBox();
this.Color_HS = new System.Windows.Forms.CheckBox();
this.Color_US = new System.Windows.Forms.CheckBox();
this.Color_LS = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// Color_H
//
this.Color_H.AutoSize = true;
this.Color_H.Location = new System.Drawing.Point(17, 13);
this.Color_H.Name = "Color_H";
this.Color_H.Size = new System.Drawing.Size(44, 19);
this.Color_H.TabIndex = 0;
this.Color_H.Text = "红";
this.Color_H.UseVisualStyleBackColor = true;
//
// Color_L
//
this.Color_L.AutoSize = true;
this.Color_L.Location = new System.Drawing.Point(17, 38);
this.Color_L.Name = "Color_L";
this.Color_L.Size = new System.Drawing.Size(44, 19);
this.Color_L.TabIndex = 1;
this.Color_L.Text = "绿";
this.Color_L.UseVisualStyleBackColor = true;
//
// Color_U
//
this.Color_U.AutoSize = true;
this.Color_U.Location = new System.Drawing.Point(17, 63);
this.Color_U.Name = "Color_U";
this.Color_U.Size = new System.Drawing.Size(44, 19);
this.Color_U.TabIndex = 2;
this.Color_U.Text = "黄";
this.Color_U.UseVisualStyleBackColor = true;
//
// Color_2U
//
this.Color_2U.AutoSize = true;
this.Color_2U.Location = new System.Drawing.Point(17, 89);
this.Color_2U.Name = "Color_2U";
this.Color_2U.Size = new System.Drawing.Size(52, 19);
this.Color_2U.TabIndex = 3;
this.Color_2U.Text = "2黄";
this.Color_2U.UseVisualStyleBackColor = true;
//
// Color_B
//
this.Color_B.AutoSize = true;
this.Color_B.Location = new System.Drawing.Point(17, 115);
this.Color_B.Name = "Color_B";
this.Color_B.Size = new System.Drawing.Size(44, 19);
this.Color_B.TabIndex = 4;
this.Color_B.Text = "白";
this.Color_B.UseVisualStyleBackColor = true;
//
// Color_A
//
this.Color_A.AutoSize = true;
this.Color_A.Location = new System.Drawing.Point(17, 140);
this.Color_A.Name = "Color_A";
this.Color_A.Size = new System.Drawing.Size(44, 19);
this.Color_A.TabIndex = 5;
this.Color_A.Text = "蓝";
this.Color_A.UseVisualStyleBackColor = true;
//
// Color_2L
//
this.Color_2L.AutoSize = true;
this.Color_2L.Location = new System.Drawing.Point(82, 13);
this.Color_2L.Name = "Color_2L";
this.Color_2L.Size = new System.Drawing.Size(52, 19);
this.Color_2L.TabIndex = 6;
this.Color_2L.Text = "2绿";
this.Color_2L.UseVisualStyleBackColor = true;
//
// Color_LU
//
this.Color_LU.AutoSize = true;
this.Color_LU.Location = new System.Drawing.Point(82, 38);
this.Color_LU.Name = "Color_LU";
this.Color_LU.Size = new System.Drawing.Size(59, 19);
this.Color_LU.TabIndex = 7;
this.Color_LU.Text = "绿黄";
this.Color_LU.UseVisualStyleBackColor = true;
//
// Color_YB
//
this.Color_YB.AutoSize = true;
this.Color_YB.Location = new System.Drawing.Point(82, 62);
this.Color_YB.Name = "Color_YB";
this.Color_YB.Size = new System.Drawing.Size(59, 19);
this.Color_YB.TabIndex = 8;
this.Color_YB.Text = "引白";
this.Color_YB.UseVisualStyleBackColor = true;
//
// Color_USU
//
this.Color_USU.AutoSize = true;
this.Color_USU.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.Color_USU.Location = new System.Drawing.Point(82, 88);
this.Color_USU.Name = "Color_USU";
this.Color_USU.Size = new System.Drawing.Size(74, 19);
this.Color_USU.TabIndex = 9;
this.Color_USU.Text = "黄闪黄";
this.Color_USU.UseVisualStyleBackColor = true;
//
// Color_BS
//
this.Color_BS.AutoSize = true;
this.Color_BS.Location = new System.Drawing.Point(82, 115);
this.Color_BS.Name = "Color_BS";
this.Color_BS.Size = new System.Drawing.Size(59, 19);
this.Color_BS.TabIndex = 10;
this.Color_BS.Text = "白闪";
this.Color_BS.UseVisualStyleBackColor = true;
//
// Color_HS
//
this.Color_HS.AutoSize = true;
this.Color_HS.Location = new System.Drawing.Point(82, 139);
this.Color_HS.Name = "Color_HS";
this.Color_HS.Size = new System.Drawing.Size(59, 19);
this.Color_HS.TabIndex = 11;
this.Color_HS.Text = "红闪";
this.Color_HS.UseVisualStyleBackColor = true;
//
// Color_US
//
this.Color_US.AutoSize = true;
this.Color_US.Location = new System.Drawing.Point(152, 13);
this.Color_US.Name = "Color_US";
this.Color_US.Size = new System.Drawing.Size(59, 19);
this.Color_US.TabIndex = 12;
this.Color_US.Text = "黄闪";
this.Color_US.UseVisualStyleBackColor = true;
//
// Color_LS
//
this.Color_LS.AutoSize = true;
this.Color_LS.Location = new System.Drawing.Point(152, 37);
this.Color_LS.Name = "Color_LS";
this.Color_LS.Size = new System.Drawing.Size(59, 19);
this.Color_LS.TabIndex = 13;
this.Color_LS.Text = "绿闪";
this.Color_LS.UseVisualStyleBackColor = true;
//
// PropertySignalColor
//
this.Controls.Add(this.Color_LS);
this.Controls.Add(this.Color_US);
this.Controls.Add(this.Color_HS);
this.Controls.Add(this.Color_BS);
this.Controls.Add(this.Color_USU);
this.Controls.Add(this.Color_YB);
this.Controls.Add(this.Color_LU);
this.Controls.Add(this.Color_2L);
this.Controls.Add(this.Color_A);
this.Controls.Add(this.Color_B);
this.Controls.Add(this.Color_2U);
this.Controls.Add(this.Color_U);
this.Controls.Add(this.Color_L);
this.Controls.Add(this.Color_H);
this.Name = "PropertySignalColor";
this.Size = new System.Drawing.Size(214, 166);
this.Load += new System.EventHandler(this.PropertySignalColor_Load);
this.Leave += new System.EventHandler(this.PropertySignalColor_Leave);
this.ParentChanged += new System.EventHandler(this.PropertySignalColor_ParentChanged);
this.ResumeLayout(false);
this.PerformLayout();

}

private void PropertySignalColor_Load(object sender, EventArgs e)
{
//赋值
Color_H.CheckState = _oldSignalColor.Color_H ? CheckState.Checked : CheckState.Unchecked;
Color_L.CheckState = _oldSignalColor.Color_L ? CheckState.Checked : CheckState.Unchecked;
Color_U.CheckState = _oldSignalColor.Color_U ? CheckState.Checked : CheckState.Unchecked;
Color_2U.CheckState = _oldSignal
Bridge_go 2020-06-16
  • 打赏
  • 举报
回复
SignalColor和ValueChanged的代码
qq_37643403 2020-06-16
  • 打赏
  • 举报
回复
这个是TypeConverter的代码
public class SignalColorConverter:TypeConverter
{
//是否能用string 转换到signalColor类型
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(String)) return true;
//这里也可以是其他类型的,可以参考后面的链接里面有介绍
return base.CanConvertFrom(context, sourceType);
}

//能否从signalColor转换到string
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(SignalColor)) return true;
//if(destinationType == typeof(InstanceDescriptor)) return true;
return base.CanConvertTo(context, destinationType);
}

/// <summary>
/// 从SignalColor转到String类型。在Property窗口中显示为string类型
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value"></param>
/// <param name="destinationType"></param>
/// <returns></returns>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
String result = "";
if (value == null) return result;
if (destinationType == typeof(String))
{
//这里处理选择后再属性框里面显示的值
SignalColor scope = (SignalColor)value;
if (scope.Color_H)
{
if (result.Length > 0)
result += ",红";
else
result += "红";
}
else
result = result.Replace("红", "").Replace(",红","");

if(scope.Color_L)
{
if (result.Length > 0)
result += ",绿";
else
result += "绿";
}
else
result = result.Replace("绿", "").Replace(",绿", "");

if(scope.Color_U)
{
if (result.Length > 0)
result += ",黄";
else
result += "黄";
}
else
result = result.Replace("黄", "").Replace(",黄", "");

if(scope.Color_2U)
{
if (result.Length > 0)
result += ",2黄";
else
result += "2黄";
}
else
result = result.Replace("2黄", "").Replace(",2黄", "");

if(scope.Color_B)
{
if (result.Length > 0)
result += ",白";
else
result += "白";
}
else
result = result.Replace("白", "").Replace(",白", "");

if(scope.Color_A)
{
if (result.Length > 0)
result += ",蓝";
else
result += "蓝";
}
else
result = result.Replace("蓝", "").Replace(",蓝", "");

if(scope.Color_2L)
{
if (result.Length > 0)
result += ",2绿";
else
result += "2绿";
}
else
result = result.Replace("2绿", "").Replace(",2绿", "");

if(scope.Color_LU)
{
if (result.Length > 0)
result += ",绿黄";
else
result += "绿黄";
}
else
result = result.Replace("绿黄", "").Replace(",绿黄", "");

if(scope.Color_YB)
{
if (result.Length > 0)
result += ",引白";
else
result += "引白";
}
else
result = result.Replace("引白", "").Replace(",引白", "");

if(scope.Color_USU)
{
if (result.Length > 0)
result += ",黄闪黄";
else
result += "黄闪黄";
}
else
result = result.Replace("黄闪黄", "").Replace(",黄闪黄", "");

if(scope.Color_BS)
{
if (result.Length > 0)
result += ",白闪";
else
result += "白闪";
}
else
result = result.Replace("白闪", "").Replace(",白闪", "");

if(scope.Color_HS)
{
if (result.Length > 0)
result += ",红闪";
else
result += "红闪";
}
else
result = result.Replace("红闪", "").Replace(",红闪", "");

if(scope.Color_US)
{
if (result.Length > 0)
result += ",黄闪";
else
result += "黄闪";
}
else
result = result.Replace("黄闪", "").Replace(",黄闪", "");

if(scope.Color_LS)
{
if (result.Length > 0)
result += ",绿闪";
else
result += "绿闪";
}
else
result = result.Replace("绿闪", "").Replace(",绿闪", "");

return result;
}
//这段代码看不太懂不知道是干什么的
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(SignalColor).GetConstructor(new Type[] { typeof(bool), typeof(bool), typeof(bool),
typeof(bool),typeof(bool),typeof(bool),typeof(bool),
typeof(bool),typeof(bool),typeof(bool),typeof(bool),
typeof(bool),typeof(bool), typeof(bool)});
SignalColor scope = (SignalColor)value;
return new InstanceDescriptor(ci, new object[] { scope.Color_H,scope.Color_L,scope.Color_U,scope.Color_2U,scope.Color_B,
scope.Color_A,scope.Color_2L,scope.Color_LU,scope.Color_YB,scope.Color_USU,
scope.Color_BS,scope.Color_HS,scope.Color_US,scope.Color_LS});
}
return base.ConvertTo(context, culture, value, destinationType);
}


//从String转换为SignalColor
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value == null || value.ToString().Length == 0) return new SignalColor();
if (value is string)
{
//String[] v = ((String)value).Split(',');
string tempvalue = (string)value;
string[] v = tempvalue.Split(',');
SignalColor csf = new SignalColor();
csf.Color_H = v.Contains("红");
csf.Color_HS = v.Contains("红闪");
//csf.Color_H = v.Contains("红");

csf.Color_L = v.Contains("绿");
csf.Color_2L = v.Contains("2绿");
csf.Color_LU = v.Contains("绿黄");
csf.Color_LS = v.Contains("绿闪");

csf.Color_U = v.Contains("黄");
csf.Color_2U = v.Contains("2黄");
csf.Color_USU = v.Contains("黄闪黄");
csf.Color_US = v.Contains("黄闪");

csf.Color_B = v.Contains("白");
csf.Color_YB = v.Contains("引白");
csf.Color_BS = v.Contains("白闪");

csf.Color_A = v.Contains("蓝");
return csf;
}
return base.ConvertFrom(context, culture, value);
}
}
qq_37643403 2020-06-16
  • 打赏
  • 举报
回复
这个是UITypeEditor的代码
public class SignalColorEditor:UITypeEditor
{


/// <summary>
/// 返回下拉模式,表示EditValue方法使用的编辑器样式为下拉模式
/// </summary>
/// <param name="context"></param>
/// <returns>一个UITypeEditorEditStyle值</returns>
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand)]
public override System.Drawing.Design.UITypeEditorEditStyle
GetEditStyle( System.ComponentModel.ITypeDescriptorContext context)
{
if (context != null && context.Instance != null)
{
//这里设置下拉模式,还有弹出框的模式
return UITypeEditorEditStyle.DropDown;
}
return base.GetEditStyle(context);
}

/// <summary>
/// 使用GetEditStyle()方法所指示的编辑器样式编辑指定对象的值
/// </summary>
/// <param name="context"></param>
/// <param name="provider"></param>
/// <param name="value">要编辑的对象</param>
/// <returns></returns>
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand)]
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
IWindowsFormsEditorService editorService = null;
if (context != null && context.Instance != null && provider != null)
{
editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (editorService != null)
{
//ComboFund自定义控件
SignalProperty control = (SignalProperty)context.Instance;
PropertySignalColor psc = new PropertySignalColor(control.SignalColor);
editorService.DropDownControl(psc);
value = psc.SignalColor;
return value;
}
}
return value;
}

}
Bridge_go 2020-06-16
  • 打赏
  • 举报
回复
其他的代码呢
qq_37643403 2020-06-16
  • 打赏
  • 举报
回复
没人吗,顶顶
这篇文章中我们重点需要实现的是(3)、(4)两项功能,下面我们来介绍具体实现的方法。 第一步,实现ImageComboBoxItem类。 要实现显示图标,当然要给每个项添加与图标相关的信息了,ImageComboBoxItem类应该包括以下内容:文本(Text)、缩进的级别(Level)、图标的索引(ImageIndex、ImageKey),用户数据(Tag)。ImageComboBoxItem类实现了ISerializable接口,实现自定义序列化。ImageComboBoxItem类的类视图如下: 图3 ImageComboxItem类视图 ImageComboBoxItem类的代码如下: [Serializable] [DefaultProperty("Text")] [TypeConverter( typeof(ExpandableObjectConverter))] public class ImageComboBoxItem : IDisposable, ISerializable ...{ Fields#region Fields private ImageComboBox _imageComboBox; private string _text = "ImageComboBoxItem"; private ImageComboBoxItemImageIndexer _imageIndexer; private object _tag; private int _level; #endregion Constructors#region Constructors public ImageComboBoxItem() ...{ } public ImageComboBoxItem(string text) : this(text, -1, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex) : this(text, imageIndex, 0) ...{ } public ImageComboBoxItem( string text, string imageKey) : this(text, imageKey, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex, int level) : this() ...{ _text = text; ImageIndexer.Index = imageIndex; _level = level; } public ImageComboBoxItem( string text, string imageKey, int level) : this() ...{ _text = text; ImageIndexer.Key = imageKey; _level = level; } protected ImageComboBoxItem( SerializationInfo info, StreamingContext context) : this() ...{ Deserialize(info, context); } #endregion Properties#region Properties [Localizable(true)]
工作以来积累了很多的常用的代码,计划整合到一个自定义的类库里面。所以在接下来的一段间会不定期的更新这个类库。 名称:DragonLibrary.Design类库 1.0.0.0 for VS2003 版本:1.0.0.0 语言:C# 作者:Dragon 作者邮箱:w001162@sina.com 作者博客:http://blog.csdn.net/w001162 运行环境:.NET Framework 1.1 命名空间:DragonLibrary.Deign 命名空间介绍:Desing命名空间下主要是对自定控件设计的支持用到的一些类 更新内容:ShortcutKeysEditor类,这是模仿VS2005的菜单项的ShortcutKeys属性的属性编辑器。就是修改属性会有CTRL、SHIFT、ALT三个复选框和一个快捷键下拉框的编辑器。 ※※重要提示※※ 当自定控件的Keys属性使用这个属性编辑器并选择Ctrl+C等组合键,自动生成的代码会出现.ShortcutKeys = System.Windows.Forms.Keys.Ctrl+C这样的情况而导致无法编译通过。理想的情况下应该生成.ShortcutKeys = System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.C这样的代码。 看到这个履历的各位,如果可以解决这个问题的话,请发信到我的邮箱:w001162@sina.com或到我的博客留言:http://blog.csdn.net/w001162 当自定控件的Keys属性使用这个属性编辑器并选择Ctrl+C等组合键,自动生成的代码会出现.ShortcutKeys = System.Windows.Forms.Keys.Ctrl+C这样的情况而导致无法编译通过。理想的情况下应该生成.ShortcutKeys = System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.C这样的代码。 看到这个履历的各位,如果可以解决这个问题的话,请发信到我的邮箱:w001162@sina.com或到我的博客留言:http://blog.csdn.net/w001162

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧