If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

is the a C# equivalent to TextBox(i).Text =

Hello everyone,

I am new here to this forum and to c#. I am a developer but i have now recently come across a problem. As far as i am aware VB has a function "TextBox(i).Text = ;". this allows a for loop to change lots of text boxes easily.

However i need an equivalent to that in c# as otherwise i have about 70 lines of code and it is much harder to use,

Re: is the a C# equivalent to TextBox(i).Text =

True, rarely is there a true need though some people would choose to do so.

In VB3 there was an actual need for it as you had a limit of objects you could have on a form but object arrays only counted as one object the result was that complex forms sometimes required the use of object arrays.

Re: is the a C# equivalent to TextBox(i).Text =

Thank you for the help everyone, Yes it is a COM conversation utility for a friend, you add your command in a list of text boxes and the data retreived is put into another, this is all done automatically. ok so i can use TextBox[i].Text = "whatever"; in my c# code?

Re: is the a C# equivalent to TextBox(i).Text =

As Arjay said, not without creating the array on your own first.
If DataMiser is right about the nature of your question, then you can see throughout the thread that, unlike VB, C# doesn't auto-generate this array for you. We usually don't use them [text box arrays] in this fashion, but if you feel you need to do this, here's a tip (this can go in the Form constructor, or maybe in the form's load-event handler):

Code:

// Assuming that all the textboxes are direct children to the containing form
// (i.e. some of them aren't on a special panel or some other container subelement)
List<TextBox> list = new List<TextBox>();
foreach(Control c in this.Controls)
{
//checks for a more specific type of c
if(c is TextBox)
list.Add(c);
}
// now, assuming you have a TextBox[] member variable called _textBoxes
_textBoxes = list.ToArray();

Re: is the a C# equivalent to TextBox(i).Text =

NullReferenceException:

The line:

TextBox[] textBoxArray = new TextBox[10];

creates a new, empty array of TextBox-es.

When you write "textBoxArray[i]", you are accessing an element of the array, however, all elements are null so far (equivalent to Nothing in VB.NET).
For "textBoxArray[i].Text = "whatever";" to be able to work, "textBoxArray[i]" has to return an existing TextBox object.
The statement is basically equivalent to textBoxAtLocation_i.Text = "whatever";.

So, you need to put all the textboxes in the array before you can set the Text property.
Wich brings me to your other question.

My example:
I forgot something. The Add() method expects a TextBox, yet the textboxes it gets are all treated as Control-s (Control is on the inheritance chain of TextBox). They need to be type-cast back to TextBox.

Code:

// Assuming that all the textboxes are direct children to the containing form
// (i.e. some of them aren't on a special panel or some other container subelement)
List<TextBox> list = new List<TextBox>();
foreach(Control c in this.Controls)
{
//checks for a more specific type of c
if(c is TextBox)
list.Add((TextBox)c);
}
// now, assuming you have a TextBox[] member variable called _textBoxes
_textBoxes = list.ToArray();

That should do the trick.
Note: You want to do this only once - at the start (assuming you're not adding more textboxes at runtime). Maybe in the constructor of your form.
For this to work, the _textBoxes array (or textBoxArray in your example needs to be a member field of the Form1 class, so that it can be acessible to any of its methods.
Also, it's even more convenient to use a list instead of an array, as you can dynamically add and remove elements form it. Just use it's Count property in for-loops, or simply use foreach.

Re: is the a C# equivalent to TextBox(i).Text =

Yeh, i think this may bring problems, as all the text boxes are in panels which are inside another panel in the form. Will it still be feasible. I am just trying to find the easiest way of doing this and am open to suggestions.

Re: is the a C# equivalent to TextBox(i).Text =

Don't worry, just a few modifications. Just to make sure - this is WinForms? All controls derive from the Control class, and thus inherit it's interface. The Control class has a Boolean property called HasChildren - it returns true if the actual type of the control is some sort of a container and has child elements. Also, each control has the Controls property (just like the Form class), that returns the child control collection.

So, this begs for recursion. Your code will start with the form and go down the control tree. The base case is if the current control is not a container (like a Button, or a TextBox). The general case would be if it is (like a Panel). So you can do something like this: