我建了一个控件,控件的构造函数一个为有参,一个为无参,我在一个窗体中加入了这个控件,我如何选择性的使用这两个构造函数?
控件代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Odbc;
using System.IO;
namespace Control_newlayer
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class layer_property : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Label label1;
public System.Windows.Forms.TextBox layername;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
public System.Windows.Forms.Button button_fh;
public System.Windows.Forms.CheckBox checkBox_autolable;
public System.Windows.Forms.Button button_bzsx;
public System.Windows.Forms.CheckBox checkBox_sffw;
public System.Windows.Forms.TextBox textBox_zoommin;
public System.Windows.Forms.TextBox textBox_zoommax;
public System.Windows.Forms.Button button_ok;
public System.Windows.Forms.Button button_cancel;
public AxMapXLib.AxMap MapXobject;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;public layer_property()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
public layer_property( string layerName)
{
InitializeComponent();
try
{
this.layername.Text = layerName;
this.layername.Enabled = false;
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )components.Dispose();
}
base.Dispose( disposing );
} 组件设计器生成的代码
public void button_ok_Click(object sender, System.EventArgs e)
{
string s = this.MapXobject.Layers[1].Name;
MessageBox.Show(s);
}private void button_cancel_Click(object sender, System.EventArgs e)
{
this.ParentForm.Close();
}
}
}
控件代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Odbc;
using System.IO;
namespace Control_newlayer
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class layer_property : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Label label1;
public System.Windows.Forms.TextBox layername;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
public System.Windows.Forms.Button button_fh;
public System.Windows.Forms.CheckBox checkBox_autolable;
public System.Windows.Forms.Button button_bzsx;
public System.Windows.Forms.CheckBox checkBox_sffw;
public System.Windows.Forms.TextBox textBox_zoommin;
public System.Windows.Forms.TextBox textBox_zoommax;
public System.Windows.Forms.Button button_ok;
public System.Windows.Forms.Button button_cancel;
public AxMapXLib.AxMap MapXobject;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;public layer_property()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
public layer_property( string layerName)
{
InitializeComponent();
try
{
this.layername.Text = layerName;
this.layername.Enabled = false;
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )components.Dispose();
}
base.Dispose( disposing );
} 组件设计器生成的代码
public void button_ok_Click(object sender, System.EventArgs e)
{
string s = this.MapXobject.Layers[1].Name;
MessageBox.Show(s);
}private void button_cancel_Click(object sender, System.EventArgs e)
{
this.ParentForm.Close();
}
}
}
layer_property layer = layer_property("haha");如果是窗体设置器中拖放控件,总是调用默认无参的构造函数。
layer_property layer = layer_property(s);
重新生成,运行都没有问题,但只要切换到设计视图,就报错,然后,控件就消失了!