pulling a number out of a listbox item

how to convert the listbox item to a string, string into char, and det

Page 1 of 1

5 Replies - 1851 Views - Last Post: 07 January 2009 - 05:21 PM Rate Topic: -----

#1 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 16
  • View blog
  • Posts: 281
  • Joined: 27-February 08

pulling a number out of a listbox item

Posted 07 January 2009 - 01:54 PM

Ok, ive come across a snag in my code. I have a listbox filled with items (the number of items doesnt really matter). What im trying to do is pull the string out of the listbox, which i believe is:
 String contents = lstField.toString();
. The next step is to take that string and break it down into a char array which i believe i have to do it this way:
  char[] c = contents.ToCharArray();
. Once that is done, i need to search that char array to see if there is a numeric value in that array, and if so, make a integer equal to it. Unfortinatly i am having some trouble doing this, and im not sure if im supposed to break each char into an ASCII key or what? Heres a complete example to make things less confusing:
					 //my list box is called "lstField"

					//This is my code to make string contents equal to the current string the program is looking at
					String contents = lstField.ToString();
					
					//this is the char array that is equal to all the characters in the string contents
					char[] c = contents.ToCharArray();
					
					for (int i = 0; i < contents.lentgh;i++)
					{

						   if (char[i].hasInt)
												   {
														int holder = char[i];
													}
					 }


I would be very greatful if anyone could help me with my predictiment, and i give my thanks now to anyone that can offer any good information!

This post has been edited by painkiller102: 07 January 2009 - 01:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: pulling a number out of a listbox item

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: pulling a number out of a listbox item

Posted 07 January 2009 - 03:01 PM

I am going to assume that you mean you have items in the List like:

Hey There
123
You Stud



And you want to get the number out.

foreach (object s in listBox1.Items)
{
	int test;
	if (int.TryParse(s.ToString(), out test))
	{
		MessageBox.Show(test.ToString());
	}

}



is that what you are trying to do?

This post has been edited by eclipsed4utoo: 07 January 2009 - 03:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 16
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: pulling a number out of a listbox item

Posted 07 January 2009 - 04:30 PM

in a manner of speaking yes, however the number's are not on their own line, for example the list box could contain:
Billie10Sam
So in this case, i would want my integer to read that string, and extract the 10.
I hope this is more helpful, i'm sorry i did not explain it all to well
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: pulling a number out of a listbox item

Posted 07 January 2009 - 05:01 PM

this should work:

listBox1.Items.Add("Billie10Sam");
int tempNumber = 0;
int finalNumber = 0;
StringBuilder sb = new StringBuilder();

foreach (object o in listBox1.Items)
{
	string line = o.ToString();

	for (int i = 0; i < line.Length; i++)
	{
		char c = line[i];

		if (int.TryParse(c.ToString(), out tempNumber))
		{
			sb.Append(tempNumber);
		}
	}
}

finalNumber = int.Parse(sb.ToString());

MessageBox.Show(finalNumber.ToString());


This post has been edited by eclipsed4utoo: 07 January 2009 - 05:02 PM

Was This Post Helpful? 0
  • +
  • -

#5 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 16
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: pulling a number out of a listbox item

Posted 07 January 2009 - 05:16 PM

Thanks for the help! it almost works fully, the only thing that remains is that it searches all of the list for numbers in stead of one specific line, however i should be able to fix that! thanks so much for all the help!

This post has been edited by painkiller102: 07 January 2009 - 05:18 PM

Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: pulling a number out of a listbox item

Posted 07 January 2009 - 05:21 PM

View Postpainkiller102, on 7 Jan, 2009 - 07:16 PM, said:

Thanks for the help! it almost works fully, the only thing that remains is that it searches all of the list for numbers in stead of one specific line, however i should be able to fix that! thanks so much for all the help!


It doesn't look at a specific line....it just does one line. you can add each of the found integers into a List<int> and you should be good.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1