CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Hybrid View

  1. #1
    Join Date
    Jul 2011
    Posts
    5

    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,

    Thanks everyone for you time,

    Dave

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

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

    Collection element access?
    TextBox[i].Text = "whatever";

    The same syntax is used to declare an array.
    TextBox[] textBoxArray = new TextBox[count];
    Last edited by TheGreatCthulhu; July 10th, 2011 at 06:51 AM.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

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

    UI's with lots of text boxes aren't allowed in C# and indicate a UI design issue.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

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

    In VB6 Text1(x).Text is a common sight as when you create a textbox control, copy and paste it will automatically create a textbox array for you which will behave as in the OP.

    VB.Net no longer does this nor does C#, instead you must create an array if you want to do this.

    I wonder what would be a "lot" of text boxes? It is not that uncommon to need 10 or 20 on an input form.
    Always use [code][/code] tags when posting code.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

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

    Quote Originally Posted by DataMiser View Post
    It is not that uncommon to need 10 or 20 on an input form.
    It's uncommon toneed to store them in an array.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Jul 2011
    Posts
    5

    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?

    Again thanks for the help everyone

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

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

    Quote Originally Posted by micr0man View Post
    ok so i can use TextBox[i].Text = "whatever"; in my c# code?
    No. Reply #2 gave you the answer on how to create a textbox array.
    Code:
    TextBox[] textBoxArray = new TextBox[count];
    from there you can access the Text property of each TextBox item in the array with:
    Code:
    textBoxArray[i].Text = "whatever";
    Of course, you'll need to initially loop through the array and position the textboxes as the form loads.

  9. #9
    Join Date
    Jan 2010
    Posts
    1,133

    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();

  10. #10
    Join Date
    Jul 2011
    Posts
    5

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

    sorry i do not think i understand. I entered this code into a c# application:
    Code:
    namespace test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                TextBox[] textBoxArray = new TextBox[10];
                for (int i = 1; i < 5; i++)
                {
                    textBoxArray[i].Text = "whatever";
                }
            }
        }
    }
    but i get a nullreferenceexeption error.

    TheGreatCthulhu the code you gave, gave me two errors:
    Error 1 The best overloaded method match for 'System.Collections.Generic.List<System.Windows.Forms.TextBox>.Add(System.Windows.Forms.TextBox)' has some invalid arguments C:\Users\Dave\AppData\Local\Temporary Projects\test\Form1.cs 26 21 test
    Error 2 Argument 1: cannot convert from 'System.Windows.Forms.Control' to 'System.Windows.Forms.TextBox' C:\Users\Dave\AppData\Local\Temporary Projects\test\Form1.cs 26 30 test

  11. #11
    Join Date
    Jan 2010
    Posts
    1,133

    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.
    Last edited by TheGreatCthulhu; August 6th, 2011 at 07:32 AM.

  12. #12
    Join Date
    Jul 2011
    Posts
    5

    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.

  13. #13
    Join Date
    Jan 2010
    Posts
    1,133

    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:

    Code:
    // the recursive method...
    private List<TextBox> GetTextBoxesFrom(Control parent)
    {
        List<TextBox> result = new List<TextBox>();
        List<TextBox> indirectChildren = null;
        foreach (Control c in parent.Controls)
        {
            //check for the base case
            if(c is TextBox)
                result.Add((TextBox)c);
            else if (c.HasChildren)    // it's a container
            {
                indirectChildren = GetTextBoxesFrom(c);
                result.AddRange(indirectChildren);
            }
        }            
        return result;
    }
    
    // And then just call it in the constructor.
    // _textBoxList is a member field of type List<TextBox>
    _textBoxList = GetTextBoxesFrom(this);   // (Form inherits Control, too.)
    It's not the most efficient way to do it, but I hope you get the idea.
    Last edited by TheGreatCthulhu; August 7th, 2011 at 05:54 PM. Reason: a correction

  14. #14
    Join Date
    Dec 2012
    Posts
    1

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

    Hy all, i new in C# too

    i work at a real state company. how can I create a form to save all the rooms at the same time without creating an array like mentioned ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured