Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
comboBox1.Items.Add(" 1 second");
comboBox1.Items.Add("2 second");
added to combobox .
1)if i select 1 second from combobox .1 should be displayed in textbox provided how can i do this .

2) Another based on textbox text such as 1 how can i fetch combox items
and display the selected item
Posted
Updated 23-Feb-11 4:24am
v3

One of many options: find Regex match of "[0-9]+"; read the help on System.Text.RegularExpressions.Regex for instructions.

—SA
 
Share this answer
 
Comments
Espen Harlinn 25-Feb-11 6:54am    
Nice and simple - my 5
Sergey Alexandrovich Kryukov 25-Feb-11 19:26pm    
Thank you.
--SA
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  string[] splitted = comboBox1.Text.Split(' ');
  if (splitted.Length > 1)
    textBox1.Text = splitted[0];
}
 
Share this answer
 
Comments
Espen Harlinn 25-Feb-11 6:57am    
Did not deserve a downvote - my 5, even if I like SAKryukovs regex based solution somewhat better :)
Sergey Alexandrovich Kryukov 25-Feb-11 19:19pm    
I agree. A little problem is not the algorithm itself: this method assumes a more about the string than it is minimally needed, therefore, I think my vote of 4 is just right.
--SA
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textbox1.text=comboBox1.Text.Split(' ')[0];
}
 
Share this answer
 
v3
Comments
yeshgowda 23-Feb-11 6:33am    
ThanQ this code will work fine with minimum number of lines.
You may try this
string s = (string)comboBox1.SelectedItem;
s = s.Substring(0, s.IndexOf(" "));
int i =Convert.ToInt32(s);
textBox1.Text = i.ToString()
 
Share this answer
 
v2
The process of adding dataitems itself is wrong.
While inserting, do this:

C#
comboBox1.Items.Insert(0,new ListItem("1 Second","1");
comboBox1.Items.Insert(1,new ListItem("2 Second","2");



This way you can easily fetch corresponding values for selected items:

C#
textBox1.Text= comboBox1.SelectedItem.Value;


Hope this helps.
(I haven't checked the code on code editor, so may contain syntactical errors, otherwise logic will always be the same)
Anurag :rose:
 
Share this answer
 
v6
Comments
Sergey Alexandrovich Kryukov 25-Feb-11 19:25pm    
This improvement is absolutely pointless! If you need semantic object it list view (and usually you do), it can be any type with overridden ToString. (Did you know that?) In this case, non-string item makes sense and very beneficial. ListItem type adds not value.
--SA
@nuraGGupta@ 26-Feb-11 10:42am    
Although, I didn't used any help from net for providing this answer, but check this link http://community.devexpress.com/forums/p/74796/255802.aspx.

Also, you may get many similar examples regarding this. Next time please think twice before providing comments or negative votes. You may be very sure about what you know, but never be sure about what you don't know.
Sergey Alexandrovich Kryukov 26-Feb-11 13:45pm    
I agree I should be very careful. Your new reference is also not correct argument; the code you reference is valid, so what? I already explained what do to. Sorry.
--SA
Assuming it's '1 second', '2 second'....
Get the selected value and then split it using space, use the first value of split.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Feb-11 19:21pm    
This is essentially the same as the Answer by JF2015, so I voted the same way (see my comment).
I disagree with the vote of "3" though.
--SA
koool.kabeer 26-Feb-11 11:39am    
i didnt expect that sol from sandeep, its System.Text.RegularExpression.Regex and its method "Replace"
Sandeep Mewara 26-Feb-11 11:41am    
Appologies sir!

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