944,160 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 7740
  • C# RSS
Jul 12th, 2007
0

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

Expand Post »
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;
Similar Threads
Reputation Points: 160
Solved Threads: 3
Posting Whiz in Training
quintoncoert is offline Offline
266 posts
since May 2007
Jul 12th, 2007
0

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

Yes you can create an array of textboxes.Look at the code below.

C# Syntax (Toggle Plain Text)
  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).

C# Syntax (Toggle Plain Text)
  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 :

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
FoX_ is offline Offline
53 posts
since Mar 2007
Jul 13th, 2007
0

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

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
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Jul 16th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by FoX_ ...
Yes you can create an array of textboxes.Look at the code below.

C# Syntax (Toggle Plain Text)
  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).

C# Syntax (Toggle Plain Text)
  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 :

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 160
Solved Threads: 3
Posting Whiz in Training
quintoncoert is offline Offline
266 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: SharpOS
Next Thread in C# Forum Timeline: hexadecimal convertions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC