I have a main form that opens a second form like this
private void button2_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3();
form3.employeenumberToolStripTextBox.Text = employeeNumberTextBox.Text;
form3.Show();
form3.Owner = this;
}
When form3 opens, I have to click the enter button again (has the information from form1 textbox) to populate the form. How can I automatically have the click event activated when the form opens instead of having to click the enter button?
Does this make sense?
My form3 looks like this:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void sp_CLE_SelectRowBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.sp_CLE_SelectRowBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.employee_DataDataSet);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
this.sp_CLE_SelectRowTableAdapter.Fill(this.employee_DataDataSet.sp_CLE_SelectRow, new System.Nullable
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}
}
AlanPosted Aug 18, 2008, 3:53 PM
Assuming that button1 on Form3 is what you mean by the 'enter' button, then you could place a direct call to button1_Click in Form3_Load:
private
void Form3_Load(object sender, EventArgs e){
button1_Click(button1,
EventArgs.Empty);}
Alternatively, you could move the code in button1_Click into a separate method and then call that both from Form3_Load and button1_Click itself.
AlanPosted Aug 18, 2008, 6:19 PM
I mean that you could also have done it this way:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
PopulateForm(); // or whatever you want to call it
}
private void sp_CLE_SelectRowBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.sp_CLE_SelectRowBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.employee_DataDataSet);
}
private void button1_Click(object sender, EventArgs e)
{
PopulateForm();
}
private void PopulateForm()(((int)(System.Convert.ChangeType(employeenumberToolStripTextBox.Text, typeof(int))))));
{
try
{
this.sp_CLE_SelectRowTableAdapter.Fill(this.employee_DataDataSet.sp_CLE_SelectRow, new System.Nullable
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}
Provided another form has a Button called button1 and an eventhandler called button1_Click, then the same code will work on that form.
However, what it does will, of course, depend on what code you have in the button1_Click eventhandler itself.
If it's not working on Form2, then the most likely reason is that you've justed pasted in the button1_Click() method from Form3 rather than adding it via the VS designer. If you've done that then the event won't be wired up to the eventhandler and so will do nothing. To remedy the situation, place this line in Form2's constructor, after the call to InitializeComponent:
this.button1.Click += new System.EventHandler(this.button1_Click);
Derek WhartonPosted Aug 18, 2008, 4:18 PM
Alternatively, you could move the code in button1_Click into a separate method and then call that both from Form3_Load and button1_Click itself.
That suggestion you made worked great on 1 form; however, when I try it on another form, it does nothing. I tried this on my other form
private void Form2_Load(object sender, EventArgs e)
{
this.button1_Click(button1, EventArgs.Empty);
}
Will this work on multiple forms?
Derek WhartonPosted Aug 18, 2008, 3:58 PM