Solved

datagridview and key press events issue

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

821 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