| | |
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:
Solved Threads: 3
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.
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;
Yes you can create an array of textboxes.Look at the code below.
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).
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 :
But this will cause a little mess for 30 textboxes...
If the textbox positions are proportional you may solve this by using a loop...
C# Syntax (Toggle Plain Text)
public partial class Form1 : Form { TextBox[] txt = new TextBox[3]; //Creating an array of textbox //references NOT an array of textboxes. public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { for(int i = 0; i < txt.Length; i++) { txt[i] = new TextBox(); //Each textbox reference must show a //textbox... this.Controls.Add(txt[i]); //Add the current textbox to the //form. } }
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)
public partial class Form1 : Form { TextBox[] txt = new TextBox[3]; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { txt[0].Text = "something"; //These assignments will cause Null //Reference Exception because each of reference shows "null"; txt[1].Text = "something else"; ....... } }
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)
txt[0].Location = new Point(45,89); txt[1].Location = new Point(34,88); ......
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.
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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:
The results showed that TextBox2 contains the text I am looking for.
Hope this helps,
Jerry
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
•
•
Join Date: May 2007
Posts: 266
Reputation:
Solved Threads: 3
•
•
•
•
Yes you can create an array of textboxes.Look at the code below.
C# Syntax (Toggle Plain Text)
public partial class Form1 : Form { TextBox[] txt = new TextBox[3]; //Creating an array of textbox //references NOT an array of textboxes. public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { for(int i = 0; i < txt.Length; i++) { txt[i] = new TextBox(); //Each textbox reference must show a //textbox... this.Controls.Add(txt[i]); //Add the current textbox to the //form. } }
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)
public partial class Form1 : Form { TextBox[] txt = new TextBox[3]; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { txt[0].Text = "something"; //These assignments will cause Null //Reference Exception because each of reference shows "null"; txt[1].Text = "something else"; ....... } }
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)
txt[0].Location = new Point(45,89); txt[1].Location = new Point(34,88); ......
But this will cause a little mess for 30 textboxes...
If the textbox positions are proportional you may solve this by using a loop...
![]() |
Similar Threads
- Please help me :"login form use drag and drop toolbox" ! (C#)
- Help with asp form will not display contact information (ASP)
- errors in my file but not sure whats wrong file attatched (Visual Basic 4 / 5 / 6)
- Need someone create this source code (C)
- Pop3 Mail Watcher (Part 1) (Visual Basic 4 / 5 / 6)
- C++ Syntax (C++)
Other Threads in the C# Forum
- Previous Thread: SharpOS
- Next Thread: A Speaking APP in C# with .NET 3.0
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast buttons c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development draganddrop drawing encryption enum event excel file finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update upload usercontrol users validate validation visualstudio webbrowser windows winforms wpf xml





