I have an issue in showing a timer when i come back tio my main form from another
the main form have the following code
public Main()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
timeLabel.Text = DateTime.Now.ToString("dd/MMM/yyyy HH:mm");
}
private void GestioneSerataButton_Click(object sender, EventArgs e)
{
this.Hide();
GestioneSerataForm gfor = new GestioneSerataForm();
gfor.Show();
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
now if i click the button in order to go to GestioneSerataForm it’s ok.
when i try to close GestioneSerataForm with the following code
private void GestioneSerataForm_FormClosing(object sender, FormClosingEventArgs e)
{
Main main = new Main();
this.Close();
main.Show();
}
i have System.StackOverflowException in Main.Designer related to Controls.Add(timeLabel);
how can i fix that?
My necessity is only to show the current timer and nothing else
i have tried to start and stop the timer an put it public in order to restart it in GestioneSerataForm but it doesn’t work
Pietro Zunino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Your Main
form creates and shows a GestioneSerataForm
form:
GestioneSerataForm gfor = new GestioneSerataForm();
gfor.Show();
And your GestioneSerataForm
form creates and shows a Main
form:
Main main = new Main();
//...
main.Show();
This would happen indefinitely, each form creating the other over and over.
Instead of creating a new Main
form, supply the GestioneSerataForm
form instance with a reference back to the Main
form you already have. For example:
// in GestioneSerataForm.cs
private Main _main;
public GestioneSerataForm(Main main)
{
_main = main;
}
and:
// in Main.cs
private void GestioneSerataButton_Click(object sender, EventArgs e)
{
this.Hide();
GestioneSerataForm gfor = new GestioneSerataForm(this);
gfor.Show();
}
Note the reference to this
used when calling the GestioneSerataForm
constructor.
Then you can use that reference to show the original Main
form instance:
private void GestioneSerataForm_FormClosing(object sender, FormClosingEventArgs e)
{
_main.Show();
}