3 Replies - 15116 Views - Last Post: 15 December 2008 - 07:08 AM Rate Topic: -----

#1 Djanvk  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 14-December 08

Printing contents of an Array to a Textbox?

Post icon  Posted 15 December 2008 - 04:28 AM

I'm new to C# and have been using c++, I'm trying to take the contents of an array and send them to a textbox.

All I have it doing right now is displaying only the last element in the array I have and I want all of them to show up.

Thanks

private void button1_Click(object sender, EventArgs e)
		{
			string[] missingNumbers = txtKnown.Text.Split(' ');

			for (int i = 0; i < missingNumbers.Length; i++)
			{
				txtMissing.Text = missingNumbers[i];
			}

		}


Is This A Good Question/Topic? 0
  • +

Replies To: Printing contents of an Array to a Textbox?

#2 Sharkadder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-March 08

Re: Printing contents of an Array to a Textbox?

Posted 15 December 2008 - 04:52 AM

shouldn't you be telling your textbox to be += missingNumbers[i]? The reason it only displays the last element is because your telling the text to equal the last element in the list depending on it's length.

Your actual code is all correct, you will find it handy to of learnt C++ before hand but we all make basic syntax errors. You'll probably be kicking yourself because += is also available in C++ also.

If you want to display each element on a new line of the textbox, you know to put +"\n" after you tell txtmissing to equal missing numbers
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Printing contents of an Array to a Textbox?

Posted 15 December 2008 - 04:55 AM

The textbox needs to have it's Multiline TextBox.Multiline Property set to true.
Then you can add lines with txtMissing.Text += missingNumbers[i];

This post has been edited by n8wxs: 15 December 2008 - 04:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Printing contents of an Array to a Textbox?

Posted 15 December 2008 - 07:08 AM

for efficiency purposes, you should use a StringBuilder object instead of "+=" on strings.

ex:

private void button1_Click(object sender, EventArgs e)
{
	string[] missingNumbers = txtKnown.Text.Split(' ');
	StringBuilder sb = new StringBuilder();

	for (int i = 0; i < missingNumbers.Length; i++)
	{
		sb.Append(missingNumbers[i]);
	}

	txtMissing.Text = sb.ToString();

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1