Multiline RadListView

13 posts, 0 answers
  1. Amin
    Amin avatar
    6 posts
    Member since:
    Feb 2012

    Posted 08 Feb 2012 Link to this post

    Hi,

    I have a radlistview on my form with a datasource set and a displaymember set at designtime.

    The problem is that my display member may be of varying lengths and may not fit into one line.  Is there anyway to make the radlistview display with a wordwrap when necessary i.e some items may be over one line, some over two and some may have even three lines.

    Many thanks.
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 08 Feb 2012 Link to this post

    Hello Amin,

    You could use some HTML in your display data to break up the text onto different lines. For exmaple:
    1: A quick class to hold some data
    public class Person
    {
        public Person(int id, string name, string description)
        {
            Id = id;
            Name = name;
            Description = description;
        }
     
        public int Id
        {
            get;
            set;
        }
     
        public string Name
        {
            get;
            set;
        }
     
        public string Description
        {
            get;
            set;
        }
    }

    and then add some data using this class to a listview

    private void RadForm1_Load(object sender, EventArgs e)
    {
        List<Person> people = new List<Person>();
        people.Add(new Person(1, "Richard", "<html>Does something.<br>I'm not sure what</html>"));
        people.Add(new Person(2, "Chris", "<html>He's the boss man.<br>He can do what he wants</html>"));
        people.Add(new Person(3, "Pete", "<html>Nothing, nada, zip, zero.<br>He does nothing<br>There's quite a long one here</html>"));
     
        this.radListView1.ViewType = ListViewType.ListView;
        this.radListView1.DataSource = people;
        this.radListView1.ValueMember = "Id";
        this.radListView1.DisplayMember = "Description";
     
        radListView1.AllowArbitraryItemHeight = true;  
    }

    Hope that helps, but let me know if you have further questions
    Richard


  3. UI for WinForms is Visual Studio 2017 Ready
  4. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 13 Feb 2012 Link to this post

    Hello Amin,

    Thank you for your question.

    Richard's suggestion is correct and might help you in some cases. However, it has the limitation that you need to put the line breaks manually.

    To enable real text wrapping you can use:
    this.radListView1.AllowArbitraryItemHeight = true;
    this.radListView1.ItemSize = new Size(this.radListView1.Width - 20, 0);
    this.radListView1.VisualItemFormatting += new ListViewVisualItemEventHandler(radListView1_VisualItemFormatting);
     
    void radListView1_VisualItemFormatting(object sender, ListViewVisualItemEventArgs e)
    {
        e.VisualItem.TextWrap = true;
    }

    However, due to a small issue in the layout of RadListView, this will only work in IconView with the Q3 2011 SP1 version. The issue will be addressed with the upcoming release and you will be able to use this code with SimpleView also.

    I hope you find this useful. Should you have any further questions, do not hesitate to contact us.

    Kind regards,
    Ivan Todorov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  5. Allen
    Allen avatar
    14 posts
    Member since:
    Aug 2012

    Posted 29 Oct 2012 Link to this post

    i tried this on details view, and it does not work. any solutions? if i try Richard's solution, my application will slow down. any alternatives? thanks!
  6. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 01 Nov 2012 Link to this post

    Hello RHEA,

    Thank you for writing.

    When RadListView is in details view mode, it uses cell elements to display its data. Therefore, you should use the CellCreating event to allow the TextWrap:
    this.radListView1.CellCreating += new ListViewCellElementCreatingEventHandler(radListView1_CellCreating);
    this.radListView1.AllowArbitraryItemHeight = true;
     
    void radListView1_CellCreating(object sender, ListViewCellElementCreatingEventArgs e)
    {
        e.CellElement.TextWrap = true;
    }

    I hope this will help. Feel free to ask if you have any further questions.

    All the best,
    Ivan Todorov
    the Telerik team
    Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.
  7. Hassan
    Hassan avatar
    13 posts
    Member since:
    Aug 2013

    Posted 13 Aug 2013 Link to this post

    Hello Ivan (Admin),

    I tried to implement but it didn't work for me. I just added a handler on form_new and introduced the event related to the handler.

    Looking forward to hearing from you please.

    Kind regards,

    Hassan
  8. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 16 Aug 2013 Link to this post

    Hi Hassan,

    Thank you for writing.

    This code seems to be working just fine on my end. Could you please provide a small sample demonstrating where the test wrapping does not behave as expected, so we can investigate the precise case and help you resolve it?

    Thank in advance for your time and cooperation.
     
    Regards,
    Stefan
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  9. Hassan
    Hassan avatar
    13 posts
    Member since:
    Aug 2013

    Posted 18 Aug 2013 Link to this post

    Hi Stefan,

    Thanks for reply. Actually, I need to have textwarp (RadListView) in vb windows. I failed to trigger the event. I tried to implement the code into C# but again event hasn't been fired. Here is sample code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;

    namespace ListViewTextWrap
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();

                
            }

            void radListView1_CellCreating(object sender, ListViewCellElementCreatingEventArgs e)
            {
                e.CellElement.TextWrap = true;
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                this.radListView1.CellCreating += new ListViewCellElementCreatingEventHandler(radListView1_CellCreating);
                this.radListView1.AllowArbitraryItemHeight = true;

                radListView1.Items.Add("abcd efgh ijkl mnop qrst uvwx yz abcd efgh ijkl mnop qrst uvwx yz abcd efgh ijkl mnop qrst uvwx yz");
                radListView1.Items.Add("aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk lllll mmmmmm");

            }
        }
    }

    Please give a me a solution in vb as well.

    Thanks,

    Hassan
  10. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 19 Aug 2013 Link to this post

    Hello Hassan,

    When the control is in DetaileView, you have to use the CellFormatting event to format the cells, while in the rest of the views, you have to use the VisualItemFormatting event. Attached you can find both C# and VB projects where both cases are handled. .

    I hope this helps.

    Regards,
    Stefan
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  11. Hassan
    Hassan avatar
    13 posts
    Member since:
    Aug 2013

    Posted 19 Aug 2013 Link to this post

    Hi Stefan,

    Thanks for reply. I tried both c# and vb but again there was no luck with me. For your information I'm using .net 3.5 and Telerik version 2011.3.11.1219. Is there any other property I need to set for RadListView for this purpose?

    It's really killing me.

    Best regards,

    Hassan
  12. Hassan
    Hassan avatar
    13 posts
    Member since:
    Aug 2013

    Posted 20 Aug 2013 Link to this post

    Hi Stefan,

    Please help me with issue. And also please let me know is there any alternative?

    Looking forward for you kind response.

    Best regards,

    Hassan
  13. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 22 Aug 2013 Link to this post

    Hello Hassan,

    Thank you for this clarification. Please note that unless there is specifically provided version in the forums we do test with the latest one and we do provide replied based on our investigations with the latest vesion.

    Indeed, I can confirm that this code does not work with your version. However, with the very next version of the suite (2012.1.12.215) everything works as expected. 

    To work around the issue in your version you need to create custom visual item as the following one:
    public class CustomSimpleListViewVisualItem : BaseListViewVisualItem
     {
     
         protected override SizeF MeasureOverride(SizeF availableSize)
         {
             if (this.Data == null)
             {
                 return SizeF.Empty;
             }
     
             if (this.dataItem.Owner.ShowCheckBoxes)
             {
                 this.ToggleElement.Visibility = ElementVisibility.Visible;
             }
             else
             {
                 this.ToggleElement.Visibility = ElementVisibility.Collapsed;
             }
     
             float indent = 0;
     
             if (this.Data.Owner.ShowGroups &&
             (this.Data.Owner.EnableCustomGrouping || this.Data.Owner.EnableGrouping) &&
             this.Data.Owner.Groups.Count > 0)
             {
                 indent = this.Data.Owner.GroupIndent;
             }
     
             SizeF desiredSize = base.MeasureOverride(LayoutUtils.InfinitySize);
             desiredSize.Width += this.ToggleElement.DesiredSize.Width;
     
             if (this.Data.Size.Height > 0)
             {
                 desiredSize.Height = this.Data.Size.Height;
             }
     
             if (this.Data.Size.Width > 0)
             {
                 desiredSize.Width = this.Data.Size.Width;
             }
     
             RadListViewElement listViewElement = this.Data.Owner;
     
     
             if (listViewElement != null && !listViewElement.AllowArbitraryItemWidth)
             {
                 desiredSize.Width = listViewElement.ItemSize.Width;
             }
     
             if (listViewElement != null && !listViewElement.AllowArbitraryItemHeight)
             {
                 desiredSize.Height = listViewElement.ItemSize.Height;
             }
     
             if (listViewElement != null && listViewElement.AllowArbitraryItemHeight && !listViewElement.AllowArbitraryItemWidth)
             {
                 SizeF measuredSize = base.MeasureOverride(new SizeF(desiredSize.Width, float.PositiveInfinity));
                 desiredSize.Height = measuredSize.Height;
             }
     
             if (listViewElement != null && listViewElement.AllowArbitraryItemWidth && !listViewElement.AllowArbitraryItemHeight)
             {
                 SizeF measuredSize = base.MeasureOverride(new SizeF(float.PositiveInfinity, desiredSize.Height));
                 desiredSize.Width = measuredSize.Width;
             }
     
             if (listViewElement != null && listViewElement.FullRowSelect)
             {
                 desiredSize.Width = Math.Max(float.IsInfinity(availableSize.Width) ? 0 : GetClientRectangle(availableSize).Width, desiredSize.Width + indent);
             }
     
             SizeF clientSize = GetClientRectangle(desiredSize).Size;
     
             RadItem editorElement = this.GetEditorElement(Editor);
     
             SizeF sizef = new SizeF(clientSize.Width - this.ToggleElement.DesiredSize.Width, clientSize.Height);
     
             if (IsInEditMode && editorElement != null)
             {
                 float editorWidth = Math.Min(clientSize.Width - this.ToggleElement.DesiredSize.Width - indent, availableSize.Width - indent);
                 editorElement.Measure(new SizeF(editorWidth, float.PositiveInfinity));
                 desiredSize.Height = Math.Max(desiredSize.Height, editorElement.DesiredSize.Height);
                 sizef.Height = desiredSize.Height;
             }
     
             this.layoutManagerPart.Measure(sizef);
             //this.Data.ActualSize = desiredSize.ToSize();
             PropertyInfo prop = this.Data.GetType().GetProperty("ActualSize");
             if (null != prop && prop.CanWrite)
             {
                 prop.SetValue(this.Data, desiredSize.ToSize(), null);
             }
     
     
             return desiredSize;
         }
     
         protected override Type ThemeEffectiveType
         {
             get
             {
                 return typeof(SimpleListViewVisualItem);
             }
         }
     }

    To put this item in action, you should use the VisualItemCreating event:
    void radListView1_VisualItemCreating(object sender, ListViewVisualItemCreatingEventArgs e)
    {
        e.VisualItem = new CustomSimpleListViewVisualItem();
    }

    I hope this helps.
     
    Regards,
    Stefan
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  14. Hassan
    Hassan avatar
    13 posts
    Member since:
    Aug 2013

    Posted 22 Aug 2013 Link to this post

    Hi Stefan,

    At last it works for me. Thanks for your support.

    Best regards,

    Hassan
Back to Top
UI for WinForms is Visual Studio 2017 Ready