ToolStrip and MenuStrip do not trigger events like buttons

M

ML Young

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top