Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

datagridview and key press events issue

Posted on 2010-08-17
5
Medium Priority
?
780 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 1000 total points
ID: 33458303
Add RemoveHandler in the Else section.



0
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

971 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