This project is read-only.

simple ListView example

Feb 23, 2010 at 1:41 PM

Hi,

first of all thanks for these nice controls! I'm building an application right now but have problems creating a ListView!
What I have so far is

        private void CreateListView()
        {
            ListView listView = new ListView();
            listView.Margin = new Margin(16);
            listView.ItemHeight = 64;
            listView.ItemClicked += new EventHandler<ItemEventArgs>(listView_ItemClicked);
            listView.BeginInit();
            foreach (string fileName in GetFileNames())
            {
                FileItem item = new FileItem(fileName);
                listView.Items.Add(item);
            }
            listView.EndInit();
            Controls.Add(listView);
        }

        void listView_ItemClicked(object sender, ItemEventArgs e)
        {
        }

Where GetFileNames() returns a List<string> and FileItem is

    public class FileItem : ListViewItem, ISelectable
    {
        public FileItem(string title)
            : base()
        {
            Label titleLabel = new Label();
            titleLabel.Text = title;
            Controls.Add(titleLabel);
        }
    }
But the list isn't visible. What am I missing?
Thanks,
heliosdev
Mar 5, 2010 at 11:43 AM

Hi heliosdev,

did you try setting the Size of the FileItem in the constructor?

Size=new Size(Width,64);

Help me once, however I am not sure if this will do the trick here. you may also try to set the Margin of the titleLabel to 0 (new Margin(0)) so that the label gets automatically adjusted to the ListViewItem.

 

/schaze

 

Mar 7, 2010 at 3:33 PM
Edited Mar 7, 2010 at 3:33 PM

Hi schaze,

yes, added

Size = new Size(0, 64);
titleLabel.Margin = new Margin(16); // 0 is the default margin

in the FileItem constructor and it works now!

Thanks,
heliosdev