Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

how to go to another form on mouse click of a combo box option in C#?

i want to show the user a different form when he selects one option in the combo box. i have tried the code below in the form load event but it didnt work.nothing happens when i select a option.however as i have set the selected index value to be 0 it loads that page directly as it is in form load.

  //this code is inside form 1 load method and as selected index is set to 1 it tries to load form1 again i guess so it gives the error too many items in combobox. however if i put form 2 to show when selected index =1 it directly loads form 2. i dnt want that happening i want it to load form 2 if the user selects form 2 option from combobox only


  if(comboBox1.SelectedIndex == 1)
            {
                
                this.Hide();
                form1 f1 = new form1();
                ha.Show();

            }
            else
            {
                this.Hide();
                form2 f2 = new  form2();
                f2.Show();
            }

 

Link to post
Share on other sites

You want another event for selectedindexchanged where you can put this if statement. I think you create it by double clicking on the combobox in the design. (I may be wrong here)

 

Also what's this about?

Quote

this.Hide();
                form1 f1 = new form1();
                ha.Show();

 

I edit my posts a lot.

Link to post
Share on other sites
9 hours ago, MrDrWho13 said:

You want another event for selectedindexchanged where you can put this if statement. I think you create it by double clicking on the combobox in the design. (I may be wrong here)

 

Also what's this about?

 

the combobox should have the options form1 and form2. when user clicks form1 it should load form1 again (i use it for like refreshing the page).if user clicks form2 he should be taken to form2. and oops i have accidentally put ha.show() instead of f1.show()

Link to post
Share on other sites
On 16.08.2017 at 6:00 PM, Shammikit said:

i want to show the user a different form when he selects one option in the combo box. i have tried the code below in the form load event but it didnt work.nothing happens when i select a option.however as i have set the selected index value to be 0 it loads that page directly as it is in form load.

 

Didn't I send you a code sample with opening new form? You had the same problem, but when clicking the button, the code would be the same, just a different event.

Try, fail, learn, repeat...

Link to post
Share on other sites
17 hours ago, Shammikit said:

when user clicks form1 it should load form1 again (i use it for like refreshing the page)

The code isn't "refreshing" the form. It's hiding the existing form, and creating another form1 and showing it. The original form1 is still there in the background.

this.Hide();  // The "this" keyword refers to the current form1. So you're hiding the current form1.
form1 f1 = new form1(); // Here you're creating a new form1 that is separate from the existing form1
f1.Show(); // Here you're showing the new form1 and the old form1 is just going to remain hidden in the background


// Side Note:

// Hide() is just setting the Visible property to false.
// So
this.Hide();
// is just a shorter way of writing
this.Visible = false;

// Show() is just setting the Visible property to true.
f1.Show();
// is just a shorter way of writing
f1.Visible = true;

So if you ran this code, you'd actually have 2 form1s open (1 invisible and 1 visible).

If you ran this code again, you'd actually have 3 form1s open (2 invisible and 1 visible).

And so on.

 

So you need to change how you "refresh" your form. Just update your existing form instead of creating a new form.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×