【发布时间】:2014-03-16 00:45:51
【问题描述】:
我有一个DataGridView 派生控件,它定义了一些新属性。它们在属性检查器中按预期显示,并输入了连接字符串值,但是当我编译程序时,如果我在 if (ConnectionString == "" ) 行中的构造函数中放置一个中断 - 它显示 ConnectionString 为空,connectionString 确实为空。我意识到我应该在写完这篇文章后测试 null 但关键是该属性中有一个字符串。
目的是将 table 和 connectionString 定义为从中填充它的属性。我做错了什么?
namespace ASControls
{
public partial class ASDataGridView : DataGridView
{
private string connectionString;
// Declares the property.
[Description("Set the Connection String for the datbase concerned"),
Category("Data")]
public string ConnectionString
{
get { return connectionString; }
set {connectionString= value; }
}
public ASDataGridView()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
if (ConnectionString == "" )
{
MessageBox.Show("You must set the connection string in order to use an ASDataGridView control");
return;
} else {
InitData(ConnectionString);
}
}
...
【问题讨论】:
-
您可能需要检查属性
this.DesignMode.. 因为如果您不允许他们在设置之前使用控件,人们就无法在设计器中设置属性..跨度> -
感谢您回来 simon,但我没有按照您的意思理解:目前该属性是在属性检查器中的设计时设置的!
-
现在我重新考虑一下,我真的完全不明白 DesignMode 的意义所在。为什么程序会处于设计模式?派生的构造函数在表单编译时按预期触发(因此它不在设计模式下)只是属性的值与预期不符 - 我假设我正在查看错误的实例,但我不知道是怎么回事!
标签: c# inheritance datagridview constructor