|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
"Handles" Questionexample: Handles Button1.Click On Form2 (a User Control) I have a button. Here's it's code: Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '... End Sub Ok, now on Form1, I have a Menu. One of the menu items needs to act like Button1 was clicked. I would like to add to the Handles section by adding: Handles Button1.Click, Menuitem1.Click This would work if the button and menuitem were on the same form. But they are on different forms. I get an error saying "Handles clause requires a WithEvents variable". How do I go about this? Thanks for your help! John "johnb41" <ord***@informatik.com> schrieb: Take a look at the 'AddHandler' statement in the documentation. You can use > I would like to add to the Handles section by adding: > Handles Button1.Click, Menuitem1.Click > > This would work if the button and menuitem were on the same form. But > they are on different forms. I get an error saying "Handles clause > requires a WithEvents variable". How do I go about this? this statement to add a handler to an object's event at runtime. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> John,
There are plenty of methods to force a click event of a button. Some of them button1.performclick ButtonClick(nothing, nothing) Or to create an extra sub and start that from your both events (probably for the documentation the best way) I hope this helps, Cor> "Cor Ligthert" <notmyfirstn***@planet.nl> schrieb: As always: /Never/ do that!> ButtonClick(nothing, nothing) -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Hmmm... I was just going to thank Cor for that easy fix. It worked for
me with no problems. Why /Never/ do that? If you convince me, i'll look into the Addhandler suggestion which looks complicated at first glance! :) John "johnb41" <ord***@informatik.com> schrieb No button has been clicked. There is no sender. There are no event args.> Hmmm... I was just going to thank Cor for that easy fix. It worked > for me with no problems. > > Why /Never/ do that? > If you convince me, i'll look into the Addhandler suggestion which Use Addhandler to tell which procedure is to be called when the event is > looks complicated at first glance! :) raised. As Button1_Click is to be called, use addhandler yourmenuitem.click, addressof Form2.Button_Click. I'd then name it differently. Even better, use Cor's 3rd suggestion. Armin John,
> Hmmm... I was just going to thank Cor for that easy fix. It worked for He never could convince me, for me are his arguments about this synthytical > me with no problems. > > Why /Never/ do that? > > If you convince me, i'll look into the Addhandler suggestion which > looks complicated at first glance! :) > sugar. However maybe has Herfried now new arguments. :-) Cor"Cor Ligthert" <notmyfirstn***@planet.nl> schrieb: My concern is with passing 'Nothing' to the event handler's parameters, >> Hmmm... I was just going to thank Cor for that easy fix. It worked for >> me with no problems. >> >> Why /Never/ do that? >> >> If you convince me, i'll look into the Addhandler suggestion which >> looks complicated at first glance! :) >> > He never could convince me, for me are his arguments about this > synthytical sugar. which may lead to problems over time. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> "johnb41" <ord***@informatik.com> schrieb: Maybe you'll extend your code to use the values passed to the handler's > Hmmm... I was just going to thank Cor for that easy fix. It worked for > me with no problems. > > Why /Never/ do that? > > If you convince me, i'll look into the Addhandler suggestion which > looks complicated at first glance! :) 'sender' and 'e' parameter, and calling the handler directly without passing the right values to it will lead to 'NullReferenceException's when attempting to access the parameter values. I suggest to either call the button's 'PerformClick' method (which will only work if the button is enabled and visible), call the handler using 'Bla(Me.Button1, EventArgs.Empty)' or put the code from the handler to a separate procedure and call this procedure from within the handler and any other place. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Thanks everyone for all your help!
I considered the PerformClick method, but it is not available for some of my controls (for example, not available for LinkLabel). I just couldn't figure out the AddHandler method (i.e. where in my code to put it) I think i'll go with making a regular sub routine, and having all my code point to it. Easy for my brain to comprehend! :) Thanks everyone!! John You should probably spend some time learning how addhandler works. Its
quite useful. Show quoteHide quote "johnb41" <ord***@informatik.com> wrote in message news:1119906057.466375.179700@f14g2000cwb.googlegroups.com... > Thanks everyone for all your help! > > I considered the PerformClick method, but it is not available for some > of my controls (for example, not available for LinkLabel). > > I just couldn't figure out the AddHandler method (i.e. where in my code > to put it) > > I think i'll go with making a regular sub routine, and having all my > code point to it. Easy for my brain to comprehend! :) > > Thanks everyone!! > > John > I understand how it works in one way: when you need to add a handler to
a dynamically created control. But i'm sure there's alot more to it than that. Because of this thread, i'm realizing that i'm missing alot of functionality from AddHandler. I'll learn it soon! :) Thanks for everyone's help with this... John John,
If you don't need it, don't use it, it adds nothing. You can normally use the build in Visual Basic functionality for this. (Although I use it often). Cor |
|||||||||||||||||||||||