Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ToolStripButton does not fire click event if form does not have focus?

113 views
Skip to first unread message

Joergen Bech

unread,
Apr 28, 2008, 4:10:34 PM4/28/08
to

Hope someone has a solution or some suggestions for this.
This cannot be right?!?

Problem:

I have multiple non-modal forms open at the same time.

One or more of these forms have a ToolStrip, each of which has
one or more ToolStripButtons.

If, say, form A has got the focus and I click a ToolStripButton on
form B, the button does not react at first: What happens is that
form B gets the focus on the first click and THEN I have to click
a second time for the ToolStripButton to fire its Click event.

If form B already had the focus, the Click event would fire the
first time.

The following code illustrates the problem:

Create a new project, add a Button control, a ToolStrip control,
and on the ToolStrip control, three ToolStripButton controls
(or one, for that matter) and paste the code below into form1.

Run the program and click the button to create a second form.
Now try getting a ToolStripButton control to fire its click event
without the form on which it resides having the focus first.

---snip---
Option Explicit On
Option Strict On

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim frm As New Form1
frm.Show()
End Sub

Private Sub ToolStripButton1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ToolStripButton1.Click
MsgBox("Click1")
End Sub

Private Sub ToolStripButton2_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ToolStripButton2.Click
MsgBox("Click2")
End Sub

Private Sub ToolStripButton3_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ToolStripButton3.Click
MsgBox("Click3")
End Sub

End Class
---snip---

Regards,

How do I get the ToolStripButton to fire its click event regardless of
whether its form already has got the focus?

(Note: There is no problem clicking the normal button, form focus or
not.)

?

TIA,

Joergen Bech

Lloyd Sheen

unread,
Apr 28, 2008, 6:27:23 PM4/28/08
to

"Joergen Bech" <jb...@post1.tele.dk> wrote in message
news:h9bc14lrju7o02noo...@4ax.com...

That is windows. There used to be an option in the mouse dialog that
allowed a emulation ( I believe of X-Windows) where when you moved the mouse
over a window it gained focus. I don't see that in Vista. You will get
behaviour in any windows app.

You could capture the mouse enter event and give focus to you app (the
appropriate window) and that would get rid of the standard behaviour.

LS

Joergen Bech

unread,
Apr 29, 2008, 1:53:03 AM4/29/08
to
On Mon, 28 Apr 2008 18:27:23 -0400, "Lloyd Sheen" <a@b.c> wrote:

>
>"Joergen Bech" <jb...@post1.tele.dk> wrote in message
>news:h9bc14lrju7o02noo...@4ax.com...
>>

---snip---


>
>That is windows. There used to be an option in the mouse dialog that
>allowed a emulation ( I believe of X-Windows) where when you moved the mouse
>over a window it gained focus. I don't see that in Vista. You will get
>behaviour in any windows app.
>
>You could capture the mouse enter event and give focus to you app (the
>appropriate window) and that would get rid of the standard behaviour.
>
>LS

Sorry, but I do not understand what you mean.

I have not seen any similar behavior in other programs, so this
is definitely not standard Windows behavior.

In other applications I have no problems switching to
another form by clicking on a tool button or control.

But never mind. I found this link, which explains it all:
http://blogs.msdn.com/rickbrew/archive/2006/01/09/511003.aspx

/JB

ML Young

unread,
Aug 26, 2010, 4:08:00 PM8/26/10
to
AARRGH...I have spent days trying to debug this one.
v3.5
Basically, it is like the person above is trying to say...
a button will trigger an even on an object that ToolStrip or MenuStrip do not.

For example: (some code below)
Create a form

Add a button
Add a Menustrip options
Add a ToolStrip Option
Have the click event of the 3 above do the same thing. Doesn't matter what, just something to set a breakpoint on, and set a breakpoint. I called mine Common_Save_Logic()

Add a datagridview
add at least 1 column.
Add the DataGridView1_CellValueChanged event (I did this in code because of the larger app with the problem, but it is not necessary.)

Run the app and enter something in a cell in the datagrid, but stay in the cell.

If you click the button, the CellValueChange event will fire.
If you click the optin on the ToolStrip or the MenuStrip, the event will not fire.

In my case, the last record being edited in a DataGridView will not have the values entered on the screen because the event to add them to the DataSource appears to not be firing. I left some debug code in there for you to see this.

If you add a .Focus() for ANY button in the code that executes (Common_Save_Logic() in my case), the event will fire. Could probably even be a hidden button. How wierd is that?

One article (older) suggests that there are some events being "eaten" by MenuStrip and ToolStrip that button does not eat.

Anyone know if MS has some property I need to set on the ToolStrip or MenuBar to change this behavior?

Some code:
Public Class frmEventNotFiringSample

Private Sub frmEventNotFiringSample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.DataSource = CreateSampleDataTable().DefaultView
AddHandler DataGridView1.CellValueChanged, AddressOf DataGridView1_CellValueChanged

End Sub
Private Sub tsSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsSave.Click
Common_Save_Logic()
End Sub

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Common_Save_Logic()
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Common_Save_Logic()
End Sub

Private Sub Common_Save_Logic()
btnSave.Focus()
Dim dgvTemp As DataGridView = DataGridView1

Dim i As Integer = 0
Dim tmpInfo As String = ""
'Dim dgvTemp As DataGridView = FindDataGridViewOnTab(TabControl1.SelectedTab)
For Each row2 As DataGridViewRow In dgvTemp.Rows
i += 1
Dim tmp22 As String
If Not IsDBNull(row2.Cells(0).Value) Then
tmp22 = row2.Cells(0).Value
Else
tmp22 = "unknown!"
End If
tmpInfo = String.Format("{0}-{1};{2};{3}", i, tmp22, row2.IsNewRow, row2.State.ToString)
Next

Dim dtEdit As DataTable = DirectCast((dgvTemp.DataSource), DataView).Table

i = 0
For Each row2 As DataRow In dtEdit.Rows
i += 1
Select Case row2.RowState.ToString
Case "Deleted"
tmpInfo = String.Format("{0}-deleted", i)
Case "Added"
tmpInfo = String.Format("{0}-added", i)
Case Else
tmpInfo = String.Format("{0}-other;{1};{2};{3}", i, row2(0), row2.RowState.ToString, row2.RowError.ToString)
End Select
Next


End Sub
Private Function CreateSampleDataTable() As System.Data.DataTable
'Create a new DataTable object
Dim dt1 As New System.Data.DataTable

'Create three columns with string as their type
dt1.Columns.Add("Col1", String.Empty.GetType())
dt1.Columns.Add("Col2", String.Empty.GetType())
dt1.Columns.Add("Col3", String.Empty.GetType())

'Adding some data in the rows of this DataTable
dt1.Rows.Add(New String() {"r1c1", "r1c2", "r1c3"})
dt1.Rows.Add(New String() {"r2c1", "r2c2", "r2c3"})
dt1.Rows.Add(New String() {"r3c1", "r3c2", "r3c3"})

Return dt1
End Function
'*********************************************************************************
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
MessageBox.Show("CellValueChanged:" & e.ColumnIndex & ";" & e.RowIndex, "DEBUG", MessageBoxButtons.OK, MessageBoxIcon.Hand)
Dim dgv As DataGridView = DataGridView1
'DirectCast(sender, DataGridView)
Dim t2 As String = dgv.Columns(e.ColumnIndex).Name
Dim t1 As String = dgv.CurrentRow.Cells(0).Value
End Sub
End Class


>> On Monday, April 28, 2008 6:27 PM Lloyd Sheen wrote:

>> "Joergen Bech" <jb...@post1.tele.dk> wrote in message
>> news:h9bc14lrju7o02noo...@4ax.com...
>>

>> That is windows. There used to be an option in the mouse dialog that
>> allowed a emulation ( I believe of X-Windows) where when you moved the mouse
>> over a window it gained focus. I don't see that in Vista. You will get
>> behaviour in any windows app.
>>
>> You could capture the mouse enter event and give focus to you app (the
>> appropriate window) and that would get rid of the standard behaviour.
>>
>> LS


>>> On Tuesday, April 29, 2008 1:53 AM Joergen Bech wrote:

>>> On Mon, 28 Apr 2008 18:27:23 -0400, "Lloyd Sheen" <a@b.c> wrote:
>>>
>>>

>>> ---snip---


>>>
>>> Sorry, but I do not understand what you mean.
>>>
>>> I have not seen any similar behavior in other programs, so this
>>> is definitely not standard Windows behavior.
>>>
>>> In other applications I have no problems switching to
>>> another form by clicking on a tool button or control.
>>>
>>> But never mind. I found this link, which explains it all:
>>> http://blogs.msdn.com/rickbrew/archive/2006/01/09/511003.aspx
>>>
>>> /JB


>>> Submitted via EggHeadCafe - Software Developer Portal of Choice
>>> Custom Favorites Web Site with MongoDb and NoRM
>>> http://www.eggheadcafe.com/tutorials/aspnet/7fbc7a01-5d30-4cd3-b373-51d4a0e1afa8/custom-favorites-web-site-with-mongodb-and-norm.aspx

Chris Knoll

unread,
Dec 7, 2010, 2:04:58 PM12/7/10
to
You don't need to do any special on click handling. The issue is that the toolstrip is consuming the click event without passing it down to the underlying controls. (this behavior is known as click-through).

The solution is to extend the ToolStrip class (ToolStripEx) and override the methods that are causing the event to be eaten.

This is described in detail here:
http://blogs.msdn.com/b/rickbrew/archive/2006/01/09/511003.aspx


>>>> Submitted via EggHeadCafe
>>>> Microsoft LINQ Query Samples For Beginners
>>>> http://www.eggheadcafe.com/training-topic-area/LINQ-Standard-Query-Operators/33/LINQ-Standard-Query-Operators.aspx

0 new messages