Solved

Checking for mouseclicks outside of CellValidating Event?

Posted on 2015-01-16
4
120 Views
Last Modified: 2015-01-17
My Windows Form has a DataGridView (dgv) in which some of the cells cannot be empty. So...once a cell has focus and then another action is taken, for instance clicking on a button, the CellValidating Event fires first. Below is the event:

    Private Sub dgvCompression_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles dgvCompression.CellValidating
        Try
            Dim newInteger As Integer

            EH.ErrorMessage = String.Empty

            Select Case e.RowIndex
                Case e.RowIndex > 0
                    Select Case e.ColumnIndex
                        Case 1  'Output Cell 
                            If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) OrElse newInteger < 0 Then
                                e.Cancel = True
                                EH.ErrorMessage = "Cell cannot be empty!" & "~I"
                            End If
                    End Select
            End Select

        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/dgvCompression_CellValidating() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub

Open in new window


This code works to a point! As long as the cell is empty, this Event continues to fire regardless how many times I click a button or basically just put focus on another control. Is there a way to break out of the Event and capture another keystroke/mouseclick?

Thanks!
0
Comment
Question by:BlakeMcKenna
  • 2
  • 2
4 Comments
 
LVL 9

Expert Comment

by:sudheeshthegreat
Comment Utility
According to MSDN:
This event occurs when a cell loses input focus, enabling content validation.

What are you doing in EH.ProcessMessages? For instance, if you set the focus to a grid, and provide a MessageBox, it would cause the cell to get focus and lose it again, causing the event to keep triggering over and over again.
0
 

Author Comment

by:BlakeMcKenna
Comment Utility
EH.ProcessMessages displays messages using a MessageBox.Show method. Doesn't sound like there's a good work-around...
0
 
LVL 9

Accepted Solution

by:
sudheeshthegreat earned 500 total points
Comment Utility
There are a few options:-
#1
Instead of using a MessageBox, try using
dataGridView.Rows[e.RowIndex].ErrorText
as the above MSDN link shows. Have the error message be displayed on the form itself, next to the grid instead of using a MessageBox.

#2
If you really have to use a MessageBox, try removing the MessageBox call for a while, and check where the focus shifts to. Is it within the grid, or to some other control on the main form? Try setting the focus to something else on the form, and then re-enable the MessageBox calls.

#3
Consider using the CellValueNeeded event instead.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvalueneeded.aspx
0
 

Author Closing Comment

by:BlakeMcKenna
Comment Utility
I actually found a way to handle the empty cells. It's not my first choice but it does accomplish what I need it to do. I will definitely look into what you said sudheeshthegreat....especially the ErrorText possibility.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now