none
Event Casting RRS feed

  • Question

  • I want to draw a border for my panel border upon clicking of a button control. Since i am handling panel paint event from the buttons click event, am struggling to cast button's System.EventArgs to System.Windows.Forms.PaintEventArgs. How can i achieve that? 

    My code snippet is like this

    Private Sub pnlSearch_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pnlSearch.Paint
            If doColor Then
                Dim arect As New Rectangle
                arect = pnlSearch.ClientRectangle
                arect.Width -= 1
                arect.Height -= 1
                e.Graphics.DrawRectangle(Pens.Red, arect)
                doColor = False
            End If
     

     End Sub

           

    Private Sub btnSearchEmp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchEmp.Click

    doColor=True

    pnlSearch_Paint(sender, e)
     End Sub

    How can i cast e in the button btnSearchEmp to the e in the Paint event?

    Friday, July 13, 2012 11:08 AM

Answers

  • You can't do that.  The event args object in the Click event is NOT of type PaintEventArgs.  What you need to do is study the mechanics of window painting.  In general, you never paint outside of the paint event (the WM_PAINT message).  If you have done changes to the state variables that should be reflected graphically somewhere, then you invalidate the control's client are with a call to Invalidate().  This will trigger a WM_PAINT message and therefore a Paint event.

    So in general, whenever the Paint event runs, you must be capable of reaching all pieces of data that dictate the graphical contents of your control so you can paint all elements.


    Jose R. MCP
    Code Samples

    • Proposed as answer by T3quila Friday, July 13, 2012 5:51 PM
    • Unproposed as answer by T3quila Friday, July 13, 2012 5:52 PM
    • Marked as answer by Rockeyey Monday, July 16, 2012 7:25 AM
    Friday, July 13, 2012 3:13 PM

All replies

  • You can't do that.  The event args object in the Click event is NOT of type PaintEventArgs.  What you need to do is study the mechanics of window painting.  In general, you never paint outside of the paint event (the WM_PAINT message).  If you have done changes to the state variables that should be reflected graphically somewhere, then you invalidate the control's client are with a call to Invalidate().  This will trigger a WM_PAINT message and therefore a Paint event.

    So in general, whenever the Paint event runs, you must be capable of reaching all pieces of data that dictate the graphical contents of your control so you can paint all elements.


    Jose R. MCP
    Code Samples

    • Proposed as answer by T3quila Friday, July 13, 2012 5:51 PM
    • Unproposed as answer by T3quila Friday, July 13, 2012 5:52 PM
    • Marked as answer by Rockeyey Monday, July 16, 2012 7:25 AM
    Friday, July 13, 2012 3:13 PM
  • There is no direct way out for this.

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Monday, July 16, 2012 5:09 AM