Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Referencing Variable in For Loop

Posted on 2010-11-29
7
Medium Priority
?
395 Views
Last Modified: 2013-12-17
I am almost 100% sure this is possible but I can't seem to get it to work.

I have a for loop and I am trying to use the Loop iterator to reference the value in a text box.

I have 5 text boxes that I want to grab the text out of the box when the for loop increments.  i tried it with brackets around the variable but I get and error stating 'txtUnit1Tag' does not exist in the current context.  How can I reference the text box txtUnit1Tag1 using i ?


for(int i=1; i<5; i++)
{
   value = txtUnit1Tag[i].Text;              
}

Open in new window

0
Comment
Question by:rkneal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 33

Expert Comment

by:jppinto
ID: 34234941
Please try this...
for(int i=1; i<5; i++)
{
   value = FindControl("txtUnit1Tag" & i, Me).Text;              
}

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34234953
WebForms or WinForms?
0
 

Author Comment

by:rkneal
ID: 34235003
Windows Forms
0
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 

Author Comment

by:rkneal
ID: 34235043
jppinto - When i try that I get the name 'FindControl' doe snot exist in the current context, followed by Operator & cannot be applied to operands of type 'string and 'int'.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 252 total points
ID: 34235050
For WinForms, try something like:
private void button1_Click(object sender, EventArgs e)
        {
            Control[] ctls;
            for (int i = 1; i < 5; i++)
            {
                ctls = this.Controls.Find("txtUnit1Tag" + i.ToString(), true);
                if (ctls.Length > 0 && ctls[0] is TextBox)
                {
                    TextBox tb = (TextBox)ctls[0];
                    MessageBox.Show(tb.Text, tb.Name, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

Open in new window

0
 
LVL 9

Assisted Solution

by:shadow77
shadow77 earned 248 total points
ID: 34236587
If you have a form named MainForm with five TextBoxes named textBox1 thru textBox5, this code will do what you want.

To create MainForm_Load, use the Properties window in the Forms Designer to add an event handler for Load (you can't just add the method; you need the triggering mechanism).  MainForm_Load may contain other code as well; this is just what you need to add.

Note that you cannot simply initialize the txtUnit[] array because the TextBoxes don't exist until after InitializeComponent() has been executed (you could also just put this code right after the call to InitializeComponent()).

You really only need the first seven lines.  Test39 just illustrates how to reference your TextBoxes.
TextBox[] txtUnit = new TextBox[5];       // an array that allows indexed access to a group of TextBoxes

    private void MainForm_Load(object sender, EventArgs e)
    {
        TextBox[] _txtUnit = { textBox1, textBox2, textBox3, textBox4, textBox5 };
        txtUnit = _txtUnit;
    }

    private void test39()
    {
        for (int i = 0; i < txtUnit.Length; ++i)
            txtUnit[i].Text = (i * i).ToString();
    }

Open in new window

0
 

Author Closing Comment

by:rkneal
ID: 34239225
Used a combination of both your suggestions to accomplish my end result.  Thanks for your assistance.
0

Featured Post

Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question