2011-11-06 89 views
3

我正在学习C#并且在加载系统事件触发时遇到问题。直到我将form1和form2连接在一起以在它们之间传递变量之前它工作正常。 IE从Form2上的选定项目在Form 1上设置标签。谢谢你的帮助!C#加载事件不会触发

这是我为Form1代码:

namespace WindowsFormsApplication5 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 


    private void button1_Click(object sender, EventArgs e) 
    { 
     selectdb selectdb = new selectdb(this); // this is the button that shows the form in question. It worked fine until I added the (this). 
     selectdb.Show(); 
    } 

    public string LabelText 
    { 
     get { return label1.Text; } 
     set { label1.Text = value; } 
    } 

} 

} 

这里是我的Form2的代码:

namespace WindowsFormsApplication5 
{ 
public partial class selectdb : Form 
{ 

    public selectdb() 
    { 
     InitializeComponent(); 
     //this.Name = "selectdb"; 
     //this.Text = "selectdb"; 
     this.Load += new System.EventHandler(selectdb_Load); 

    } 
    private Form1 mainForm = null; 

    public selectdb(Form callingForm) 
    { 
     mainForm = callingForm as Form1; 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     this.mainForm.LabelText = listBox1.SelectedItem.ToString(); 
    } 

    private void selectdb_Load(Object sender, EventArgs e) 
    { 
     // Microsoft Access provider factory 
     DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); 

     DataTable userTables = null; 
     using (DbConnection connection = factory.CreateConnection()) 
     { 
      connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"C:\\Database.accdb\";Persist Security Info=False;"; 
      // We only want user tables, not system tables 
      string[] restrictions = new string[4]; 
      restrictions[3] = "Table"; 

      connection.Open(); 

      // Get list of user tables 
      userTables = connection.GetSchema("Tables", restrictions); 
     } 

     List<string> tableNames = new List<string>(); 
     for (int i = 0; i < userTables.Rows.Count; i++) 
      listBox1.Items.Add(userTables.Rows[i][2].ToString()); 

    } 

} 
} 
+0

看起来很清楚 - 你没有在新的构造函数中挂接事件处理函数。 – Hogan

回答

5

你正在做的事情很奇怪,但回答你的问题是,你是在使用以表单作为参数的构造函数时不设置事件。你可以通过几种方法解决这个问题。您可以在无参数构造函数中复制代码,或者可以执行下列操作。注意构造函数之后的: this(),这将在它使用参数执行构造函数之前调用无参数构造函数。

另请注意,我拿出额外的InitializeComponent,因为它会在无参数的构造函数中执行。

public selectdb() 
{ 
    InitializeComponent(); 
    this.Load += new System.EventHandler(selectdb_Load); 
} 

private Form1 mainForm = null; 

public selectdb(Form callingForm) : this() 
{ 
    mainForm = callingForm as Form1; 
} 
+0

非常感谢。对不起,我确定这很奇怪。我只是在学习C#。 – Reg

1

我认为你需要改变你的新的构造函数来调用默认的构造函数,使一切都正确接线,无论你如何到达那里:

public selectdb(Form callingForm) : this() 
{ 
    mainForm = callingForm as Form1; 
} 
+1

默认的构造函数会自动调用基本的默认构造函数,不需要指定它。 –

+0

我没有意识到(我的背景是vb.net)。更新了答案。 –

0

的一个可能的原因:Form_Load事件没“T火

  1. 我做出释放路径集的引用
  2. 我删除参照,作出PROJEKT参照的文ce到我的程序集 - > form_load事件没有触发
  3. 我反转了Project引用,并使用Release路径 - > form_load事件再次执行程序集引用。