Results 1 to 8 of 8

Thread: Overriding OnPaint in Inherited control..

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297

    Overriding OnPaint in Inherited control..

    Hey ya'll

    I'm trying to mess around with the ListView control, and first I'm overriding the OnPaint method like so:

    Code:
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    
            Dim g As Graphics = e.Graphics
            Dim pn As Pen = New Pen(Color.Green, 10)
            g.DrawLine(pn, 100, 10, 30, 10)
            g.DrawEllipse(New Pen(Color.Red, 20), 20, 40, 20, 20)
        End Sub
    But nothing seems to change when I use my version of the control on my form? What gives? Shouldn't I see a red circle and a green line in my control?

    Thanks!
    -Ben

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297
    Ok,
    So I set the UserPaint style to true.. does this mean I have to re-draw the entire control myself?

    Thanks again,

    Ben

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    First, drop the user-drawn idea for now.

    List controls are touchy. They are usually are a combination of other low-level controls. In some cases, the low-level control will draw after your custom paint event. I haven't played with listview, but have messed around with listbox.
    VB Code:
    1. Private Sub SoftListBox_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    2.         'draw a soft border around the listbox
    3.         e.Graphics.DrawRectangle(New Pen(SystemColors.ControlDarkDark), _
    4.          New Rectangle(ListBox1.Location.X - 1, ListBox1.Location.Y - 1, ListBox1.Width + 2, ListBox1.Height + 2))
    5.     End Sub


    My advice to you is try to use the _Paint event first. If that doesn't work use this:

    VB Code:
    1. Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    2.  
    3. [b]Mybase.OnPaint(e)[/b]
    4.  
    5.         Dim g As Graphics = e.Graphics
    6.         Dim pn As Pen = New Pen(Color.Green, 10)
    7.         g.DrawLine(pn, 100, 10, 30, 10)
    8.         g.DrawEllipse(New Pen(Color.Red, 20), 20, 40, 20, 20)
    9.     End Sub
    Last edited by nemaroller; Nov 21st, 2003 at 03:44 PM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297
    Yea, I've tried MyBase.OnPaint, got me nowhere.

    I read somewhere that I've got to re-draw the entire control myself, so, it's the weekend and I figure what the hell, why not.

    Thanks for the advice,

    Ben

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    When you say inherited control, you mean just declaring a new class and adding Inherits ListView correct?

    Just making sure you are not clicking Add Inherited Control from the designer.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297
    Yea, I've created a new class and typed 'Inherits ....' in the class definition.

    I'll admit I'm having some trouble getting it to paint properly. I'm not sure if it's my fault or something internal to the control that I'm unaware of, but getting my vertical columns to always line up is gettin' a bit tricky.

    Ah well, I haven't given up yet. I'll keep you's guys informed on my progress.

    Thanks for the help!

    -Ben

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297
    On a different note,

    How do I draw text to my control? I figured that GDI would have a way to do it but I don't see anything...

    Thanks!

    Ben

  8. #8
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by BenFinkel
    On a different note,

    How do I draw text to my control? I figured that GDI would have a way to do it but I don't see anything...

    Thanks!

    Ben
    Grahpics.DrawString
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

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