Solved

Checking for mouseclicks outside of CellValidating Event?

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 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