C# WINFORMS Declare instances of user control

87 views Asked by At

1st of all I will show my code, its very simple.

FileStream fs = new FileStream("Stock.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);

                string line = "";
                string[] Produtos = new string[] { };
                int count = 1;
                int i = 0;
                int x = 25;
                int y = 25;

                while ((line = sr.ReadLine()) != null)
                {

                    if (!string.IsNullOrEmpty(line))
                    {
                        
                        
                        ListagemProdutos myPanel = new ListagemProdutos();
                        myPanel.Location = new Point(x, y);
                        this.Controls.Add(myPanel);

                        y = y + 25;
                        x = x + 25;
                        i++;
              
                    }

                }

Now the problem is, every time I get a line != null, it means I have a product which I will display like this

ProductName ProductPrice ProductQuantity Picture

That is what the usercontrol has, just 3 labels and a picturebox, then I want to call it and fill it until the end of my file. The code works perfect with only a MAIN problem since I'm declaring the instance like this

ListagemProdutos myPanel = new ListagemProdutos();

I think he is overwriting , and instead getting lets say I have 5 products get 5 rows (5 times the user control) I only get the last line.. I think I should do it something like this mypanel+[i] ,so it would be mypanel1,mypanel2 but I am not getting the way to do it, cause we can't just do

ListagemProdutos myPanel+[i] = new ListagemProdutos();

Thank you all for reading this, and sorry for any grammar errors hope you could understand anyway!

EDIT: This is a print showing my problem, in a simpler way. Why I can't have 2 or more instances of my user control? why only shows only 1 always?

http://epvpimg.com/Be8tc

1

There are 1 answers

2
Arick On

Romero, Instead incrementing both x & y axis at the end of the foreach loop, increment only y axis. May be your control is going out of the window area.

Just a suggestion, adding control the way you are attempting could be cumbersome instead try using a grid control and add new rows to it with desired values.