How to prevent several forms to be displayed

T

Tony Johansson

Hello!

My start form that is run from Program ccaled StartApp has two buttons
called Stock and Cash.
Yoe can see the event handler for Stock and Cash below.
If I click on Stock button then form Stock is displayed and if I click on
Cash button
then Cash form is displayed. So far so good.

Now to my little problem if I for example click on Stock button four times
then four Stock
forms will be displayed.

So I don't want to be able to start several Stock forms or Cash forms how is
that done.
If I need to start the Cash form I will have to close the Stock form how is
done done.

private void BtnStock_Click(object sender, EventArgs e)
{
new Stock().Show();
}

private void BtnCash_Click(object sender, EventArgs e)
{
new Cash().Show();
}

//Tony
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello!

My start form that is run from Program ccaled StartApp has two buttons
called Stock and Cash.
Yoe can see the event handler for Stock and Cash below.
If I click on Stock button then form Stock is displayed and if I click on
Cash button
then Cash form is displayed. So far so good.

Now to my little problem if I for example click on Stock button four times
then four Stock
forms will be displayed.

So I don't want to be able to start several Stock forms or Cash forms howis
that done.
If I need to start the Cash form I will have to close the Stock form how is
done done.

private void BtnStock_Click(object sender, EventArgs e)
{
       new Stock().Show();

}

private void BtnCash_Click(object sender, EventArgs e)
{
     new Cash().Show();

}

//Tony

Hi,

Instead of using Show() use ShowDialog()
 
P

Pavel Minaev

Hello!

My start form that is run from Program ccaled StartApp has two buttons
called Stock and Cash.
Yoe can see the event handler for Stock and Cash below.
If I click on Stock button then form Stock is displayed and if I click on
Cash button
then Cash form is displayed. So far so good.

Now to my little problem if I for example click on Stock button four times
then four Stock
forms will be displayed.

So I don't want to be able to start several Stock forms or Cash forms howis
that done.
If I need to start the Cash form I will have to close the Stock form how is
done done.

private void BtnStock_Click(object sender, EventArgs e)
{
       new Stock().Show();

}

private void BtnCash_Click(object sender, EventArgs e)
{
     new Cash().Show();

}

Store a reference to the form you open into a field. Check it for null
every time you open a new form - if it's not null, then there's a
previous form, and you Close() that before opening the new one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top