hi
i have 90 textBoxes at my program and i want to insert them for my DataBase (sql)
how can i do that?
i know that i can to do array like -> TextBox[] arr = {textBox1,texBox2......textBox(n)};
but i dont wanna this way cause that a lot of work.
how can i do that in another way and easy?
thanks...

Recommended Answers

All 11 Replies

Try doing a loop through foreach control of the form:

private void Insert()
        {
            foreach (Control c in this.Controls)
            {
                if (c is TextBox && c != null)
                {
                    string valueOfTextBox = c.Text;
                }
            }
        }

@Mitja
Right, but you would have the textboxes in the order they are added to the Controls collection of the form, is that always OK?

You'd also miss textboxes that are in containers. And with 90 textboxes on a form, I hope he has them organized in some way!

BTW, why do you have 90 textboxes on a form? That's about 80 too many for standard UI design guidelines.

Momerath, I dont know his code, so I cannot say how how would access to all the wanted control. I was (am) only showing some other way how to access to controls, expecially if there is a lot of them in use.
My code will get all the textBoxes (except those which are in containers -as Momerath stated) which are on the form. But that is his decision what to do, or he gets all the wanted TextBoxes into an array, like he shows in his code snipet, or he loops through all of them, like I showed in my code snpiet.
If he uses my code, and there are some other textBoxes on the form, which he doesn`t want to be included in the loop he can somehow exclude it. For example, if a loop checking if the current textBox name is one of those he doesn`t want:

//lets say he has 2 testBoxes (with names "Unwanted1, aUnwanted2", which he wants to be excluded from the code:
        private void Insert()
        {
            foreach (Control c in this.Controls)
            {
                if (c is TextBox && c != null)
                {
                    string valueOfTextBox =null;
                    if (c.Name != "textBoxUnvanted1" && c.Name != "textBoxUnvanted2")
                        valueOfTextBox = c.Text;
                }
            }
        }

Still better way then declaring 90 textBoxes in the array, like he showed on beginning (my opinion).

PS: I am also wondering why does he use 90!!! textBoxes on the form. This is way to much. This is a pure mess. But thats again his problem...
Hope it helps.

i have 90 textboxes in tabControl -> (tabPage1, tabPage2, tabPage3)

and this code doesnt work!

foreach (Control c in this.Controls)
            {
                if (c is TextBox && c != null)
                {
                    string valueOfTextBox =null;
                   // if (c.Name != "textBoxUnvanted1" && c.Name != "textBoxUnvanted2")
                      //  valueOfTextBox = c.Text;
                }
            }

I THINK ABOUT SOMETHING MAYBE CAN I CHANGE "this.Controls" TO "this.tabControl" ? but it doesnt work!!! why?
The Eror -> Error : foreach statement cannot operate on variables of type 'System.Windows.Forms.TabControl' because 'System.Windows.Forms.TabControl' does not contain a public definition for 'GetEnumerator' ??? (i dont understand sorry)


SOMEONE HAVE ANOTHER WAY ?

Because the textBoxes are added to the form (so "this"), not to the tabControl.
You can add them manually when you create a control:

TextBox tb = new TextBox();
//set name, location,...
//and add it:
this.tabControl1.Controls.Add(tb);
//now textBox "tb" is added to tabControl, and not to the form.

I did a new code, which will loop through the tabPages of your tabControl:

foreach (Control c1 in this.tabControl1.Controls)
            {
                if (c1 is TabPage)
                {
                    TabPage pg = (TabPage)c1;
                    Control.ControlCollection tabs = pg.Controls;
                    foreach (Control c2 in tabs)
                    {
                        if (c2 is TextBox && c2 != null)
                        { 
                        
                        }
                    }
                }
            }

Hope it helps,
Mitja

thankssssss mann (Mitja Bonca) its worrk very very goood

@Momerath: I've already seen sudoku game code here build with 81 textboxes.

thankssssss mann (Mitja Bonca) its worrk very very goood

I am glad it works.
you can vote as helpful, or mark it as answered.
Thx
Mitja

hi
i have 90 textBoxes at my program and i want to insert them for my DataBase (sql)
how can i do that?
i know that i can to do array like -> TextBox[] arr = {textBox1,texBox2......textBox(n)};
but i dont wanna this way cause that a lot of work.
how can i do that in another way and easy?
thanks...

[solved]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.