CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2007
    Posts
    2

    Loop through txtbox Names

    I have 4 textboxes that I need to run the same function on. Instead of typing out the function 4 times I want to be able to run a loop that does it for me. I'm not sure how to go about doing this. The text boxes are all named as such txtblah1 txtblah2 txtblah3 txtblah4. The function takes in txtblah1.Text. How would I loop through changing the 1 to a 2 and so on?
    Code:
     double 5gtr1 = Calculate(txt5Gtr1.Text, .45);
     double 5gtr2 = Calculate(txt5Gtr2.Text, .45);
     double 5gtr3 = Calculate(txt5Gtr3.Text, .45);
     double 5gtr4 = Calculate(txt5Gtr4.Text, .45);
    ^Is an example of the code I have. I want to turn it into one simple loop instead.

  2. #2
    Join Date
    Dec 2011
    Location
    .Net 4.0
    Posts
    39

    Re: Loop through txtbox Names

    Wish I had the answer. From what I gather, it is a small PITA where you will have to write some chunks of code to allow for "runtime evaluation" (Google the phrase).

    I subscribed to this thread, so if it turns out you find a simple way, please post it.

  3. #3
    Join Date
    Aug 2007
    Posts
    2

    Re: Loop through txtbox Names

    I googled around a while looking for an answer and that's exactly what it looks like, a PITA. Right now I'll just write in like that and when I get done and have more time I can do some more research.

  4. #4
    Join Date
    Dec 2011
    Location
    .Net 4.0
    Posts
    39

    Re: Loop through txtbox Names

    Here is probably the best link. I bet you could create a generic function using all this.

    http://msdn.microsoft.com/en-us/libr...mprovider.aspx

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

    Re: Loop through txtbox Names

    I would think in the example above a for each loop could be used but the double vars should be and array rather than 4 different vars. You could also do this with a control array and probably a couple of other ways as well.

    That said if it is really only 4 items as simple as in the OP I would not bother as the code there would likely be faster anyway and apparently has already been written.
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: Loop through txtbox Names

    A little late where I am and probably not the best answer due to my inexperience but how about something like this?

    Code:
    // Class level list containing Text boxes for calculating
    List<TextBox> txtList = new List<TextBox>(10);
    
    public frmMain()
    {
       InitializeComponent();
    
       // Add text box to list. Only have to be added in once
       txtList.Add(txtBox1);
       txtList.Add(txtBox2);
       txtList.Add(txtBox3);
    }
    
    private void btnCalculate_Click(object sender, EventArgs e))
    {
       // Run a foreach loop 
       foreach (TextBox t in txtList)
       {
          t.Text = Calculate(t.Text, 0.45).ToString();
       }
    }
    
    private double Calculate(string firstVal, double secondVal)
    {
       // Do some calculation
       double calcTotal = double.Parse(firstVal) * secondVal;
    
       // Return double
       return calcTotal;
    }
    Don't know the efficiency of doing something like that though as it seems like it would get confusing fast.

    The only advantage to the code I can see would be the ability to use a foreach loop and change the calculate function to something else without having to write out too much code.

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