0

Possible Duplicate:
Panel not getting focus

I have a slightly annoying situation. I have a WinForm with a PictureBox held within a Panel (so I can make the picture scrollable if it exceeds a certain size constraint my form has).

Now, in order to enable the user to be able to scroll in the panel using the MouseWheel, I had to put in the following code:

Private Sub MyPanel_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyPanel.MouseEnter
    MyPanel.Focus()
End Sub

This works great and allows the user to scroll the picture as I would like.

Now, my problem is that I also have a textbox on my form which I would like the user to be able to fill data into. The challenge comes about when the user clicks in the textbox and moves the mouse over the Panel - This takes control away from the textbox and stops the user from being able to type.

How can I handle this situation best??

Community
  • 1
  • 1
John Bustos
  • 19,036
  • 17
  • 89
  • 151
  • I apologize that this is seen as a duplicate question - Truth is I didn't even see the similarity - That probably has to do with my programming prowess, though :) – John Bustos Nov 14 '12 at 14:27

2 Answers2

0

Since I had to come up with some kind of a solution since this form is due for production, I coded up the following (which solved the problem), but if anyone has any better solutions, I'd love to hear them!!!

' Form-Level Variable stating whether the panel can take
' the focus away from the preceding control

Dim DontTakeAwayFocus As Boolean = False

Private Sub MyPanel_Click(sender As Object, e As EventArgs) Handles MyPanel.Click
  MyPanel.Focus()
End Sub

Private Sub MyPanel_MouseEnter(sender As Object, e As EventArgs) Handles MyPanel.MouseEnter
  If DontTakeAwayFocus Then Exit Sub
  MyPanel.Focus()
End Sub

Private Sub MyTxtBox_GotFocus(sender As Object, e As EventArgs) Handles MyTxtBox.GotFocus
  DontTakeAwayFocus = True
End Sub

Private Sub MyTxtBox_LostFocus(sender As Object, e As EventArgs) Handles MyTxtBox.LostFocus
  DontTakeAwayFocus = False
End Sub

What this allowed me to do was that if the textbox had focus, then you had to click on the panel to take the focus away, otherwise, the panel still kept the same functionality.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
John Bustos
  • 19,036
  • 17
  • 89
  • 151
0

Stealing focus?

Consider if stealing focus is really the right way to solve your problem. It's quite tempting because the focus/non-focus difference is so obvious, and it's seems so easy. But it can quickly turn into a mess.

Imagine you're using a third party component that steals focus sometimes when it's not supposed to. Annoyed, you'd write some code to grab focus back. Problem solved, not? But now instead of one, there's two components that are inappropriately stealing focus. And in the end, the user will be the victim of this focus war.

Subscribe to the TextBox events instead

Right now you're listening for events on the Panel. Another way of getting the Panel to react to the MouseWheel events is by instead subscribing to the appropriate event of the Textbox on your form, or even subscribe to the event for all controls on your form.

Then in your handler, check to see if the mouse is within the bounds of the Panel. If so, instruct the panel to scroll. Don't forget to propagate the mouse event if necessary.

EDIT

Additional information:

Hope this helps.

Joseph Tanenbaum
  • 2,211
  • 15
  • 30
  • Arjen, I apologize - Your solution sounds so much smarter, but I don't really know how to do what you describe - Could you post some simple code / provide a link of what you mean in terms of subscribing to the events for this kind of situation? Thanks!! – John Bustos Nov 13 '12 at 22:22