Solved

datagridview and key press events issue

Posted on 2010-08-17
5
772 Views
Last Modified: 2012-06-27
Hi,

I have a datagridview in which i have several columns and each column has different datatypes like dates, address, phone numbers.
So, to prevent the user from entering a unwanted data in the fields we are using key press events.
For the phone field i am calling this event.

  Private Sub dgvSchedules_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvSchedules.EditingControlShowing
        Dim strColumnHeader As String
        strColumnHeader = dgvSchedules.Columns(dgvSchedules.CurrentCell.ColumnIndex).HeaderText

        If (strColumnHeader = "Start_Phone" AND  Not e.Control Is Nothing Then
      AddHandler tb.KeyPress, AddressOf cls_Global.NumericText_KeyPress
      end if
   End sub

This part is working good. But when i move out of this cell and enter into the next cell which is a address field, it is not allowing me to enter alphabets. I can only enter numbers.

Why is this happening? How can i prevent this?
0
Comment
Question by:rowternet
[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
  • 3
5 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33457894
Try something like this

Private withEvents thiscontrol as textbox


'editingcontrol showing event'


if Mydatagridview.curentCell.COlumnIndex="YourcolumnIndex" then 
     ThisControl=Ctype(e.Control,Textbox)
   Else 
   ThisCOntrol=Nothing

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33457915
Ok then you can handle the ThisControl_KeyPress Event ...
ThisControl=Nothing is very Important ....
So ...You Are Declaring in which Column You need a key Press Event and after leaving this Cell ThisCOntrol Goes to Nothing
0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 250 total points
ID: 33458303
Add RemoveHandler in the Else section.



0
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 250 total points
ID: 33460688
Complete Code
Private WithEvents thiscontrol As TextBox 

Private Sub dgvSchedules_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvSchedules.EditingControlShowing
      Try
            If dgvSchedules.CurrentCell.ColumnIndex = YourColumnIndex Then
                thiscontrol = CType(e.Control, TextBox)

            Else
                thiscontrol = Nothing
            End If
End Sub

Private Sub thiscontrol_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles thiscontrol.KeyPress
"YourMethod"
End Sub


'Or As I Comment Before ID:33457915 Remove the Event ..Of The CONTROL .In your Case'
 If (strColumnHeader = "Start_Phone" AND  Not e.Control Is Nothing Then
      AddHandler tb.KeyPress, AddressOf cls_Global.NumericText_KeyPress
   Else
      RemoveHandler tb.KeyPress, AddressOf cls_Global.NumericText_KeyPress
 End if
   End sub

Open in new window

0
 

Author Closing Comment

by:rowternet
ID: 33470654
zhaolai and jtoutou.
Could not respond earlier.
Thanks. Adding  remove handler helped
0

Featured Post

Independent Software Vendors: 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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
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…

752 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