Reputation: 11
I have one form (say form1
) that has 2 buttons (say button1
& button2
). When I click on button2
the same form i.e form1
should open but the condition is when form1
opens for the second time both button1
& button2
should get hide. How do I do that?
Upvotes: 0
Views: 225
Reputation: 23732
You could create a static
instance counter and increment it in the constructor. If this counter exceeds 1 then hide the buttons in the constructor:
public static int InstanceCount = 0;
public Form1()
{
InstanceCount++;
InitializeComponent();
if (InstanceCount > 1)
{
this.button1.Hide();
this.button2.Hide();
}
}
You could then reverse the process in the closing event and count backwards each time a Form1
is closed:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
InstanceCount--;
}
Upvotes: 0
Reputation: 9650
Simply hide both buttons when instantiating the form for the second time:
private void button2_Click(object sender, EventArgs e)
{
var secondForm1 = new Form1();
secondForm1.button1.Hide();
secondForm1.button2.Hide();
secondForm1.Show();
}
Upvotes: 2