Thursday, March 18, 2004

Alright, so I've got this new and shiny blog now! Big thanks to Neil for spending his valuable time converting me to the dasBlogs. It has so much stuff that I will have to spend some time looking around.

Meanwhile, back to the subject.

In Windows programming there is a notion of Virtual ListBoxes or ListViews. It means that a list box would allow you to have unlimited amount of the displayed items. It is usually done by telling it how many items there should be and it simply delegates all drawing of items to the owner of the list box. Under Windows CE the list box can’t be made virtual, only a ListView. But it should be really easy to implement by utilizing OwnerDrawnList base control, which I described here and which has become a part of SDF.

So, here are the steps to implement the VirtualListBox:

 

  1. Derive from OwnerDrawnList to create a new class:

public class VirtualListBox : OwnerDrawnList

{

// Private members

      int count;

      private ArrayList itemList;

      // Constructor

      public VirtualListBox()

      {

            itemList = new ArrayList();

            this.ShowScrollbar= true;

      }

 

      // Property to the number of items

      public int Count

      {

            get

            {

                  return count;

            }

            set

            {

                  if (count!=value)

                  {

                        count = value;

                        itemList.Clear();

                        int[] items = new int[count];

                        itemList.AddRange(items);

                  }

            }

      }

 

      // if you don’t want to draw the item yourself just use this method

      public void DefaultDrawItem(DrawItemEventArgs e, String text)

      {

            Brush textBrush;

 

            Rectangle rcText = e.Bounds;

 

            //Add 3 pixels

            rcText.X += 3;

 

            if (e.State == DrawItemState.Selected) // selected                      {

                  e.DrawBackground();

                  textBrush = new SolidBrush(SystemColors.HighlightText);

            }

            else

            {

                  e.DrawBackground(this.BackColor);

                  textBrush = new SolidBrush(SystemColors.WindowText);

            }

 

// Draw the string

            e.Graphics.DrawString(text, this.Font, textBrush, rcText);

                 

            // Dispose the brush

            textBrush.Dispose();

 

      }

 

      // This is the required by base class override

      protected override System.Collections.IList BaseItems

      {

          get

          {

             return itemList;

          }

      }

           

       // This one required by base class too, but it will do nothing for us.

       protected override object BuildItemForRow(object row)

       {

             return null;

       }

 

   }

 

 

  1. Now we’re ready to use this control on the form. Add these lines to your form’s constructor:

 

InitializeComponent();

 

virtList = new VirtualListBox();

            virtList.Bounds = new Rectangle(1, 1, 200, 100);

            virtList.BackColor = Color.White;

            virtList.Count = 100;

            virtList.ItemHeight = 14;

           

            // Add the control to the form’s controls collection

            this.Controls.Add(virtList);

            // Let’s hook up into DrawItem event

         virtList.DrawItem+=new DrawItemEventHandler(virtList_DrawItem);

 

  1. Implement drawing event in your form:

private void virtList_DrawItem(object sender, DrawItemEventArgs e)

      {

        // You can draw the items yourself here or just delegate to the list itself.   

        virtList.DefaultDrawItem(e, "Virtual Item " + e.Index.ToString());

   }

 

 

3/18/2004 8:26:33 PM (GMT Standard Time, UTC+00:00)  #     | 
 Friday, February 27, 2004

If you think that this is a screenshot of the Today screen then you're mistaken:

In fact, it's the owner-drawn ListBoxEx control from our SDF at OpenNETCF. There was only one line of code had been writen to make it look like Today screen:

listBoxEx1.BackgroundImage = new Bitmap(@"\windows\stwater.gif");

All other settings have been done in the VS IDE designer.

2/27/2004 9:17:16 PM (GMT Standard Time, UTC+00:00)  #     |