Solved

Checking for mouseclicks outside of CellValidating Event?

Posted on 2015-01-16
4
130 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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