Solved

datagridview and key press events issue

Posted on 2010-08-17
5
769 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.NET tools for adding thread safety to a web app? 3 33
Simple Injector with Web Service 4 40
Write to a printer using vb.net 9 26
No Data for DropDown List 2 24
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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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