Results 1 to 17 of 17

Thread: Visual Inheritance - handling button click [Resolved]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Visual Inheritance - handling button click [Resolved]

    I created Form1 with a button and a textbox on it. I then added Form2 to the project, and changed

    VB Code:
    1. Inherits System.Windows.Forms.Form


    to

    VB Code:
    1. Inherits WindowsApplication2.Form1

    That of course, got me the basic look of Form1 on Form2. Now, I'd like to handle the click event of Button1. How can I do that?
    Last edited by mendhak; Apr 28th, 2004 at 10:51 PM.

  2. #2

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    OK, I think it's figured out, but I still need someone to point out to me whether this is the best way to do it or not:

    In Form1, I changed the Button1_Click event to Overridable.

    VB Code:
    1. Overridable Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim abc As System.Windows.Forms.Form
    3.         abc = New Form2
    4.         abc.Show()
    5.  
    6. End Sub

    And in Form2, I declared button 2 like this:

    VB Code:
    1. Shadows WithEvents button1 As System.Windows.Forms.Button
    2.  
    3. 'and it's click event was::
    4.  
    5.  Overrides Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
    6.         MessageBox.Show("Overriden!")
    7.  
    8. End Sub

    This worked. But I have a nagging doubt that "Shadows" is the wrong way to do this.

    Some guidance please.

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    As I understand it, Member Shadowing is used where you have no access to the source code of the base class (usually a DLL).

    You should not need it for your purpose and the following should work by itself

    VB Code:
    1. Overrides Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
    2.         MessageBox.Show("Overriden!")
    3.  
    4. End Sub
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Shadows is kinda like overrides, except with shadows you can override something that's declared notoverridable...
    anyways why the heck do you have 2 events for button1? hmm

    anyways if you want to have 2 or whatever number of events, you dont need to override or anything like that. Simply copy paste the sub to each form:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         MessageBox.Show("Poo")
    3.     End Sub

    if you look at the sub, it's declared as PRIVATE, so it can be declared in BOTH form1 and form2 without having any conflicts. Also, Button1 is easily accessible in form2 so it's just as though button1 was declared in form2 and there was no form1 to worry about comprende?


    this way if you have 2 event handlers, when you click the button, the event handler in form1 is fired first, and then the event handler for form2... both get "fired"
    Last edited by MrPolite; Apr 28th, 2004 at 10:13 PM.
    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!!

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by taxes

    You should not need it for your purpose and the following should work by itself

    VB Code:
    1. Overrides Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
    2.         MessageBox.Show("Overriden!")
    3.  
    4. End Sub


    I did that, I removed the Shadows line, and it works. Except, when I click on Form2's Button1, I get this messagebox twice. If I put the shadow line back in there, I get the messagebox just once.

    Am I missing something?

  6. #6

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by MrPolite
    Shadows is kinda like overrides, except with shadows you can override something that's declared notoverridable...
    anyways why the heck do you have 2 events for button1? hmm

    anyways if you want to have 2 or whatever number of events, you dont need to override or anything like that. Simply copy paste the sub to each form:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         MessageBox.Show("Poo")
    3.     End Sub

    if you look at the sub, it's declared as PRIVATE, so it can be declared in BOTH form1 and form2 without having any conflicts. Also, Button1 is easily accessible in form2 so it's just as though button1 was declared in form2 and there was no form1 to worry about comprende?


    this way if you have 2 event handlers, when you click the button, the event handler in form1 is fired first, and then the event handler for form2... both get "fired"
    Aah... shoma inja shod!

    I don't have two event handlers for the same button. I'm inheriting form1 into form2 and trying to handle the button click in form2. Since it's being inherited, I can't declare it private, so I had to declare it Overridable in form1 and Overrides in Form2.

    The only probem here is the Shadows/Twice-executed-event tradeoff I'm getting.

  7. #7
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by mendhak
    I did that, I removed the Shadows line, and it works. Except, when I click on Form2's Button1, I get this messagebox twice. If I put the shadow line back in there, I get the messagebox just once.

    Am I missing something?
    read again what I wrote... I said if you have it in both forms, then it will first use the event handler in form1 and then form2, hence 2 message boxes. Shadows basically DELETES the one in form1 and uses the one in form2, hence only one messagebox. dont use shadows
    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!!

  8. #8
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by mendhak
    Aah... shoma inja shod!

    I don't have two event handlers for the same button. I'm inheriting form1 into form2 and trying to handle the button click in form2. Since it's being inherited, I can't declare it private, so I had to declare it Overridable in form1 and Overrides in Form2.

    The only probem here is the Shadows/Twice-executed-event tradeoff I'm getting.
    aah

    just delete the event handler in form1 (step 1). Then, (step 2!) add this to form 2
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MessageBox.Show("Poo")
    End Sub
    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!!

  9. #9
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    if you REALLY want to have the event handler in both forms, you can do this also... but it may be even more stupid than what you are already doing. First declare the event handler in form1 as protected so it would be accessible to form2. Then add this to form2
    VB Code:
    1. Public Sub New()
    2.         ' Don't let form1 handle the event
    3.         RemoveHandler Button1.Click, AddressOf MyBase.Button1_Click
    4.     End Sub
    5.  
    6.     Private Sub Button1_ClickTWO(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         MessageBox.Show("Poo")
    8.     End Sub
    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!!

  10. #10

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by MrPolite
    read again what I wrote... I said if you have it in both forms, then it will first use the event handler in form1 and then form2, hence 2 message boxes. Shadows basically DELETES the one in form1 and uses the one in form2, hence only one messagebox. dont use shadows
    OH, OK, I get it now.

    I changed the bit of code here:

    VB Code:
    1. Overrides Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    2.         MessageBox.Show("Overriden!")
    3.  
    4. End Sub

    (I removed the "Handles Button1.Click" portion)

    And this worked fine.

    Thanks a lot for your help everyone!

    Merci, aqa!

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    HI,

    The beauty of a thread like this is that we all learn something we would not have otherwise investigated.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  12. #12

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by taxes
    HI,

    The beauty of a thread like this is that we all learn something we would not have otherwise investigated.
    I didn't know that this would not be common knowledge. After reading about Visual Inheritance, I had assumed that a lot of people would be using it, since it's kinda like a CSS-for-applications... real useful. (I'm new to VB.NET btw)

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "After reading about Visual Inheritance, I had assumed that a lot of people would be using it,"


    I would think we ARE all using inheritance. If not we might as well stick with VB6. Relating this to your first post, I would not have included the Button1 on the base form if I was going to change its effect in the derived form, but now I know you can bypass the base form event by removing the handles in the derived form, that will change my approach in future.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  14. #14
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    one more thing since it's all about visual inheritance.... if you search the forum you'll find that I and others have posted some really angry threads about visual inheritance
    The Visual Studio IDE has some seious issues with it. I have about 15 forms that inherit from a base form in my project and every now and then IDE messes up and doesn't show any of the forms (shows really NONSENSE errors, for example saying the blah blah button is undefined, even though I haven't edited anything in the forms)....

    so anyways if that happens, you just have to REBUILD the project instead of doing a normal BUILD. In many other cases it has happened to me that some user controls that I've written myself just DISAPPEAR from the inherited forms... then I have to fix it manually by changing the form designer code... so get ready for some headaches if you are working on anything big
    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!!

  15. #15

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by MrPolite

    so anyways if that happens, you just have to REBUILD the project instead of doing a normal BUILD. In many other cases it has happened to me that some user controls that I've written myself just DISAPPEAR from the inherited forms... then I have to fix it manually by changing the form designer code... so get ready for some headaches if you are working on anything big
    I better stock up on aspirin then, because I have something coming up soon...

  16. #16
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Visual Inheritance - handling button click [Resolved]

    Quote Originally Posted by kulrom
    That's because you didn't build your inheritable form before start to inherit from.
    1st Rebuild (recompile) your project. This step is very important. When the project is rebuilt the code is output in a DLL instead of an EXE. If you don't do this step you will not be able to inherit the form ... especially in another project.
    regards
    eh I know that it still has problems with visual inheritance... sometimes it "eats" up the code that's written for controls too (ie,the inherited controls disappear and so do their event handlers)
    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!!

  17. #17
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Visual Inheritance - handling button click [Resolved]

    Quote Originally Posted by kulrom
    In that case we should pray for it that next VS.NET version (2005) will haven't certain bug
    yes
    so far it seems like they've redecorated the toolbar with a truly useless and ugly gradient.
    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