C#, Load-Ereignis nicht auslösen.

Lerne ich C# und habe Probleme mit dem brennen der laden der system-Ereignis. Es war in Ordnung arbeiten, bis ich verbunden form1 und form2 zusammen zur übergabe von Variablen zwischen Ihnen. IE-legen Sie die label auf die Form einer aus dem ausgewählten Element auf form2. Vielen Dank für jede Hilfe!!!

Hier ist mein code für die 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; }
    }

}

}

Hier ist mein code für 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());

    }

}
}
  • Scheint klar-Sie sind nicht Einhaken in die event-handler in Ihre neue Konstruktor.
InformationsquelleAutor Reg | 2011-11-06
Schreibe einen Kommentar