Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all

my ListBox Contain this items :
386-25
235-205
236-459

Now How do remove this character '_' then save each number to Variable ?

thanks all
Posted
Updated 8-Nov-13 11:43am
v2
Comments
Sergey Alexandrovich Kryukov 7-Nov-13 19:55pm    
The better question would be: how come this '_' character (actually your 3 lines show '-' not '_') in first place? :-)
—SA
anurag19289 8-Nov-13 12:09pm    
:)

I think you don't want to remove anything, you need to determine two separate numbers from the list box item. The solution is simple:
http://msdn.microsoft.com/en-us/library/ms131448%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/b3h1hf19%28v=vs.110%29.aspx[^]:
C#
string[] numbers = myItemText.Split('-', System.StringSplitOptions.RemoveEmptyEntries);
int left = int.Parse(numbers[0]);
int right = int.Parse(numbers[1]);

The only problem is: this is a bad dumb solution!

The real solution is to store proper data in your list items, not strings. (This is a curse of beginners these days: trying to work with strings representing data instead of data itself.) The type if the list item can be any object. Create a type which holds the data you need, which you could safely use later. For example:
C#
struct MyListItem {
    internal MyListItem(int left, int right) { this.Left = left; this.Right = right; }
    internal int Left { get; private set; }
    internal int Right { get; private set; }
    public override string ToString() { return string.Format("{0}-{1}", Left, Right); }
}

Why System.Object.ToString() is overridden here? Because this is what is shown in an item on screen. This way, you use original data, with no string parsing. Strings should be used only for output, presentation.

Now when you need this data, you case the item to the type MyListItem and directly access MyListItem.Left, MyListItem.Right.

—SA
 
Share this answer
 
v2
Comments
Thanks7872 7-Nov-13 23:11pm    
And this is what i should call smart solution. +5
Sergey Alexandrovich Kryukov 7-Nov-13 23:37pm    
Thank you, Rohan.
—SA
amirpooya 8-Nov-13 2:02am    
Thank you —SA ,very good but i want to move mouse by this cod :
Cursor.Position = new Point(int.Parse(txtx.Text), int.Parse(txty.Text));
Here Any time read one record of my listbox and send values for examp(235,453)to txtx.Text and txty.Text ...when position of my mouse chenged then read Second item of my list and update x,y for new position of mouse The next step
I hope I've explained
Sergey Alexandrovich Kryukov 8-Nov-13 2:16am    
I see no "but" here. Why "but"? I clearly answered your question, but now you are talking about something else.
If you understand the solution, please accept it formally. In all cases, your follow-up questions will be welcome, especially if you don't understand something. The solution have nothing to do with the application of it. What other problem do you see? How is your explanation related to the original question you posed?
—SA
amirpooya 8-Nov-13 2:42am    
The solution was found thanks
MY solution :

C#
string result = listBox1.SelectedItem.ToString();
           string[] s = result.Split(' ');
            foreach (string ss in s)
            {
                textBox1.Text = s.GetValue(0).ToString();
                textBox2.Text = s.GetValue(1).ToString();
            }


THANK AGAIN FROM —SA (;
 
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