0

What I am trying to do is make a click through PictureBox. However, since I am moving the PictureBox from groupbox to groupbox, it's not as simple as "Give it the same OnClick event as the form". So what I am trying to do is use RemoveHandler and AddHandler to change the PictureBox's OnClick event to the groupbox it's attached to.

Here's an example of the concept I'm trying to use:

Public Shared Sub TransferCoin(pictureBox, groupBoxFrom, groupBoxTo)
    groupBoxTo.Controls.Add(pictureBox)
    groupBoxFrom.Control.Remove(pictureBox)
    RemoveHandler pictureBox.Click, AddressOf pictureBox.Click.Method
    AddHandler pictureBox.Click, AddressOf groupBoxTo.Click.Method
End Sub

This doesn't work because you can't just get the click method of the Picbox or groupbox.

Any help or suggestion would be greatly appreciated. *I know there are similar questions out there but this is different because the picturebox moves around and, as explained above, cannot simply be given the same event as what's behind it because what's behind it changes. *

ched
  • 1
  • 2
  • Can't you just leave the event handler as it is and change the `PictureBox.Parent`: `PictureBox1.Parent = groupBoxTo`? – Jimi Jun 29 '19 at 10:11
  • @Jimi Thanks for the suggestion! However, it doesn't seem to work. I just tried it and clicking the picturebox still doesn't do the same as clicking the groupbox behind it. – ched Jun 29 '19 at 10:17
  • I'm not sure what is that you want to accomplish here. A GroupBox is a container, it doesn't even have a predefined Click event. If you want the PictureBox to perform some other action when moved to another container, remove the old handler `RemoveHandler PictureBox1.Click, AddressOf PictureBox1_Click` and attach the new one: `AddHandler PictureBox1.Click, AddressOf SomeOtherHandler`. `SomeOtherHandler` just needs to have the same signature: `Private Sub SomeOtherHandler(sender As Object, e As EventArgs)` – Jimi Jun 29 '19 at 10:25
  • You can also use a lambda: `PictureBox1.Parent = groupBoxTo RemoveHandler PictureBox1.Click, AddressOf PictureBox1_Click AddHandler PictureBox1.Click, Sub() MessageBox.Show("Clicked on new Parent")` – Jimi Jun 29 '19 at 10:30
  • Possible duplicate of [Is it possible to "steal" an event handler from one control and give it to another?](https://stackoverflow.com/questions/293007/is-it-possible-to-steal-an-event-handler-from-one-control-and-give-it-to-anoth) – Peter Duniho Jun 29 '19 at 18:04
  • Possible duplicate of [How to determine if an event is already subscribed](https://stackoverflow.com/questions/2697247/how-to-determine-if-an-event-is-already-subscribed) – Peter Duniho Jun 29 '19 at 18:04
  • It is not clear at all what it is exactly you're trying to do. The proposed duplicates address the immediate question here, i.e. accessing event handlers for other classes, including Winforms controls. But the specific scenario you describe is unclear, and since there actually isn't a _good_ way to access event handlers from outside the class declaring the event, what you really need is help implementing your goal some other way. But without knowing what that goal is, it's not possible to provide a good answer. – Peter Duniho Jun 29 '19 at 18:08
  • You should consider completely rewriting this question so that instead of asking how to access the subscribed event handler on some class other than your own, you provide a good [mcve] showing the basic structure of the program, explain _precisely_ what behavior you want to add to that, and explain exactly what difficulty you are having in implementing that behavior. See also [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Peter Duniho Jun 29 '19 at 18:09
  • Thanks for the suggestion, I altered the question to be more useful to people trying to help. – ched Jun 30 '19 at 13:36

0 Answers0