Solved

datagridview and key press events issue

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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 …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now