2011-11-06 86 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事件再次執行程序集引用。