Solved

Checking for mouseclicks outside of CellValidating Event?

Posted on 2015-01-16
4
124 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
ID: 40554775
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
ID: 40554823
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
ID: 40554843
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
ID: 40555758
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

861 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

25 Experts available now in Live!

Get 1:1 Help Now