Here's my code
Form1
TicketDetails td = new TicketDetails(getTicketID());
td.Show();
I want to close Form1 when td is shown.
If Form1 is the form you passed as an argument to Application.Run then you cannot close it, or your application will exit. What you can do though is hide the form when td is displayed.
TicketDetails td = new TicketDetails(getTicketID());
td.Show();
// Hide the main form
form1.Hide();
If Form1 is not your application's main form, then you may simply call Close() on form1.
Edit @comment,
public class Form1 : Form
{
private override OnLoad(object sender, EventArgs e) {
DoWork();
}
public void DoWork() {
// your code
}
}
Then a for-instance.
TicketDetails td = new TicketDetails(getTicketID());
td.Show();
form1.Hide();
// example, maybe you need to call your work method
form1.DoWork();
If you want to close the first form after the second has been displayed use the Form.Shown event:
td.Shown += (o, e) => { this.Close(); }
td.Show();
For other events that might be interesting, check out order of events in Windows Forms. And of course it might also be OK to close the current form immediately:
this.Close();
td.Show();
I've done something like this:
TicketDetails td = new TicketDetails(getTicketID());
td.Show();
this.Hide();
td.Close += (object sender, EventArgs e) =>
{
td.Parent.Close();
};
As long as TicketDetails isn't modal then any code after td.show() will run.
I'm also assuming that Form1 is the startup form for your project. If that's the case then using .close will stop your app.
have you tried this:
TicketDetails td = new TicketDetails(getTicketID());
td.Show();
Form1.Hide();
This obviously hides Form1
Thanks
Paul