locked
How do i add the content in the variable string radableRss the same format to listView1 ? RRS feed

  • Question

  • This is the code in form1 load event:

    public Form1()
            {
                InitializeComponent();
    
                readableRss = RssReader.covertRss("http://rotter.net/rss/rotternews.xml");
    
                lines = Regex.Split(readableRss, "\r\n")
                                   .Where(str => !string.IsNullOrEmpty(str))
                                   .ToList();
                StringBuilder strToAddToList = new StringBuilder();
                for (int i = 0; i < lines.Count; i++)
                {
                    listView1.Items.Add(lines[i].ToString());
                }
            }

    Now when im using a breakpoint on the variable readableRss that is a string i see text inside in this format:

    This is the format i want to see in listView1. The exact format:

    A line of text and under it the line of date and time.

    A space and then again line of text and under it date and time.

    The same format as it is in this image.

    In the variable lines that is a List<string> i see all the lines of texts and date and time in indexs for example in index 0 i see:

    "hello this is a test"

    In index 1 i see: wed , 29 jan 2014.....etc

    Then in index 2 text: "this is a test now"

    In index 3 time and date again.

    And so on.

    When im adding the variable lines to the listView1 im gett a mess i dont see all the text lines i dont see all the date and time lines and they are not one under each other.

    I want that it will be show in listView1 the same format as it is in the variable readableRss.

    How can i do it ?

    Wednesday, January 29, 2014 3:20 PM

Answers

  • Hi Chocolade,

    We can use ListBox’s DrawMode property to show wrap text in ListBox item. I build a sample project, you can see the below code:

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                listBox1.DrawMode = DrawMode.OwnerDrawVariable;
                listBox1.MeasureItem += lst_MeasureItem;
                listBox1.DrawItem += lst_DrawItem;
            }
    
            private void lst_MeasureItem(object sender, MeasureItemEventArgs e)
            {
                e.ItemHeight = (int)e.Graphics.MeasureString(listBox1.Items[e.Index].ToString(), listBox1.Font, listBox1.Width).Height;
            }
            private void lst_DrawItem(object sender, DrawItemEventArgs e)
            {
                e.DrawBackground();
                e.DrawFocusRectangle();
                e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                //replace to your code here
                RSS[] rss = new RSS[]{
                    new RSS(){ Title="aaa", Time=DateTime.Now},
                    new RSS(){ Title="bbb",Time = DateTime.Now.AddDays(1)},
                    new RSS(){ Title="ccc",Time = DateTime.Now.AddDays(2)},
                    new RSS(){ Title="ddd",Time = DateTime.Now.AddDays(3)},
                    new RSS(){ Title="eee",Time = DateTime.Now.AddDays(4)},
                };
               listBox1.Items.AddRange(rss);
            }
        }
         class RSS
        {
            public string Title { get; set; }
    
            public DateTime Time { get; set; }
    
            public override string ToString()
            {
                return Title + "\r\n" + Time.ToString();
            }
    }

    If I misunderstand you, please feel free to let me know.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, January 30, 2014 3:05 AM