blah blah blah is here! blah blah » Close

up0down
link

Hey guys
i am new to Visual C#, and would like to get some help.
I have two forms, Form1 and Form2
Form1 has a comboBox where numbers can be selected. and Form2 has a bunch of buttons which are disabled.
Once i select a number from the ComboBox on Form1, i want Form2 to pop up with ONLY the selected button being enabled.
I cannot get to make this happen. I somehow need to connect Form1 and Form2, I guess
Thanks a lot in advance =)

last answered 2 years ago

1 answers

up0down
link

Try something like the following:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Form2 f2 = (Form2)Application.OpenForms["Form2"];
if (f2 == null)
{
f2 = new Form2();
f2.Show();
}
else
{
if (!f2.Visible) f2.Visible = true;
}
int number = int.Parse(comboBox1.SelectedItem.ToString());
switch (number)
{
case 1:
f2.Controls["button1"].Enabled = true;
f2.Controls["button2"].Enabled = false;
f2.Controls["button3"].Enabled = false;
return;
case 2:
f2.Controls["button2"].Enabled = true;
f2.Controls["button1"].Enabled = false;
f2.Controls["button3"].Enabled = false;
return;
case 3:
f2.Controls["button3"].Enabled = true;
f2.Controls["button1"].Enabled = false;
f2.Controls["button2"].Enabled = false;
return;
}
}

up0down
link

Hey
Thanks for the code, it does what i want it to do, but by any chance is it possible that the form doesnot show up first ..
i have the combo-box, so when i select the number of buttons, i want to be able to hit "ENTER" button (which is on the board) before the form shows up. I dont want the Form2 to show up immediately after a selection change in the combobox.
Thanks
Muhu

up0down
link

In that case, just copy the code to the Click eventhandler for your button and delete it from the comboBox1_SelectedIndexChanged eventhandler.
Also add this line above the other code:
if (comboBox1.SelectedIndex == -1) return;
That will avoid an exception being thrown if no item has been selected in the combobox when the button is pressed.

up0down
link

Hey
Thanks a lot!
Okay, if i wish to inactivate a form when i have moved on to the next form then how can i do it?
Plus
If i want to inactivate a button once its clicked, how can i do that?
Thanks
Muhu

up0down
link

Well, in Windows only one form can be active at a time so, if you change the input focus from one form to another, then the first form will automatically deactivate.
To disable a button (button1 say) when it's clicked, place this line in its Click eventhandler:
button1.Enabled = false;

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback