Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have searched and searched but cant see to find an answer to my problem. I want to get the selected text from a combobox on form1 and use it to search in a file from a class file. an example of what im looking to do is
C#
if (line.Contains(Form1.Combobox1.SelectedItem))
  {
    /*search code here*/
  }


where Combobox1.SelectedItem is what was chose on form1. I'm new to C# and could accomplish this in Vb but am wanting to learn a new language.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Dec-11 20:57pm    
Do you know how to do it in VB or in VB.NET? If VB.NET, with C# this is exactly the same.
--SA
Sergey Alexandrovich Kryukov 19-Dec-11 20:58pm    
What is "class file"?
--SA

The question is not clear; and it is not clear why do you use Contains. It looks like you just need a text of a selected item:
C#
object selectedItem = Form1.Combobox1.SelectedItem;
if (selectedItem != null) { //important check
    string selectedText = selectedItem.ToString();
    //...
}


Please do yourself a favor: never leave ugly auto-generated names like Form1, Combobox1, renamed all of them to give them some semantic names. Those auto-generated names even violate Microsoft naming conventions — test your assembly by FxCop to see. Why do you thing you have a refactoring engine?

—SA
 
Share this answer
 
A bit more searching and i was able to find a way to work out the code. as i said i'm still learning so what i muddle though at this point is dirty at best, but i am getting the gist of it. i wont post my fix cause as i said it's real dirty.

@SAKryukov
thanks for the direction. the auto-generated MS names were just an example of what i was wanting to do(thought the type of control would be relevant to the answer). the "class file" i was referring to was when you add a "Class" to the project so all my code would not be in the "Form" file. As for FxCop i seem to have a problem installing it but thats another topic. As for the vb or vb.net, i thought vb.net but after looking into the differences between the 2 im gonna have to say just plain-old vb.
 
Share this answer
 
Use this

C#
string a=Form1.Combobox1.Text; //Here you will get the selected text that you have choose in comboBox 
//Hope this will help you


then you can assign the 'a' variable to your search class
 
Share this answer
 

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