Click here to Skip to main content
15,921,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey All
i am trying to create multiple textboxes from a class but can not return textboxes using loop, i thought it would be simple.can any one teach me how can i make an array or list of textboxes within class.
i will deeply appreciate

What I have tried:

C#
public class FormDesign
{
public TextBox txtbox(string txtID)
        {
                        int i = 1;
            while (i < 5)
            {
                TextBox tbox = new TextBox();
                tbox.ID = txtID + i.ToString();
                tbox.Text = "inserted from class";
i++;
              return tbox;
            }
                                                                                                                                    }
}
Posted
Updated 19-Sep-20 19:20pm
Comments
Richard MacCutchan 19-Sep-20 3:57am    
Notwithstanding OriginalGriff's comments, the first thing you need to do is to create the array or List<T> that will contain the set.

Read my solution to the last time you posted this: How do I return textbox in loop from class using ASP.NET C#[^] - it hasn't changed in less than a day ...
 
Share this answer
 
Comments
Aftab Iqbal Clips 19-Sep-20 4:40am    
i appreciate your last solution my brother
but i am looking for some code because i have not yet figured it out how i will assemble list of textboxes,it is bit confusing for me
have a good day
OriginalGriff 19-Sep-20 4:56am    
Google Translate: "I don't know what I am doing, and I don't want to think about it."

Well ... that's a problem, because we can't give you code that will work.
You need to think about what you are trying to do, and work that out properly - then think about how to implement it. The code you show is useless for any practical purpose - and we can't change that without changing the "outside world" that calls it: which we have no access to or idea what it is trying to do either!

So stop throwing code together and hoping, and start thinking about your wider problem - it needs to be designed right or it's going to give you loads more problems in the future.
I will not share the exact code given you have been suggested how to move in detail earlier.
Though I will still nudge you with few more pointers (hopefully) on top of what Griff has already shared.

What/How array or list of textboxes would look like:
C#
//Array
TextBox[] myTextboxArray = new TextBox[2];
myTextboxArray[0] = TextBox1;
myTextboxArray[1] = TextBox2;

//List
List<TextBox> myTextboxList = new List<TextBox>();
myTextBoxList.Add(TextBox1);
myTextBoxList.Add(TextBox2);
mytextBoxList.Add(TextBox3);

Couple of learning articles:
Arrays - C# Programming Guide | Microsoft Docs[^]
Create controls dynamically and retrieve values from it in ASP.Net using C# and VB.Net[^]

Learn and try again. This is for your benefit as these come under basics.
 
Share this answer
 
i figured it out myself
thing that i learned was that if you want to return multiple values from function using loop then you will have to create an Array function instead of creating normal function.
C#
public TextBox[] txtbox(int count)
        {
                                    TextBox[] txtbox;
            txtbox = new TextBox[count];
            int i = 0;
            while(i<txtbox.Length)
                        {
                TextBox t = new TextBox();
                txtbox[i] = t;
                txtbox[i].Text= "inserted textbox array"+i.ToString();
                mypanel.Controls.Add(txtbox[i]);
                i++;
            }
            return txtbox;
        }
 
Share this answer
 

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