Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a rich textbox in a Windows Forms Application.
I want to display data from a for loop. My Code is:
C#
ArrayList arr = new ArrayList();
                for (int i = frm; i <= to; i++)
                {
                    //I have stored the data as barcode by concatinating and adding it to arr

                    barcode = prefix + i + suffix;
                    arr.Add(barcode);
                    //MessageBox.Show(barcode);               
                }
                richTextBox1.Text = arr.ToString();
                foreach (string s in arr)
                {

                    richTextBox1.Text = s;
//WHat should i do next to display the coming output from my foreach loop to that richtextbox?
//Currently it shows the last element of my output. How to display all the list elements?
Posted
Comments
Karthik_Mahalingam 27-Nov-13 3:41am    
you r assigning value to the richtextbox in the for loop , so ofcourse it will store only the last data of the loop collection..
u can check my solution for it...

Try this code...



C#
private void Form1_Load(object sender, EventArgs e)
       {
           string barcode = "";
           string prefix = "prefix";
           string suffix = "suffix";
           int frm = 0;
           int to = 10;
           ArrayList arr = new ArrayList();
           for (int i = frm; i <= to; i++)
           {

               barcode = prefix + i + suffix;
               arr.Add(barcode);
           }

           foreach (string s in arr)
           {  richTextBox1.Text  += s + Environment.NewLine;
           }
       }
 
Share this answer
 
Comments
Jiban jyoti Rana 27-Nov-13 5:26am    
Thanks Karthik Mahalingam for your help.
After getting the values in the rich textbox i want to take them one by one and process them one by one. I will be generating barcodes of those values and print them on a page/image/document. So how to do that any idea?
Actually i am confused how to process them one by one and print the list into one single document so that i can print them...
Expecting some help from you....
Thanks in advance...
First off arr.ToString won;t help it will just give you the name of the type, rather than any of the content. Namely "System.Collections.ArrayList"

Second, please don't use ArrayList any more - it was superceded nearly ten years ago with teh much safer and more flexible generic collection such as List<>

Thirdly, your problem is simply that you are setting the rich text box text each time, throwing away any previous text.

Try this:
C#
List<string> list = new List<string>();
for (int i = frm; i <= to; i++)
    {
    //I have stored the data as barcode by concatinating and adding it to arr

    barcode = prefix + i + suffix;
    list.Add(barcode);
    //MessageBox.Show(barcode);
    }
StringBuilder sb = new StringBuilder();
foreach (string s in list)
    {
    sb.Append(s);
    }
richTextBox1.Text = sb.ToString;
Or if you don't need the data again:
C#
richTextBox1.Clear();
for (int i = frm; i <= to; i++)
    {
    richTextBox1.AppendText(string.Format("{0}{1}{2}", prefix, i, suffix));
    }
Replaces all of your code...
 
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