Solved

Checking for mouseclicks outside of CellValidating Event?

Posted on 2015-01-16
4
127 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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