Hi,

I have 15 textboxes that I created. Now, I want to assign a value to each of these texboxes.

I want to do somethign like this.

for(int i = 0; i < 15; i++)
{
//example: texbox0.Text = 0;
(textbox.Name+i.tostring).Text = i.tostring();

}

Thank you for your help.

You obviously will want to make sure the control is found, this code will throw an exception if not.

private void button1_Click(object sender, EventArgs e)
    {
      for (int i1 = 1; i1 <= 15; i1++)
      {
        (this.Controls["textBox" + i1.ToString("F0")] as TextBox).Text = "Hello, world";
      }
    }

actually that won't work because C# is a strongly typed language as opposed to programming language such as PERL or PHP where you can do that one... I've been thinking of doing that before but up to now I never had a solution.. the only way you could do that one is to store all the textboxes you have created and store it in an array of text boxes.. then that's where you do your looping and assigning of values.

commented: No, you are wrong. You can do it and I provided the code that shows how. +0

actually that won't work because C# is a strongly typed language as opposed to programming language such as PERL or PHP where you can do that one... I've been thinking of doing that before but up to now I never had a solution.. the only way you could do that one is to store all the textboxes you have created and store it in an array of text boxes.. then that's where you do your looping and assigning of values.

No, you're wrong. Please run the code sample I posted in this thread.

interesting... i didn't knew you could query objects using variable names... it makes sense since variable names are unique....
now i'm wondering about relating this to events or methods this time... is it possible?

so let's say i have 5 textboxes named textbox1, textbox2, textbox3,.... textbox5. and i have methods names function1, function2.... function 5. I want textbox1 have an event which would be funcion1, and textbox2 would be function2, textbox3 would be function3...... , textbox5 would be function5...

now you see that there is a pattern and it's just the numbers which changes.. is this possible via loop?

interesting... i didn't knew you could query objects using variable names... it makes sense since variable names are unique....
now i'm wondering about relating this to events or methods this time... is it possible?

so let's say i have 5 textboxes named textbox1, textbox2, textbox3,.... textbox5. and i have methods names function1, function2.... function 5. I want textbox1 have an event which would be funcion1, and textbox2 would be function2, textbox3 would be function3...... , textbox5 would be function5...

now you see that there is a pattern and it's just the numbers which changes.. is this possible via loop?

You need to create another thread to start asking questions as it is outside the scope of this thread, and you are not the original poster. Move this over to a new thread and I will be glad to help you.

am not the administrator.. how am i going to move this thread? it's either you answer now then I edit it to some content relevant to this site. orrr let's wait it gets deleted. or you could reply to this message and it would be more worse and would be like a chatroom.

You can't remove the thread, start a new thread in the C# forum and stop posting to this thread. Administrators will clean it up. I won't answer your question in this thread. We all need to work together to keep DaniWeb organized :)

hehe but you have to promise first you have a solution to my problem cause it would be a waste of time posting it over there just to get my answer from you.

Make an empty forms application and add this (see after InitializeComponent) :

public Form1()
        {
            InitializeComponent();

            // create a textbox with name textbox1
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox1.Location = new System.Drawing.Point(50, 50);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.Controls.Add(textBox1);
            //this is the code line from sknake, just changed i1(index variable) to 1
            //works fine with me! Perhaps the Name property of your textboxes differs?
            (this.Controls["textBox" + 1.ToString("F0")] as TextBox).Text = "Hello, world";
           
        }

hehe but you have to promise first you have a solution to my problem cause it would be a waste of time posting it over there just to get my answer from you.

You're wrong about two things -- first I don't have to promise to answer your question. I am not obligated to post on daniweb I just happen to enjoy it. Secondly you are wasting your time by posting here because I can assure your question won't be answered in this thread.

Just open up a new thread and stop arguing, its not hard. See you soon I hope!

Hope my contribution still means something to the OP since it was thrown in in the middle of a heavy debate:icon_eek:

Hope my contribution still means something to the OP since it was thrown in in the middle of a heavy debate:icon_eek:

Of course they were :) You confirmed it was a working code sample!

Thanks

Cool, thanks all for your help.

I'm glad you have it working and good luck!

Please mark this thread as solved if we have answered your question.

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.