Array of TextBoxes

How to use a loop to extract text from a list of textboxes

Page 1 of 1

13 Replies - 17795 Views - Last Post: 28 October 2010 - 04:28 AM Rate Topic: -----

#1 Jayboss  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 06-May 09

Array of TextBoxes

Post icon  Posted 06 May 2009 - 12:42 AM

Hey so i have a group of textboxes named textBox1, textBox2, etc and i want to go through a loop extracting all the text from textboxes and placing it into a equivalent array. Here is the code i have at the moment but whenever i run it it says Null Reference exception. Can someone help editing this code or got another way to do this? Thanks in advance

		
 private void button1_Click(object sender, EventArgs e)
		{
			int[] value = new int[82];
			for (int x = 1; x < 4; x++) 
			{
				TextBox inputTextBox = (TextBox) this.Controls["textBox" + x.ToString()];
				value[x] = Convert.ToInt32(inputTextBox.Text);
			}
			
		}


This post has been edited by Jayboss: 06 May 2009 - 12:46 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Array of TextBoxes

#2 beatles1692  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 62
  • Joined: 03-December 08

Re: Array of TextBoxes

Posted 06 May 2009 - 02:22 AM

Hi
Here's my suggestion:
You can loop through all controls of your form find textboxes and add their text value to a list:
private static int[] function GetTextBoxValuesArray(Form form)
{
var result=new List<int>();
foreach(Control ctrl in Form.Controls)
{
if(ctrl is TextBox)
{
int value;
if(int.TryParse(ctrl.Text,out value)
{
result.Add(value);
}
else
result.Add(0);//A value to show the input value is can't be parsed to an int.
}
return result.ToArray();
}
}



This solution has a problem that returns the values of all textboxes on your form as an integer.If you'd like to specify some textboexs of your form you can use a metadata that help you select these textboxes (use Tag property of textbox or an attribute to mark a textbox for example) or you can inherit a custom text box and check for its type.
Was This Post Helpful? 0
  • +
  • -

#3 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Array of TextBoxes

Posted 06 May 2009 - 05:38 AM

I did something similar only instaed of textboxes I used Labels. Just change where it says label to textbox.

How many textboxes do you want to cycle through? Change the number 48 in the for loop, to the number of textboxes you have.

for (int i = 1; i <= 48; i++)
{
	TextBox textBox = (TextBox)Controls["textBox" + i];
	textBox.Text = "blank" // Place what you want to do to every textbox here.
}



This post has been edited by papuccino1: 06 May 2009 - 05:39 AM

Was This Post Helpful? 1

#4 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Array of TextBoxes

Posted 06 May 2009 - 08:36 AM

Hello,

Another easy way would be to store all your text boxes in a array or list, then iterate through each text box and add the text to another list:

List<int> myList = new List<int>();
TextBox[] textBoxes = { textBox1, textBox2, textBox3, textBox4 };

for (int i = 0; i < textBoxes.Length; i++)
{
	 list.Add(Convert.ToInt32(textBoxes[i].Text));
}




Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#5 Jayboss  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 06-May 09

Re: Array of TextBoxes

Posted 06 May 2009 - 11:45 PM

Thanks all got it working now! :D
Was This Post Helpful? 0
  • +
  • -

#6 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

Re: Array of TextBoxes

Posted 09 May 2009 - 11:57 AM

View Postpapuccino1, on 6 May, 2009 - 04:38 AM, said:

I did something similar only instaed of textboxes I used Labels. Just change where it says label to textbox.

How many textboxes do you want to cycle through? Change the number 48 in the for loop, to the number of textboxes you have.

for (int i = 1; i <= 48; i++)
{
	TextBox textBox = (TextBox)Controls["textBox" + i];
	textBox.Text = "blank" // Place what you want to do to every textbox here.
}





If I try your method I get following error:

Object reference not set to an instance of an object.
Was This Post Helpful? 1
  • +
  • -

#7 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Array of TextBoxes

Posted 09 May 2009 - 03:02 PM

View Postwuu, on 9 May, 2009 - 12:57 PM, said:

View Postpapuccino1, on 6 May, 2009 - 04:38 AM, said:

I did something similar only instaed of textboxes I used Labels. Just change where it says label to textbox.

How many textboxes do you want to cycle through? Change the number 48 in the for loop, to the number of textboxes you have.

for (int i = 1; i <= 48; i++)
{
	TextBox textBox = (TextBox)Controls["textBox" + i];
	textBox.Text = "blank" // Place what you want to do to every textbox here.
}





If I try your method I get following error:

Object reference not set to an instance of an object.


Well, do you have 48 text boxes on your form?

This post has been edited by lesPaul456: 09 May 2009 - 03:03 PM

Was This Post Helpful? 0
  • +
  • -

#8 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

Re: Array of TextBoxes

Posted 09 May 2009 - 04:16 PM

No I have three. But this doesn't mater, because I get error at first irritation of a for loop, which I set it like that:

for (int i = 1; i < 4; i++)
{
	  TextBox textBox = (TextBox)Controls["textBox" + i];
	   textBox.Text = "some text" // Place what you want to do to every textbox here.
}


Was This Post Helpful? 0
  • +
  • -

#9 fremgenc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 119
  • Joined: 15-November 07

Re: Array of TextBoxes

Posted 10 May 2009 - 03:16 PM

are you using your code before the text boxes are initialized?

and its iteration, not irritation lol
Was This Post Helpful? 0
  • +
  • -

#10 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Array of TextBoxes

Posted 10 May 2009 - 05:15 PM

View Postwuu, on 9 May, 2009 - 05:16 PM, said:

No I have three. But this doesn't mater, because I get error at first irritation of a for loop, which I set it like that:

for (int i = 1; i < 4; i++)
{
	  TextBox textBox = (TextBox)Controls["textBox" + i];
	   textBox.Text = "some text" // Place what you want to do to every textbox here.
}



Try using this code:

for (int i = 1; i < 4; i++)
{
    TextBox.textBox = typeof(TextBox)Controls["textBox" + i.ToString()];
    textBox.Text = "some text";
}


This post has been edited by SixOfEleven: 10 May 2009 - 05:16 PM

Was This Post Helpful? 0
  • +
  • -

#11 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

Re: Array of TextBoxes

Posted 11 May 2009 - 07:30 AM

I'm sorry I forgot to add that my Text Boxes are positioned inside tabPage1 on Form1
Was This Post Helpful? 0
  • +
  • -

#12 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Array of TextBoxes

Posted 11 May 2009 - 08:46 AM

Should be an easy fix...

for (int i = 1; i < 4; i++)
{    
    TextBox.textBox = typeof(TextBox)tabPage1.Controls["textBox" + i.ToString()];    
    textBox.Text = "some text";
}




:)


Six
Was This Post Helpful? 0
  • +
  • -

#13 rajavelugold  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-October 10

Re: Array of TextBoxes

Posted 27 October 2010 - 11:19 PM

View Postwuu, on 09 May 2009 - 10:57 AM, said:

View Postpapuccino1, on 6 May, 2009 - 04:38 AM, said:

for (int i = 1; i <= 48; i++)
{
	TextBox textBox = (TextBox)Controls["textBox" + i];
	textBox.Text = i.tostring() // Place what you want to do to every textbox here.
}





If I try your method I get following error:

Object reference not set to an instance of an object.



i used group box to place the textboxes so only i got this error.

i just place the textboxes in the form it works..

how to resolve the error when place in group box or panel

Object reference not set to an instance of an object.
Was This Post Helpful? 0
  • +
  • -

#14 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

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

Re: Array of TextBoxes

Posted 28 October 2010 - 04:28 AM

View Postrajavelugold, on 28 October 2010 - 02:19 AM, said:

View Postwuu, on 09 May 2009 - 10:57 AM, said:

View Postpapuccino1, on 6 May, 2009 - 04:38 AM, said:

for (int i = 1; i <= 48; i++)
{
	TextBox textBox = (TextBox)Controls["textBox" + i];
	textBox.Text = i.tostring();
}





If I try your method I get following error:

Object reference not set to an instance of an object.



i used group box to place the textboxes so only i got this error.

i just place the textboxes in the form it works..

how to resolve the error when place in group box or panel

Object reference not set to an instance of an object.


you would use this instead...

for (int i = 1; i <= 48; i++)
{
        // changed this line to use the GroupBox instead
	TextBox textBox = (TextBox)groupBox1.Controls["textBox" + i];
	textBox.Text = i.tostring() // Place what you want to do to every textbox here.
}



You need to loop through the controls of the groupbox/panel.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1