Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I have loaded new form using this

Form3 newform = new Form3();
this.Hide();
newform.ShowDialog();
this.Show();


but my intention is to load this form if only value of combobox selected.... now this will work for just click search button.... that combobox is in form1

Can some one help me with coding?

Thanks
A-code
Posted
Comments
Sergey Alexandrovich Kryukov 9-Jan-14 23:44pm    
What prevents you from using "if" statement?!...
—SA

Your code, as I think you know, will not compile as it is, since 'SelectedValue is mis-spelled.

'SelectedValue is going to return something "meaningful" only if you are doing data-binding and have a valid 'ValueMember linked to the Control [^].

'SelectedItem.ToString() can be used to test the user's choice in the ComboBox, as can 'SelectedIndex.

Hiding a main form while you show another Form in 'Dialog mode is not a good idea; it's not the way users expect Windows Applications to behave.

I suggest:
"cs"
// declare and create your instance of Form3 once
private Form3 f3 = new Form3();

//somewhere in your code
if (comboBox1.SelectedItem == null) return;

switch (comboBox1.SelectedItem.ToString())
{
    case "choice 1":
        f3.ShowDialog();
        break;
    case "choice 2":
        // whatever ...
        break;
}
Or, (more efficient):
"cs"
switch (comboBox1.SelectedIndex)
{
    case -1:
        // no choice made
        break;
    case 0:
        // item 1 chosen
        f3.ShowDialog();
        break;
    case 1:
        // item 2 chosen
        // whatever ...
        break;
}
 
Share this answer
 
try something like this

C#
if(combobox.selectedvalue == "your desired value")
{
Form3 newform = new Form3();
this.Hide();
newform.ShowDialog();
this.Show();
}
 
Share this answer
 
Comments
Asa code 9-Jan-14 23:47pm    
after trying this code I have got
Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'

meantime "your desired value" instead of that can't we search by select value ?
ravikhoda 10-Jan-14 0:14am    
this is just to give hint that you can check condition if any value is selected in the combobox. you can navigate to other form if your condition is true based on your combobox value. otherwise u can show some message that please select value from the combobox. regarding the type string on left side you can write

combobox.selectedvalue.tostring();
Sergey Alexandrovich Kryukov 9-Jan-14 23:49pm    
Not only it demonstrate extremely bad style programming — "your desired value" would be an immediate constant, an unsupportable way of programming — but it won't even compile. Poor formatting, too. I understand that you are not very experience and not very careful developer, but then you need to test all the code you send to help someone.

Not answering is way better than answering this way. You need to feel some responsibility for the help you give. Here, the advice has purely negative value.

—SA
Asa code 10-Jan-14 0:40am    
if(cmbcode.SelectedValue>-1)
{
Form3 newform = new Form3();
this.Hide();
newform.ShowDialog();
this.Show();
}
I tried this way too but didn't work either :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900