Reg
Reg

Reputation: 565

C# Load Event not Firing

I am learning C# and am having trouble with the firing of the load system event. It was working fine until I linked form1 and form2 together to pass variables between them. IE set the label on Form one from the selected item on form2. Thanks for any help!

Here is my code for 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; }
    }

}

}

Here is my code for 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());

    }

}
}

Upvotes: 5

Views: 10499

Answers (3)

Herbert
Herbert

Reputation: 1

A possible Reason for: form_load Event didn't fire

  1. I make a reference to an assembly in Release path
  2. I delete the reference and make a projekt reference to my assembly -> form_load Event didn't fire
  3. I reverse the Project reference and do the assembly reference with Release path -> form_load Event work again.

Upvotes: 0

competent_tech
competent_tech

Reputation: 44931

I think that you need to change your new constructor to call the default constructor so that everything is wired up correctly regardless how you got there:

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

Upvotes: 1

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

You're doing things very oddly, but the answer to your problem is that you are not setting the event when using the constructor that takes a form as the parameter. You can solve this in one of a couple ways. You could just duplicate the code in your parameterless constructor, or you can do something like the following. Note the : this() after constructor, this will call the parameterless constructor before it executes the constructor with the parameter.

Also note, i took out the extra InitializeComponent because it will get executed in the parameterless constructor.

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

private Form1 mainForm = null; 

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

Upvotes: 6

Related Questions