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. Control derived from Listbox not drawing properly

Control derived from Listbox not drawing properly

Scheduled Pinned Locked Moved C#
databasegraphicssales
4 Posts 2 Posters 1 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.
  • R Offline
    R Offline
    Russell Jones
    wrote on last edited by
    #1

    I've got the following code running and the idea is that a row in the DB has a coloured letter in it to denote customer or supplier etc. The first row i highlight seems to vanish, the background for the row is blue and the selected index is set so that's all nice but the text disappears. If i click the row a second time the row unselects and the text comes back. public partial class CompanyListControl : ListBox { int _previousIndex = 0; public CompanyListControl() { InitializeComponent(); this.DrawMode = DrawMode.OwnerDrawFixed; } protected override void OnDrawItem(DrawItemEventArgs e) { base.OnDrawItem(e); if (!DesignMode) { e.DrawBackground(); if (this.SelectedIndex == e.Index) { if (_previousIndex != e.Index) { InvalidateItem(_previousIndex); DrawSelected(e); _previousIndex = e.Index; } } else { DrawUnselected(e); } } } public void InvalidateItem(int index) { if ((index < 0) || (index >= this.Items.Count)) return; object InvalidatedObject = this.Items[index]; this.Items.RemoveAt(index); this.Items.Insert(index, InvalidatedObject); } private void DrawSelected(DrawItemEventArgs e) { if (e.Index == -1) return; int RowWidth = e.Bounds.Width; Font f = new Font("Arial Black", e.Font.Size); Size s = TextRenderer.MeasureText("C", f); int SalesPos = 0; float PurchasePos = s.Width * 0.7f; float TextPos = PurchasePos * 2; int VerticalPos = e.Bounds.Top; e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, Brushes.White, TextPos, e.Bounds.Top); e.Graphics.DrawString("C", f, Brushes.Red, SalesPos, VerticalPos); } private void DrawUnselected(DrawItemEventArgs e) { int RowWidth = e.Bounds.Width; Font f = new Font("Arial Black", e.Font.Size); Size s = TextRenderer.MeasureText("C", f); int SalesPos = 0; float PurchasePos = s.Width * 0.8f; floa

    S 1 Reply Last reply
    0
    • R Russell Jones

      I've got the following code running and the idea is that a row in the DB has a coloured letter in it to denote customer or supplier etc. The first row i highlight seems to vanish, the background for the row is blue and the selected index is set so that's all nice but the text disappears. If i click the row a second time the row unselects and the text comes back. public partial class CompanyListControl : ListBox { int _previousIndex = 0; public CompanyListControl() { InitializeComponent(); this.DrawMode = DrawMode.OwnerDrawFixed; } protected override void OnDrawItem(DrawItemEventArgs e) { base.OnDrawItem(e); if (!DesignMode) { e.DrawBackground(); if (this.SelectedIndex == e.Index) { if (_previousIndex != e.Index) { InvalidateItem(_previousIndex); DrawSelected(e); _previousIndex = e.Index; } } else { DrawUnselected(e); } } } public void InvalidateItem(int index) { if ((index < 0) || (index >= this.Items.Count)) return; object InvalidatedObject = this.Items[index]; this.Items.RemoveAt(index); this.Items.Insert(index, InvalidatedObject); } private void DrawSelected(DrawItemEventArgs e) { if (e.Index == -1) return; int RowWidth = e.Bounds.Width; Font f = new Font("Arial Black", e.Font.Size); Size s = TextRenderer.MeasureText("C", f); int SalesPos = 0; float PurchasePos = s.Width * 0.7f; float TextPos = PurchasePos * 2; int VerticalPos = e.Bounds.Top; e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, Brushes.White, TextPos, e.Bounds.Top); e.Graphics.DrawString("C", f, Brushes.Red, SalesPos, VerticalPos); } private void DrawUnselected(DrawItemEventArgs e) { int RowWidth = e.Bounds.Width; Font f = new Font("Arial Black", e.Font.Size); Size s = TextRenderer.MeasureText("C", f); int SalesPos = 0; float PurchasePos = s.Width * 0.8f; floa

      S Offline
      S Offline
      Saksida Bojan
      wrote on last edited by
      #2

      It was wery hard to find your code problem, but i did find it. Use this:

      protected override void OnDrawItem(DrawItemEventArgs e)
      {
      base.OnDrawItem(e);
      if (!DesignMode)
      {
      e.DrawBackground();
      if (this.SelectedIndex == e.Index)
      DrawSelected(e);
      else
      DrawUnselected(e);
      }
      }

      Also InvalidateItem function removed item and inserted, witch coused to be OnDrawItem called. You have created infinitive loop. Edit: also I do not think if (!DesignMode) is nesesery, because you want to see items in designer mode if you add items using property windows. This is normal behaviour for ListView

      R 1 Reply Last reply
      0
      • S Saksida Bojan

        It was wery hard to find your code problem, but i did find it. Use this:

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
        base.OnDrawItem(e);
        if (!DesignMode)
        {
        e.DrawBackground();
        if (this.SelectedIndex == e.Index)
        DrawSelected(e);
        else
        DrawUnselected(e);
        }
        }

        Also InvalidateItem function removed item and inserted, witch coused to be OnDrawItem called. You have created infinitive loop. Edit: also I do not think if (!DesignMode) is nesesery, because you want to see items in designer mode if you add items using property windows. This is normal behaviour for ListView

        R Offline
        R Offline
        Russell Jones
        wrote on last edited by
        #3

        thank you, not at my desk at the moment but i'll try that fix in the morning. The invalidate thing was added because i thought maybe it needed to redraw to fix it, adding it seemed to make no difference at all, I should have taken it out before posting :-O Thanks again, Russ

        S 1 Reply Last reply
        0
        • R Russell Jones

          thank you, not at my desk at the moment but i'll try that fix in the morning. The invalidate thing was added because i thought maybe it needed to redraw to fix it, adding it seemed to make no difference at all, I should have taken it out before posting :-O Thanks again, Russ

          S Offline
          S Offline
          Saksida Bojan
          wrote on last edited by
          #4

          Russell Jones wrote:

          i thought maybe it needed to redraw to fix it

          Actualy you only draw an items. For that usaly happens in OnDraw function.

          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