Solved

update textbox with List<string>

Posted on 2011-09-30
3
304 Views
Last Modified: 2012-06-21
In a windows application (C# / VS 2010 Prof) , is there a way to update a textbox with the contents of a List<string> object all at once, instead of looping through the List<string> and adding the strings to the textbox one by one?

Also, is there a way to not update the textbox while it is being updated?
0
Comment
Question by:knowlton
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 36894807
You can use ToArray() on the List and pass that to String.Join() with a separator.  Or you can just set the Lines() property:
private void button1_Click(object sender, EventArgs e)
        {
            List<string> values = new List<string>();
            values.Add("cat");
            values.Add("dog");
            values.Add("fish");

            textBox1.Lines = values.ToArray();

            // or

            textBox1.Text = String.Join(", ", values.ToArray());

            // or

            textBox1.Text = String.Join(Environment.NewLine, values.ToArray());
        }

Open in new window

0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 36903557
It's tons faster now, too, thanks!
0
 
LVL 5

Author Comment

by:knowlton
ID: 36903570
FOR MY REFERENCE:

        private void buttonCalcDivs_Click(object sender, EventArgs e)
        {
            List<string> lsmarkup = new List<string>();
            List<string> lscss = new List<string>();

            int maxdivs = Convert.ToInt32(textBoxHowMany.Text);
            int newnamemarker = maxdivs - 26;
            string controlname = textBoxControlName.Text;
            textBoxMarkup.Text = "";
            textBoxCSS.Text = "";
            const string cssbody = "{position:relative;float:left;}";


            for (int i = 0; i < maxdivs; i++)
            {
                if(i < 26)
                {
                    string tempname = controlname  + char.ConvertFromUtf32(i + 65);
                    string tempmarkup = "<div id='" + tempname + "' class='" + tempname + "' runat='server'></div><!-- end of div " + tempname + " -->";
                    lsmarkup.Add(tempmarkup);    
                    lscss.Add("." + tempname + cssbody);
                    //textBoxCSS.Text += "." + tempname + cssbody;                
                }
                else
                {
                    string tempnameextra = controlname + "A" + (i - 25).ToString();
                    string tempmarkupextra = "<div id='" + tempnameextra + "' class='" + tempnameextra + "'></div><!-- end of div " + tempnameextra + " -->";

                    lsmarkup.Add(tempmarkupextra);
                    lscss.Add("." + tempnameextra + cssbody);
                    
                    //textBoxCSS.Text += "." + tempnameextra + cssbody;                
                }
            }




            textBoxMarkup.Lines = lsmarkup.ToArray();

            textBoxCSS.Lines = lscss.ToArray();
        }

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question