Subject: how to override an event with an event?
Posted By: blah Post Date: 11/12/2003 11:16:19 AM
Hi i'm new to programming so i'm sorry if this sounds dumb. I've created a class that paints my form then i want to override the paint event within the class that inherits it eg.

Class A
Public Overridable Sub blah_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
...code that paints the form
End Sub

Class B
inherits class A
overrides sub blah_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
End Sub

this works but... what if i only want it overridden if the user clicks on a button?

Blah.


Reply By: Hal Levy Reply Date: 11/12/2003 11:24:31 AM
Always override the paint.

Put an  IF - THEN - ELSE in Class B that, based on the button click, will either run the local version or just call the inherited one.

Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
Reply By: planoie Reply Date: 11/12/2003 11:48:07 AM
To expand on Hal suggestion... in case you aren't sure how to call the first one:

Class B
inherits class A
overrides sub blah_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
If bButtonWasClicked Then
    'Do what you want here
Else
    MyBase.blah_Paint(...)
End Sub

You'll need to figure out how to determine whether the button was clicked or now.  Maybe set a var to true on the button click event then use that as your check inside the new blah_Paint().

Peter
------------------------------------------------------
Work smarter, not harder.
Reply By: blah Reply Date: 11/12/2003 4:35:31 PM
I'm sorry but i'm really confused...
I made a form. By default it's grey and plain looking. I used the paint event to change it. When the program loads, it is painted like i want it look. I want the user to have the option to set it back to default as if i had never used the paint event (plain gray form ) without me having to repaint or set new colors for everything. I got it working by putting in the overrides sub but it is gray when the program loads and not when triggered by any event like a button click because i don't know how to use the button.click event to override the paint event.

This is how i was thinking although obviously it's not going to work...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
overrides blah_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
End Sub
End Sub

so i'm like trying to do this,

If bButtonWasClicked Then
    override the paint (override my paint job) so like override my overrides and make it plain grey default form no paint.
Else
    do nothing, don't override my paint, leave form alone.
End Sub

planoie your right i don't know how to call anything i'm new at this i don't know what i'm doing sorry.

Blah.





Reply By: planoie Reply Date: 11/12/2003 6:16:49 PM
Don't apollogize for inexperience.  We all started as a single cell!

Although I don't work with VB.Net Windows forms much, it would seem to me that you are overcomplicating this problem.  It sounds like you want to modify the visual styles of the form.  Can't you just change the form properties that you are interested in?  It seems accessive to try to deal with this paint method you are trying to conditionally override.

As far as .Net goes, if you override a method, that's it. It's overridden. You can't "un-override" it or override conditionally.  As Hal suggested, all you can do is override it, then do something conditionally within the method.  It happens that you can call the original method by calling MyBase.OriginalMethod() so you could sort of have a conditional override.  But you'll have to base that condition on something like a boolean value set from your button's click event.

Peter
------------------------------------------------------
Work smarter, not harder.
Reply By: blah Reply Date: 11/13/2003 2:06:34 PM
your right... thanks for your help :)


Go to topic 6449

Return to index page 1006
Return to index page 1005
Return to index page 1004
Return to index page 1003
Return to index page 1002
Return to index page 1001
Return to index page 1000
Return to index page 999
Return to index page 998
Return to index page 997