HighTechTalks DotNet Forums  

DataGridView, how to capture a cell's KeyPress event

Visual Studio.net (General) microsoft.public.vsnet.general


Discuss DataGridView, how to capture a cell's KeyPress event in the Visual Studio.net (General) forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Komandur Kannan
 
Posts: n/a

Default DataGridView, how to capture a cell's KeyPress event - 01-30-2007 , 08:56 AM






Hi
I am working in vb.Net 2005 framework 2.0. How to capture the cell's
keypress event in datagridview. any great ideas.



Reply With Quote
  #2  
Old   
Linda Liu [MSFT]
 
Posts: n/a

Default RE: DataGridView, how to capture a cell's KeyPress event - 01-30-2007 , 10:53 PM






Hi Kannan,

DataGridView doesn't provided an event something like 'CellKeyPress' for us
to capture a cell's KeyPress event, because there're several kinds of
cells, e.g. DataGridViewTextBoxCell, DataGridViewComboBoxCell,
DataGridViewCheckBoxCell and so on, and not all DataGridView cells should
have the 'KeyPress' event.

Fortunately, DataGridView has provided another
event--EditingControlShowing, which brings us the inner editing control the
current cell is holding. We could get the editing control by handling the
EditingControlShowing event and then subscribe the KeyPress event of the
editing control.

The following is a sample.

public Form1()
{
InitializeComponent();
this.dataGridView1.EditingControlShowing += new
DataGridViewEditingControlShowingEventHandler(data GridView1_EditingControlSh
owing);
}

void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtbox = e.Control as TextBox;
if (txtbox != null)
{
txtbox.KeyPress += new
KeyPressEventHandler(txtbox_KeyPress);
}
}

void txtbox_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #3  
Old   
Komandur Kannan
 
Posts: n/a

Default RE: DataGridView, how to capture a cell's KeyPress event - 01-31-2007 , 03:07 AM



Hi
Can you give the same below sample in vb.Net


"Linda Liu [MSFT]" wrote:

Quote:
Hi Kannan,

DataGridView doesn't provided an event something like 'CellKeyPress' for us
to capture a cell's KeyPress event, because there're several kinds of
cells, e.g. DataGridViewTextBoxCell, DataGridViewComboBoxCell,
DataGridViewCheckBoxCell and so on, and not all DataGridView cells should
have the 'KeyPress' event.

Fortunately, DataGridView has provided another
event--EditingControlShowing, which brings us the inner editing control the
current cell is holding. We could get the editing control by handling the
EditingControlShowing event and then subscribe the KeyPress event of the
editing control.

The following is a sample.

public Form1()
{
InitializeComponent();
this.dataGridView1.EditingControlShowing += new
DataGridViewEditingControlShowingEventHandler(data GridView1_EditingControlSh
owing);
}

void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtbox = e.Control as TextBox;
if (txtbox != null)
{
txtbox.KeyPress += new
KeyPressEventHandler(txtbox_KeyPress);
}
}

void txtbox_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.



Reply With Quote
  #4  
Old   
Linda Liu [MSFT]
 
Posts: n/a

Default RE: DataGridView, how to capture a cell's KeyPress event - 01-31-2007 , 04:06 AM



Hi Kannan,

Here is a sample written in VB.NET.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler DataGridView1.EditingControlShowing, AddressOf
DataGridView1_EditingControlShowing
Me.DataGridView1.RowCount = 1
End Sub

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlSho wingEventArgs)
Dim txtbox As TextBox = CType(e.Control, TextBox)
If Not (txtbox Is Nothing) Then
AddHandler txtbox.KeyPress, AddressOf txtBox_KeyPress
End If
End Sub


Private Sub txtBox_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
MsgBox(e.KeyChar.ToString())
End Sub

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support


Reply With Quote
  #5  
Old   
Komandur Kannan
 
Posts: n/a

Default RE: DataGridView, how to capture a cell's KeyPress event - 02-05-2007 , 06:55 AM



Hi
Thank you very much. It worked fine but every tab this code
creates a new address space. That is If i click in one cell another cell the
value is called twice.
example
If I click n cell 1 and press A
And
If I click on cell 2 and press A
On the First time A occurs once. 2 time it occurs 2 twice and likewise

Parameswaran.N

"Linda Liu [MSFT]" wrote:

Quote:
Hi Kannan,

Here is a sample written in VB.NET.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler DataGridView1.EditingControlShowing, AddressOf
DataGridView1_EditingControlShowing
Me.DataGridView1.RowCount = 1
End Sub

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlSho wingEventArgs)
Dim txtbox As TextBox = CType(e.Control, TextBox)
If Not (txtbox Is Nothing) Then
AddHandler txtbox.KeyPress, AddressOf txtBox_KeyPress
End If
End Sub


Private Sub txtBox_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
MsgBox(e.KeyChar.ToString())
End Sub

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support



Reply With Quote
  #6  
Old   
Linda Liu [MSFT]
 
Posts: n/a

Default RE: DataGridView, how to capture a cell's KeyPress event - 02-05-2007 , 09:36 PM



Hi,

Thank you for your feedback.

I performed another test on my sample code and did see the problem you
described. The reason of the problem is that the KeyPress event of the
editing control is subscribed more than once. I didn't realize the editing
controls in different cells of same cell template are the same one.

To solve this problem, we should unsubscribe the KeyPress event of the
editing control when the current cell leaves focus.

The following is the modified sample code.

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler DataGridView1.EditingControlShowing, AddressOf
DataGridView1_EditingControlShowing
' subcribe the CellLeave event of the DataGridView
AddHandler DataGridView1.CellLeave, AddressOf
DataGridView1_CellLeave

Me.DataGridView1.RowCount = 1
End Sub

' add a field for the editing control to access it later
Dim txtbox As TextBox = Nothing

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlSho wingEventArgs)
txtbox = CType(e.Control, TextBox)
If Not (txtbox Is Nothing) Then
AddHandler txtbox.KeyPress, AddressOf txtBox_KeyPress
End If
End Sub


Private Sub txtBox_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
MsgBox(e.KeyChar.ToString())
End Sub

Private Sub DataGridView1_CellLeave(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
' if the editing control is not nothing, unsubscribe the KeyPress
event
If Not (txtbox Is Nothing) Then
RemoveHandler txtbox.KeyPress, AddressOf txtBox_KeyPress
End If
End Sub
End Class

I have tested the above code on my machine and confirmed it works.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support



Reply With Quote
  #7  
Old   
Komandur Kannan
 
Posts: n/a

Default RE: DataGridView, how to capture a cell's KeyPress event - 02-05-2007 , 11:40 PM



Hi
Thanks

"Linda Liu [MSFT]" wrote:

Quote:
Hi,

Thank you for your feedback.

I performed another test on my sample code and did see the problem you
described. The reason of the problem is that the KeyPress event of the
editing control is subscribed more than once. I didn't realize the editing
controls in different cells of same cell template are the same one.

To solve this problem, we should unsubscribe the KeyPress event of the
editing control when the current cell leaves focus.

The following is the modified sample code.

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler DataGridView1.EditingControlShowing, AddressOf
DataGridView1_EditingControlShowing
' subcribe the CellLeave event of the DataGridView
AddHandler DataGridView1.CellLeave, AddressOf
DataGridView1_CellLeave

Me.DataGridView1.RowCount = 1
End Sub

' add a field for the editing control to access it later
Dim txtbox As TextBox = Nothing

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlSho wingEventArgs)
txtbox = CType(e.Control, TextBox)
If Not (txtbox Is Nothing) Then
AddHandler txtbox.KeyPress, AddressOf txtBox_KeyPress
End If
End Sub


Private Sub txtBox_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
MsgBox(e.KeyChar.ToString())
End Sub

Private Sub DataGridView1_CellLeave(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
' if the editing control is not nothing, unsubscribe the KeyPress
event
If Not (txtbox Is Nothing) Then
RemoveHandler txtbox.KeyPress, AddressOf txtBox_KeyPress
End If
End Sub
End Class

I have tested the above code on my machine and confirmed it works.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support




Reply With Quote
  #8  
Old   
progress200710b
 
Posts: n/a

Default RE: DataGridView, how to capture a cell's KeyPress event - 09-30-2008 , 04:40 AM



Hello

thanks for ur grest response.

Actully i m using a DataGridViewTextBoxCell

each time whrn i click on it the messagboxes increases.

i have handled the Cell Leave event
but it is not working

url:http://www.ureader.com/msg/15363865.aspx

Reply With Quote
  #9  
Old   
Samuel Kamiru Mwangi
 
Posts: n/a

Default RE: DataGridView, how to capture a cell's KeyPress event - 04-28-2010 , 10:02 AM



Hi Linda,
This is nice has sorted my problem partly.
How do I capture the Enter Key so that when I press the enter key, I move to
the next column instead to the next row.

Thanks.

Samuel.

url:http://www.ureader.com/msg/15363865.aspx

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.