create textbox in code and set it equal to one on form.

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2007
Posts: 266
Reputation: quintoncoert is an unknown quantity at this point 
Solved Threads: 3
quintoncoert quintoncoert is offline Offline
Posting Whiz in Training

create textbox in code and set it equal to one on form.

 
0
  #1
Jul 12th, 2007
Can someone please tell me what is wrong with the following code or if it is even legal? I am trying to access the text in a multitude of textboxes without having to go to each textbox manually. so i named them all similar enough. their names are all variations of
txtSG1D1
with only the numbers varying. the problem here is that s2 cannot be equal to the text of curText. I get an error message that seems to imply that curText is not a real object. I must create an instance by using keyword new.

Or is there some other way of creating an array of controls. I really do not want to resign myself to the fate of having to code 30 textboxes by hand.

Thanks.

 
TextBox curText = newTextBox();
curText = (TextBox) this.Controls["txtSG" + x.ToString() + "D" + y.ToString()];
 
s2 = curText.Text;
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 53
Reputation: FoX_ is an unknown quantity at this point 
Solved Threads: 7
FoX_'s Avatar
FoX_ FoX_ is offline Offline
Junior Poster in Training

Re: create textbox in code and set it equal to one on form.

 
0
  #2
Jul 12th, 2007
Yes you can create an array of textboxes.Look at the code below.

  1.  
  2. public partial class Form1 : Form
  3. {
  4. TextBox[] txt = new TextBox[3]; //Creating an array of textbox //references NOT an array of textboxes.
  5.  
  6. public Form1()
  7. {
  8. InitializeComponent();
  9. }
  10.  
  11. private void Form1_Load(object sender, EventArgs e)
  12. {
  13.  
  14.  
  15. for(int i = 0; i < txt.Length; i++)
  16. {
  17. txt[i] = new TextBox(); //Each textbox reference must show a //textbox...
  18.  
  19. this.Controls.Add(txt[i]); //Add the current textbox to the //form.
  20.  
  21. }
  22.  
  23.  
  24. }

Now why we wrote txt[i] = new TextBox(); ???
Because when you initialize the array of textbox references , each reference shows "null".So when you write txt[i].Text = "something" without write txt[i] = new TextBox(); it will cause an error(Null reference exception).

  1. public partial class Form1 : Form
  2. {
  3. TextBox[] txt = new TextBox[3];
  4.  
  5. public Form1()
  6. {
  7. InitializeComponent();
  8. }
  9.  
  10. private void Form1_Load(object sender, EventArgs e)
  11. {
  12. txt[0].Text = "something"; //These assignments will cause Null //Reference Exception because each of reference shows "null";
  13. txt[1].Text = "something else";
  14. .......
  15. }
  16. }

Note that all of the textboxes have the same location on the form when they were added to the form.You must change their location with the code below :

  1.  
  2. txt[0].Location = new Point(45,89);
  3. txt[1].Location = new Point(34,88);
  4. ......

But this will cause a little mess for 30 textboxes...
If the textbox positions are proportional you may solve this by using a loop...
Last edited by FoX_; Jul 12th, 2007 at 12:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: create textbox in code and set it equal to one on form.

 
0
  #3
Jul 13th, 2007
If I understand you right, you have some text controls on the form, and you want to iterate through each to look at their text value.

Since all of them are already in the controls array, you can just iterate through them. You can rely on the name, or set the Tag value of those you want to search (from the designer IDE).

Your first line of TextBox curText = new TextBox() should really be set to null, unless you are really trying to create a new control on the form.

As a sample, I created a new project, dropped three TextBox controls and a button. In the second control I set the Text property to "Hello World" then used this code in the button handler:
foreach (Control ctrl in this.Controls)
{
if (ctrl is TextBox)
{
if (ctrl.Text == "Hello World")
{
MessageBox.Show(ctrl.Name + " has this value.");
break;
}
}
}

The results showed that TextBox2 contains the text I am looking for.

Hope this helps,
Jerry
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 266
Reputation: quintoncoert is an unknown quantity at this point 
Solved Threads: 3
quintoncoert quintoncoert is offline Offline
Posting Whiz in Training

Re: create textbox in code and set it equal to one on form.

 
0
  #4
Jul 16th, 2007
Originally Posted by FoX_ View Post
Yes you can create an array of textboxes.Look at the code below.

  1.  
  2. public partial class Form1 : Form
  3. {
  4. TextBox[] txt = new TextBox[3]; //Creating an array of textbox //references NOT an array of textboxes.
  5.  
  6. public Form1()
  7. {
  8. InitializeComponent();
  9. }
  10.  
  11. private void Form1_Load(object sender, EventArgs e)
  12. {
  13.  
  14.  
  15. for(int i = 0; i < txt.Length; i++)
  16. {
  17. txt[i] = new TextBox(); //Each textbox reference must show a //textbox...
  18.  
  19. this.Controls.Add(txt[i]); //Add the current textbox to the //form.
  20.  
  21. }
  22.  
  23.  
  24. }

Now why we wrote txt[i] = new TextBox(); ???
Because when you initialize the array of textbox references , each reference shows "null".So when you write txt[i].Text = "something" without write txt[i] = new TextBox(); it will cause an error(Null reference exception).

  1. public partial class Form1 : Form
  2. {
  3. TextBox[] txt = new TextBox[3];
  4.  
  5. public Form1()
  6. {
  7. InitializeComponent();
  8. }
  9.  
  10. private void Form1_Load(object sender, EventArgs e)
  11. {
  12. txt[0].Text = "something"; //These assignments will cause Null //Reference Exception because each of reference shows "null";
  13. txt[1].Text = "something else";
  14. .......
  15. }
  16. }

Note that all of the textboxes have the same location on the form when they were added to the form.You must change their location with the code below :

  1.  
  2. txt[0].Location = new Point(45,89);
  3. txt[1].Location = new Point(34,88);
  4. ......

But this will cause a little mess for 30 textboxes...
If the textbox positions are proportional you may solve this by using a loop...
thank you. it was most helpfull. there still is some issues but they involve logic and i shal be able to deal with them.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC