Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.
I'm using C# in Visual Studio and created an application in Windows forms. There's a routine that deals with balance in banks and creates text boxes for the amount in a groupbox. The number of text boxes depends on the number of bank accounts, retrieved from a database.
Another routine uses the data in the textboxes. My problem is that I don't know how to get the data in the dinamically created textboxes. My first routine is:

        <pre>private void Frm_Saldo_Load(object sender, EventArgs e)
        {
            int i;
            int Nbancos = 0;
            Dtp_DataSaldo.Text = DateTime.Now.ToShortDateString();
            DataTable Tab1 = Banco.LeBancos();
            DataRow[] Linha = Tab1.Select(null, null, DataViewRowState.CurrentRows);
            int Nitem = Tab1.Rows.Count;

// Caso haja mais que 3 bancos, deve-se ajustar o tamanho do formulário e do groupbox
            if (Nitem > 3)
            {
                Height = 375 + (Nitem - 3) * 5;
                Gb_Bancos.Height = 180 + (Nitem - 3) * 5;
            }
            Gb_Bancos.Tag = 0;
            for (i = 0; i < Nitem; i++)
            {
                var Rotulo = new Label();
                Rotulo.Name = "Lb_Banco" + i.ToString();
                Rotulo.Text = Linha[i].ItemArray[2].ToString();
                Rotulo.Location = new System.Drawing.Point(10, 40 + 30 * i);
                Rotulo.Size = new Size(150, 26);
                Gb_Bancos.Controls.Add(Rotulo);

                var CampoSaldo = new TextBox();
                CampoSaldo.Name = "Tb_Saldo" + i.ToString();
                CampoSaldo.Font = FonteNormal;
                CampoSaldo.Location = new System.Drawing.Point(160, 40 + 30 * i);
                Gb_Bancos.Controls.Add(CampoSaldo);

                var CampoCod = new TextBox();
                CampoCod.Name = "Tb_Cod" + i.ToString();
                CampoCod.Font = FonteNormal;
                CampoCod.Location = new System.Drawing.Point(180, 40 + 30 * i);
                CampoCod.Text = Linha[i].ItemArray[0].ToString();
                CampoCod.Visible = false;
                Gb_Bancos.Controls.Add(CampoCod);
                Nbancos += 1;
                Gb_Bancos.Tag = Nbancos;
            }
            MostraSaldo();
        }


The routine MostraSaldo() is where I have the problem. Does anyone have a suggestion? Thank you.

What I have tried:

I tried the following routine:
private void MostraSaldo()
{
    int Nbancos = Convert.ToInt32(Gb_Bancos.Tag);
    int i, j;
    var CampoSaldo = new TextBox();
    var CampoCod = new TextBox();
    DataTable Tab1 = SaldoBanco.LeSaldo(Dtp_DataSaldo.Text);
    DataRow[] Linha = Tab1.Select(null, null, DataViewRowState.CurrentRows);
    int Nitem = Tab1.Rows.Count;
    if (Nitem > 0)
    {
        for (i = 0; i < Nbancos; i++)
        {
            CampoSaldo.Name = "Tb_Saldo" + i.ToString();
            CampoCod.Name = "Tb_Cod" + i.ToString();
            MessageBox.Show(CampoCod.Name + " = " + CampoCod.Text);
            for (j = 0; j < Nitem; j++)
                if (CampoCod.Text == Linha[j].ItemArray[0].ToString())
                    CampoSaldo.Text = Linha[j].ItemArray[1].ToString();
        }
    }
}

but it didn't work.
Posted
Updated 18-Jun-21 20:02pm

The first thing I'd do is change the way you do that: instead of creating a label and two textboxes each time, move them into a UserControl and crate one instance of that in your loop instead. It them becomes responsible for it's content, which keeps them together and makes them easier to find. Add Saldo, Cod, and AccName properties to the new control for access and you're good to go.

Then when you want to find them, just loop through Gb_Bancos.Controls and look for the AccountControl instances:
C#
foreach (Control c in Gb_Bancos.Controls)
   {
   if (c is AccountControl ac)
      {
      var saldo = ac.Saldo;
      var cod = ac.Cod;
      }
   }
Give it a go - it sounds complicated, but it isn't really - and it makes the rest of your code much, much cleaner!
 
Share this answer
 
Comments
[no name] 19-Jun-21 2:08am    
8 hours later and we both think about the same question at the same time ...
Ismael Oliveira 2021 19-Jun-21 11:02am    
Hi, OriginalGriff. I think your idea is good, but I'm starting in creating User controls. If you could give me a hint, I would appreciate it. Or if you have other way of doing what I need with my code, it also would be great. Thanks.
OriginalGriff 19-Jun-21 11:39am    
Add a UserControl to your Project - Right click the project in eth solution explorer and select "Add Item", just as you do for a form. Give it a sensible name!
Drop a label and two textboxes onto it - give them appropriate names.
Add three properties - one for the label, and one each for the textboxes:

public string Data
    {
    get { return tbData.Text; }
    set { tbData.Text = value; }
    }

Compile.
You can now treat it just like any other control: drag it from the toolbox, or create it dynamically just as you do your label and text boxes.
Ismael Oliveira 2021 20-Jun-21 20:53pm    
Hi, OriginalGriff.
I tried your suggestion, and I had another problem: when I insert the user control via programming, its name is always the name of the control: camposBancos1. So I don't know how to differenciate each instance. How is it possible to have different names for these instances?
Thanks.
OriginalGriff 21-Jun-21 1:08am    
Um ... yes. Just change the Name property of the instance ... but what use is that to you, since you can't access the name from code:
MyControl mc = new MyControl();
mc.Name = $"MyName{index++}";

Works fine, but you can't later say
MyName1.Text = "Hello world";


Why do you need to name them?
Add a List<textbox> xxx = new List<textbox>() to your form / class.

When you create a new textbox and add it to your groupbox, also add it to the new list.

When you need "only" the textboxes, they're right there; you don't have to iterate the groupbox control collection.

You can even pass the list of textboxes to another routine for processing.
 
Share this answer
 
Comments
Ismael Oliveira 2021 19-Jun-21 11:03am    
Gerry. When I retrieve from DB the account balance, I must put this value into the appropriate field (textBox). I think a list would not help me do this. Od did I misunderstand what you said?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900