我建了一个控件,控件的构造函数一个为有参,一个为无参,我在一个窗体中加入了这个控件,我如何选择性的使用这两个构造函数?
控件代码如下:
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();
}
}
}

解决方案 »

  1.   

    选择性的?你想用的时候就用嘛。 
    layer_property layer = layer_property("haha");如果是窗体设置器中拖放控件,总是调用默认无参的构造函数。
      

  2.   

    楼上正解无参就这样用 layer_property layer = new layer_property();
      

  3.   

    如二楼所说,我也是这样用的,但是,如果用layer_property layer = layer_property("haha");没有任何问题,但如果,这样写: string s = "haha";  
    layer_property layer = layer_property(s);
    重新生成,运行都没有问题,但只要切换到设计视图,就报错,然后,控件就消失了!