是這樣的,小弟現在在寫一個程式,期間用到了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事件,有沒有大哥知道是為什么嗎?希望知道的大哥能夠不吝賜教,小弟感激不盡!
uj5u.com熱心網友回復:
沒人嗎,頂頂
uj5u.com熱心網友回復:
其他的代碼呢uj5u.com熱心網友回復:
這個是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;
}
}
uj5u.com熱心網友回復:
這個是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);
}
}
uj5u.com熱心網友回復:
SignalColor和ValueChanged的代碼uj5u.com熱心網友回復:
然后這個是下拉多選框的代碼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
uj5u.com熱心網友回復:
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);
}
uj5u.com熱心網友回復:
SignalPorperty也是一個自定義的類public class SignalProperty
{
.....
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/31305.html
標籤:C#
上一篇:flexcell的問題
