CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: DrawItem event

Hybrid View

  1. #1
    Join Date
    Nov 2010
    Posts
    6

    DrawItem event

    I am using a MS Visual Studio 8 and I have a .NET Framework 2.0 C++ application which have a listbox in it.

    How can I change the colors for the listbox lines in the listbox Event using the following function generated by the system?

    private: System::Void listBoxTest_DrawItem(System::Object^ sender, System::Windows::Forms:rawItemEventArgs^ e)
    {
    // Place the code here
    }


    SG

    btw. I tried to find a solution from the net but found only Win32 C++, C#, VB examples.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: DrawItem event

    This post may turn out to be helpful to you: http://www.codeguru.com/forum/showthread.php?p=1986301

    HTH

    BTW, it didn't do much harm to the tiny code snippet in your post (aside from the fact that theres a bogus smiley in there) but please use code tags when posting code.

    EDIT: Oops: I just noticed that the code in the post I linked to is C#. But I think it shouldn't be too hard to translate it.
    Last edited by Eri523; January 13th, 2011 at 08:12 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Nov 2010
    Posts
    6

    Re: DrawItem event

    Thanks for the info but this is the problem: Net is full of examples but none of them for that function (DrawItem event, C++, .NET 2.0).

    Can somebody please translate it because I found it too hard for my skills...

    SG

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: DrawItem event

    Ok, here goes the translation:

    Code:
    // Form1.cpp
    
    #include "stdafx.h"
    
    #include "Form1.h"
    
    using namespace Test5;
    
    using namespace System::Drawing;
    
    System::Void Form1::listBox1_DrawItem(System::Object^  sender, System::Windows::Forms::DrawItemEventArgs^  e)
    {
      ListBox ^lbMe = static_cast<ListBox ^>(sender);
    
      e->DrawBackground();
      bool bSelected = static_cast<bool>(e->State & DrawItemState::Selected);
    
      if (bSelected) {
        e->Graphics->FillRectangle(Brushes::Gray, e->Bounds);
        e->Graphics->DrawString(lbMe->SelectedItem->ToString(),
          e->Font, Brushes::Black, e->Bounds, StringFormat::GenericDefault);
      } else {
        e->Graphics->FillRectangle(Brushes::White, e->Bounds);
        e->Graphics->DrawString(lbMe->Items[e->Index]->ToString(),
          e->Font, Brushes::Black, e->Bounds, StringFormat::GenericDefault);
      }
      e->DrawFocusRectangle();
    }
    There is, however, one fundamental difference to the C# implementation I linked to: It doesn't reside in a class derived from ListBox, instead it is an event handler attached to the ListBox control using the IDE. But unlike the habit of the IDE of placing the member implementation in the Form class' body inside the .h file, I moved it to a separate .cpp file. (This is the common way of doing that in native C++ and I definitely prefer it.) I chose this dfifferent way of implementing the drawing code because I don't know yet how to attach a derived class to an existing control. (I asked about that in post #11 of the thread I linked to but got no answer.)

    As a consequence of not implementing it in a derived class I can't call the original ListBox::OnDrawItem() at the end of my function because it's a protected member and therefore not reachable except from a derived class. But I doubted the usefulness of that step anyway and apparently it works perfectly without it.

    In order for this to work the DrawMode property of the list box needs to be set to OwnerDrawFixed in the IDE. The C# implementation does this in the derived class' constructor which I can't do here because there simply is no derived class.
    Last edited by Eri523; January 14th, 2011 at 08:10 AM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Nov 2010
    Posts
    6

    Re: DrawItem event

    So there is no known way to use the following function to do it?

    <code>
    private: System::Void listBoxTest_DrawItem(System::Object^ sender, System::Windows::FormsrawItemEventArgs^ e)
    {
    // Place the code here
    }
    </code>

    SG

    EDIT: Stupid me but for some reason I cannot add code tags...
    Last edited by scalpguy; January 15th, 2011 at 11:30 AM.

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: DrawItem event

    Quote Originally Posted by scalpguy View Post
    So there is no known way to use the following function to do it?

    <code>
    private: System::Void listBoxTest_DrawItem(System::Object^ sender, System::Windows::FormsrawItemEventArgs^ e)
    {
    // Place the code here
    }
    </code>
    This is exactly the handler I implemented. With the private: in front of it and without the Form1:: qualification it is just the version placed in the form's .h file by the IDE. If you want to define it there, just leave the header as-is and use the function body I have posted without modification.

    EDIT: Stupid me but for some reason I cannot add code tags...
    You were pretty close: The code tags are [code] and [/code]. Or you can insert them in the Standard or Advanced Post Editor using the button depicted in the attached image.
    Attached Images Attached Images  
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured