Solved

update textbox with List<string>

Posted on 2011-09-30
3
305 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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