准备设计一个自定义控件,实现选择保存文件路径,包含有自定义路径和默认路径,其中控件有:GroupPanel一个、LabelX一个、TextBoxX一个、RadioButton两个、ButtonX按钮一个,我想动态改变Lebel的Text属性以及GroupPanel的背景、样式等等,在将该自定义控件加载到一窗体后,找到对应的子控件属性,改变相应值,保存窗体,然后关闭窗体后再重新打开窗体,发现该刚才在控件上修改的子控件属性又恢复到默认值。不知道该如何实现我需要的功能。谢谢指教!
我的代码如下(还没完善):using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;namespace SavePath
{
public partial class SavePath : UserControl
{
private string m_strOpenFileDialogFilter = "";
private string m_strOpenFileDialogTitle = "";
private GroupPanel m_ctlGroupPanel_SavePath = null;
private LabelX m_ctrlLabel_SavePathTitle = null;
private RadioButton m_ctlRadiaoButton_Default = null;
private RadioButton m_ctlRadiaoButton_Custom = null;
private TextBoxX m_ctlTextBoxX_FilePath = null;
private ButtonX m_ctlButton_OpenPath= null; public SavePath()
{
InitializeComponent();
this.VariableInitialize();
} private void VariableInitialize()
{
this.m_strOpenFileDialogFilter = "";
this.m_strOpenFileDialogTitle = "";
this.m_ctlGroupPanel_SavePath = this.grpSavePath;
this.m_ctrlLabel_SavePathTitle = this.lblSavePathTitle;
this.m_ctlRadiaoButton_Default = this.rdbDefaultPath;
this.m_ctlRadiaoButton_Custom = this.rdbCustomPath;
this.m_ctlTextBoxX_FilePath = this.txtReportFilePath;
this.m_ctlButton_OpenPath = this.btnOpenReportPath;
} #region ===== 自定义控件属性 =====
[Category("自定义参数设置")]
[DefaultValue("")]
[Description("获取/设置打开文件对话框过滤器内容")]
public string OpenFileDialogFilter
{
get
{
return this.m_strOpenFileDialogFilter;
}
set
{
this.m_strOpenFileDialogFilter = value;
}
} [Category("自定义参数设置")]
[DefaultValue("")]
[Description("获取/设置打开文件对话框标题内容")]
public string OpenFileDialogTitle
{
get
{
return this.m_strOpenFileDialogTitle;
}
set
{
this.m_strOpenFileDialogTitle = value;
}
} [Category("自定义参数设置")]
[Description("获取/设置组容器控件")]
public GroupPanel Control_GroupPanel
{
get
{
return this.m_ctlGroupPanel_SavePath;
}
set
{
this.m_ctlGroupPanel_SavePath = value;
this.grpSavePath = this.m_ctlGroupPanel_SavePath;
}
} [Category("自定义参数设置")]
[Description("获取/设置控件标题提示语控件")]
public LabelX Control_LableTitle
{
get
{
return this.m_ctrlLabel_SavePathTitle;
}
set
{
this.m_ctrlLabel_SavePathTitle = value;
this.lblSavePathTitle.Text = value.Text;
this.lblSavePathTitle.Font = value.Font;
this.lblSavePathTitle.ForeColor = value.ForeColor;
}
} [Category("自定义参数设置")]
[Description("获取/设置默认方式单选按钮控件")]
public RadioButton Control_RadioButtonDefault
{
get
{
return this.m_ctlRadiaoButton_Default;
}
set
{
this.m_ctlRadiaoButton_Default = value;
this.rdbDefaultPath.Checked = value.Checked;
this.rdbDefaultPath.Font = value.Font;
this.rdbDefaultPath.ForeColor = value.ForeColor;
this.rdbDefaultPath.Text = value.Text;
}
} [Category("自定义参数设置")]
[Description("获取/设置自定义方式单选按钮控件")]
public RadioButton Control_RadioButtonCustom
{
get
{
return this.m_ctlRadiaoButton_Custom;
}
set
{
this.m_ctlRadiaoButton_Custom = value;
this.rdbCustomPath.Checked = value.Checked;
this.rdbCustomPath.Font = value.Font;
this.rdbCustomPath.ForeColor = value.ForeColor;
this.rdbCustomPath.Text = value.Text;
}
} [Category("自定义参数设置")]
[Description("获取/设置文件保存路径控件")]
public TextBoxX Control_TextBoxSavePath
{
get
{
return this.m_ctlTextBoxX_FilePath;
}
set
{
this.m_ctlTextBoxX_FilePath = value;
this.txtReportFilePath = this.m_ctlTextBoxX_FilePath;
}
} [Category("自定义参数设置")]
[Description("获取/设置打开文件保存路径按钮控件")]
public ButtonX Control_ButtonOpenPath
{
get
{
return this.m_ctlButton_OpenPath;
}
set
{
this.m_ctlButton_OpenPath = value;
this.btnOpenReportPath = this.m_ctlButton_OpenPath;
}
}
#endregion #region ========================选择报表路径================================
private void rdbCustomPath_CheckedChanged(object sender, EventArgs e)
{
if (this.rdbCustomPath.Checked == true)
this.EnableCustomPath(true);
} private void EnableCustomPath(bool isEnable)
{
this.txtReportFilePath.ReadOnly = !isEnable;
this.btnOpenReportPath.Enabled = isEnable;
} private void btnOpenReportPath_Click(object sender, EventArgs e)
{
SaveFileDialog dlgSaveReportFilePath = null; try
{
dlgSaveReportFilePath = new SaveFileDialog();
dlgSaveReportFilePath.Filter = this.m_strOpenFileDialogFilter;
dlgSaveReportFilePath.Title = this.m_strOpenFileDialogTitle;
dlgSaveReportFilePath.CheckPathExists = true;
dlgSaveReportFilePath.OverwritePrompt = true;
dlgSaveReportFilePath.RestoreDirectory = false; if (dlgSaveReportFilePath.ShowDialog() != DialogResult.OK)
return; this.txtReportFilePath.Text = dlgSaveReportFilePath.FileName;
}
catch (Exception ex)
{
MessageBox.Show("选择文件保存路径发生错误,错误信息为:->\r\n" + ex.Message, "错误信息",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
}
}
我的代码如下(还没完善):using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;namespace SavePath
{
public partial class SavePath : UserControl
{
private string m_strOpenFileDialogFilter = "";
private string m_strOpenFileDialogTitle = "";
private GroupPanel m_ctlGroupPanel_SavePath = null;
private LabelX m_ctrlLabel_SavePathTitle = null;
private RadioButton m_ctlRadiaoButton_Default = null;
private RadioButton m_ctlRadiaoButton_Custom = null;
private TextBoxX m_ctlTextBoxX_FilePath = null;
private ButtonX m_ctlButton_OpenPath= null; public SavePath()
{
InitializeComponent();
this.VariableInitialize();
} private void VariableInitialize()
{
this.m_strOpenFileDialogFilter = "";
this.m_strOpenFileDialogTitle = "";
this.m_ctlGroupPanel_SavePath = this.grpSavePath;
this.m_ctrlLabel_SavePathTitle = this.lblSavePathTitle;
this.m_ctlRadiaoButton_Default = this.rdbDefaultPath;
this.m_ctlRadiaoButton_Custom = this.rdbCustomPath;
this.m_ctlTextBoxX_FilePath = this.txtReportFilePath;
this.m_ctlButton_OpenPath = this.btnOpenReportPath;
} #region ===== 自定义控件属性 =====
[Category("自定义参数设置")]
[DefaultValue("")]
[Description("获取/设置打开文件对话框过滤器内容")]
public string OpenFileDialogFilter
{
get
{
return this.m_strOpenFileDialogFilter;
}
set
{
this.m_strOpenFileDialogFilter = value;
}
} [Category("自定义参数设置")]
[DefaultValue("")]
[Description("获取/设置打开文件对话框标题内容")]
public string OpenFileDialogTitle
{
get
{
return this.m_strOpenFileDialogTitle;
}
set
{
this.m_strOpenFileDialogTitle = value;
}
} [Category("自定义参数设置")]
[Description("获取/设置组容器控件")]
public GroupPanel Control_GroupPanel
{
get
{
return this.m_ctlGroupPanel_SavePath;
}
set
{
this.m_ctlGroupPanel_SavePath = value;
this.grpSavePath = this.m_ctlGroupPanel_SavePath;
}
} [Category("自定义参数设置")]
[Description("获取/设置控件标题提示语控件")]
public LabelX Control_LableTitle
{
get
{
return this.m_ctrlLabel_SavePathTitle;
}
set
{
this.m_ctrlLabel_SavePathTitle = value;
this.lblSavePathTitle.Text = value.Text;
this.lblSavePathTitle.Font = value.Font;
this.lblSavePathTitle.ForeColor = value.ForeColor;
}
} [Category("自定义参数设置")]
[Description("获取/设置默认方式单选按钮控件")]
public RadioButton Control_RadioButtonDefault
{
get
{
return this.m_ctlRadiaoButton_Default;
}
set
{
this.m_ctlRadiaoButton_Default = value;
this.rdbDefaultPath.Checked = value.Checked;
this.rdbDefaultPath.Font = value.Font;
this.rdbDefaultPath.ForeColor = value.ForeColor;
this.rdbDefaultPath.Text = value.Text;
}
} [Category("自定义参数设置")]
[Description("获取/设置自定义方式单选按钮控件")]
public RadioButton Control_RadioButtonCustom
{
get
{
return this.m_ctlRadiaoButton_Custom;
}
set
{
this.m_ctlRadiaoButton_Custom = value;
this.rdbCustomPath.Checked = value.Checked;
this.rdbCustomPath.Font = value.Font;
this.rdbCustomPath.ForeColor = value.ForeColor;
this.rdbCustomPath.Text = value.Text;
}
} [Category("自定义参数设置")]
[Description("获取/设置文件保存路径控件")]
public TextBoxX Control_TextBoxSavePath
{
get
{
return this.m_ctlTextBoxX_FilePath;
}
set
{
this.m_ctlTextBoxX_FilePath = value;
this.txtReportFilePath = this.m_ctlTextBoxX_FilePath;
}
} [Category("自定义参数设置")]
[Description("获取/设置打开文件保存路径按钮控件")]
public ButtonX Control_ButtonOpenPath
{
get
{
return this.m_ctlButton_OpenPath;
}
set
{
this.m_ctlButton_OpenPath = value;
this.btnOpenReportPath = this.m_ctlButton_OpenPath;
}
}
#endregion #region ========================选择报表路径================================
private void rdbCustomPath_CheckedChanged(object sender, EventArgs e)
{
if (this.rdbCustomPath.Checked == true)
this.EnableCustomPath(true);
} private void EnableCustomPath(bool isEnable)
{
this.txtReportFilePath.ReadOnly = !isEnable;
this.btnOpenReportPath.Enabled = isEnable;
} private void btnOpenReportPath_Click(object sender, EventArgs e)
{
SaveFileDialog dlgSaveReportFilePath = null; try
{
dlgSaveReportFilePath = new SaveFileDialog();
dlgSaveReportFilePath.Filter = this.m_strOpenFileDialogFilter;
dlgSaveReportFilePath.Title = this.m_strOpenFileDialogTitle;
dlgSaveReportFilePath.CheckPathExists = true;
dlgSaveReportFilePath.OverwritePrompt = true;
dlgSaveReportFilePath.RestoreDirectory = false; if (dlgSaveReportFilePath.ShowDialog() != DialogResult.OK)
return; this.txtReportFilePath.Text = dlgSaveReportFilePath.FileName;
}
catch (Exception ex)
{
MessageBox.Show("选择文件保存路径发生错误,错误信息为:->\r\n" + ex.Message, "错误信息",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
}
}
比如你想改变Labelx的Text属性,不必将整个控件公开吧,只要添加一个访问LabelX.Text的属性就可以啊
[Category("自定义参数设置")]
[Description("获取/设置控件标题提示语控件")]
public LabelX Control_LableTitle
{
get
{
return this.m_ctrlLabel_SavePathTitle;
}
set
{
this.m_ctrlLabel_SavePathTitle = value;
this.lblSavePathTitle.Text = value.Text;
this.lblSavePathTitle.Font = value.Font;
this.lblSavePathTitle.ForeColor = value.ForeColor;
}
}
可以换成
[Category("自定义参数设置")]
[Description("获取/设置控件标题提示语控件")]
public string Control_LableTitle
{
get
{
return this.m_ctrlLabel_SavePathTitle.Text;
}
set
{
this.lblSavePathTitle.Text = value }
}如果还不行就将setter方法的那句代码改作用propertyinfo.Setvalue的方法更改控件在设计阶段的更改
不记得这样写对不对,这样在设计阶段设计器可接收到属性的更改事件,进而生成设计时的必要代码,这样才能将设计时的更改带到运行时呀!
[Category("自定义参数设置")]
[Description("获取/设置控件标题提示语控件")]
public LabelX Control_LableTitle
{
get
{
return this.lblSavePathTitle;
}
}
直接获取引用对象,这样它内部的属性就全部暴露了,但是绝对不能设置set属性。原因?你自己先想想。