|
-
Apr 28th, 2004, 10:41 AM
#1
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:
Inherits System.Windows.Forms.Form
to
VB Code:
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.
-
Apr 28th, 2004, 10:50 AM
#2
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:
Overridable Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim abc As System.Windows.Forms.Form
abc = New Form2
abc.Show()
End Sub
And in Form2, I declared button 2 like this:
VB Code:
Shadows WithEvents button1 As System.Windows.Forms.Button
'and it's click event was::
Overrides Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
MessageBox.Show("Overriden!")
End Sub
This worked. But I have a nagging doubt that "Shadows" is the wrong way to do this.
Some guidance please.
-
Apr 28th, 2004, 08:14 PM
#3
PowerPoster
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:
Overrides Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
MessageBox.Show("Overriden!")
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.
-
Apr 28th, 2004, 10:08 PM
#4
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("Poo")
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!!
-
Apr 28th, 2004, 10:37 PM
#5
Originally posted by taxes
You should not need it for your purpose and the following should work by itself
VB Code:
Overrides Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
MessageBox.Show("Overriden!")
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?
-
Apr 28th, 2004, 10:39 PM
#6
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("Poo")
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.
-
Apr 28th, 2004, 10:39 PM
#7
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!!
-
Apr 28th, 2004, 10:40 PM
#8
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!!
-
Apr 28th, 2004, 10:46 PM
#9
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:
Public Sub New()
' Don't let form1 handle the event
RemoveHandler Button1.Click, AddressOf MyBase.Button1_Click
End Sub
Private Sub Button1_ClickTWO(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!!
-
Apr 28th, 2004, 10:51 PM
#10
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:
Overrides Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("Overriden!")
End Sub
(I removed the "Handles Button1.Click" portion)
And this worked fine.
Thanks a lot for your help everyone!
Merci, aqa!
-
Apr 29th, 2004, 05:33 AM
#11
PowerPoster
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.
-
Apr 29th, 2004, 06:45 AM
#12
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)
-
Apr 29th, 2004, 10:06 AM
#13
PowerPoster
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.
-
Apr 29th, 2004, 06:40 PM
#14
-
Apr 30th, 2004, 06:45 AM
#15
I better stock up on aspirin then, because I have something coming up soon...
-
Apr 17th, 2005, 01:46 PM
#16
Re: Visual Inheritance - handling button click [Resolved]
 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!!
-
Apr 17th, 2005, 02:31 PM
#17
Re: Visual Inheritance - handling button click [Resolved]
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|