Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ListBox control - Freezing one or more rows at top?

ListBox control - Freezing one or more rows at top?

Scheduled Pinned Locked Moved C#
databasewinformsgraphicsquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AussieLew
    wrote on last edited by
    #1

    I have a Windows Forms ListBox object showing items that originate in a dictionary. To apply different coloring etc to some of the entries I have set dropDownListBox.DrawMode = DrawMode.OwnerDrawFixed and handling the listbox DrawItem and MeasureItem events. The different colored items are basically additional choices or items I add or remove depending on other factors. These are commands if you like eg. "Advanced Filter", "Remove This Filter" etc and always appear at the top of the listbox items, and are highlighted to separate them from the normal data values. When I show the listbox I set the dropDownListBox.SelectedItem to the previously selected item which may be well down the list. I would like to be able to "freeze" the required top row(s) or commands eg "Advanced Filter", so that as I scroll down, or the listbox shows with the previously selected item, the required rows are always visible. This would save scrolling back right back to the start to select "Advanced Filter". Not having much joy so far, can anyone assist? Thanks AussieLew DrawItem and MeasureItem handlers below...

        void dropDownListBox\_DrawItem(object sender, DrawItemEventArgs e)
            {
            Brush myBrush = Brushes.Green;
            Brush backBrush = Brushes.Red; // This color not used
           
            // string to draw
            string listString = dropDownListBox.Items\[e.Index\].ToString();
    
            // paint the list background
            e.DrawBackground();
    
            switch (listString)
                {
                case "Advanced Filter...":
                    myBrush = Brushes.DarkGreen;
                    backBrush = Brushes.PaleGreen;
                    e.Graphics.FillRectangle(backBrush, e.Bounds);
                    // check what filter commands are included in the list
                    // draw a separator line below the command if it is the last before the autofilter choices
                    if (filterCommandFlag == 2)
                        {
                        // needed to draw the line at e.Bounds.Bottom-1 otherwise the line was overwritten at times
                        e.Graphics.DrawLine(Pens.Green, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);
                        }
                    break;
    
                case "Remove This Filter":
                    myBrush = Brushes.DarkGreen;
                    backBrush = Brushes.PaleGreen;
                    e.Graphi
    
    L 1 Reply Last reply
    0
    • A AussieLew

      I have a Windows Forms ListBox object showing items that originate in a dictionary. To apply different coloring etc to some of the entries I have set dropDownListBox.DrawMode = DrawMode.OwnerDrawFixed and handling the listbox DrawItem and MeasureItem events. The different colored items are basically additional choices or items I add or remove depending on other factors. These are commands if you like eg. "Advanced Filter", "Remove This Filter" etc and always appear at the top of the listbox items, and are highlighted to separate them from the normal data values. When I show the listbox I set the dropDownListBox.SelectedItem to the previously selected item which may be well down the list. I would like to be able to "freeze" the required top row(s) or commands eg "Advanced Filter", so that as I scroll down, or the listbox shows with the previously selected item, the required rows are always visible. This would save scrolling back right back to the start to select "Advanced Filter". Not having much joy so far, can anyone assist? Thanks AussieLew DrawItem and MeasureItem handlers below...

          void dropDownListBox\_DrawItem(object sender, DrawItemEventArgs e)
              {
              Brush myBrush = Brushes.Green;
              Brush backBrush = Brushes.Red; // This color not used
             
              // string to draw
              string listString = dropDownListBox.Items\[e.Index\].ToString();
      
              // paint the list background
              e.DrawBackground();
      
              switch (listString)
                  {
                  case "Advanced Filter...":
                      myBrush = Brushes.DarkGreen;
                      backBrush = Brushes.PaleGreen;
                      e.Graphics.FillRectangle(backBrush, e.Bounds);
                      // check what filter commands are included in the list
                      // draw a separator line below the command if it is the last before the autofilter choices
                      if (filterCommandFlag == 2)
                          {
                          // needed to draw the line at e.Bounds.Bottom-1 otherwise the line was overwritten at times
                          e.Graphics.DrawLine(Pens.Green, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);
                          }
                      break;
      
                  case "Remove This Filter":
                      myBrush = Brushes.DarkGreen;
                      backBrush = Brushes.PaleGreen;
                      e.Graphi
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      AFAIK the Windows ListBox does not support what you want. It insists on positioning the items itself, at best it will let you decide the height of an item, and let you draw the items. However to my knowledge it always scrolls everything. You may or may not paint your items outside the intended rectangle (part of the EventArgs), however I don't expect that to work properly. I basically see two ways to get what you want, both taking more effort than you would hope: 1. use a second ListBox, on top of the first, just showing the fixed items. And if their number and/or height isn't constant, that also means resizing/repositioning the ListBoxes. 2. don't use a ListBox at all, in other words create your own. And then of course there are companies that specialize in Controls (CodeProject is showing some ads for such companies) and may offer exactly what you want, but I'm not familiar with their offerings. :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups