Hi,

i have a form with controls that show productinformation.
now i have 10 textboxes named tbxPrice1 -> tbxPrice10 .

in my artikecleobject exists a property list<articleprice> named articlepricelist that could have 0 till 10 elements.

now i want to fill my 10 textboxes in a loop against my articlepricelist property.

If the articlepricelist contains 3 elements, the first 3 textboxes tbxPrice1,tbxPrice2 and tbxPrice3 should be filled.
If the articlepricelist is empty, no textbox should be filled.

Has anybody an idea, how to do this ?

by jo

Recommended Answers

All 3 Replies

Show your code. Use bb code tags - Wrap up source code with bb code tags. See # icon at toolbar.

I mocked up your class since you didn't post a definition, but try this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmFindControl : Form
  {
    private List<ArticlePrice> prices;
    public frmFindControl()
    {
      InitializeComponent();
      //initialize the list
      prices = new List<ArticlePrice>();
      for (int i1 = 1; i1 <= 3; i1++)
      {
        prices.Add(new ArticlePrice(i1));
      }
    }
    private void button1_Click(object sender, EventArgs e)
    {
      const string textBoxBaseName = @"textBox";
      if (prices.Count > 0)
      {
        //repeat until we used 10 text boxes or the list ran out of elements
        for (int i1 = 1; i1 <= Math.Min(prices.Count, 10); i1++)
        {
          string ctrlName = textBoxBaseName + i1.ToString("F0");

          TextBox tb = null;
          //Find the controls and search all child containers. Should return 1 element
          Control[] ctrls = this.Controls.Find(ctrlName, true);
          //Make sure it was found
          if ((ctrls.Length > 0) && (ctrls[0] is TextBox))
            tb = (ctrls[0] as TextBox);
          //We didnt find it
          if (tb == null)
            throw new Exception(string.Format("Could not locate text box '{0}'!", ctrlName));
          //We did find it, set the price
          tb.Text = prices[i1-1].Price.ToString("F2");
        }
      }
    }

  }
  //I mocked up a class since you didnt post your code
  public class ArticlePrice
  {
    public decimal Price { get; set; }
    public ArticlePrice() 
    {
      this.Price = default(decimal);
    }
    public ArticlePrice(decimal Price)
      : this()
    {
      this.Price = Price;
    }
  }
}

I don't see any need to know the name of TextBox because what differentiates one on one is the last number => TextBox1 and TextBox2 so I've button AddTextBox which creates and adds new TextBox

List<TextBox> TextBoxesLst = new List<TextBox>();
void AddTextBox_Click(...)
{
if(TextBoxesLst.Count < 10)
{
TextBoxesLst.Add(new TextBox());
//some UI procedures....
}
}
void SetText(int textBoxNumber, string text)
{
TextBoxesLst[textBoxNumber].Text = text;
}
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.