This is a migrated thread and some comments may be shown as answers.

Cell KeyPress

2 Answers 505 Views
GridView
This is a migrated thread and some comments may be shown as answers.
gabi
Top achievements
Rank 1
gabi asked on 29 May 2010, 01:11 PM

I use your RadGridView with some GridViewMaskBoxColumn. The GridViewMaskBoxColumn is in masktype numeric with mask=f3.
I want to use the cells keypress event so if i press comma then the cursor jumps after the comma. But i cant find any keypress event for cells, any idea how could i make that work?

2 Answers, 1 is accepted

Sort by
0
gabi
Top achievements
Rank 1
answered on 01 Jun 2010, 09:20 AM

I have find a sollution.
Private Sub GridView_CellEditorInitialized(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles Me.CellEditorInitialized  
        If Me.Columns(e.ColumnIndex).GetType.Name = "UniqeName" And TryCast(Me.Columns(e.ColumnIndex), WindowsControlLibrary1.MaskedBoxColumn).MaskType = Telerik.WinControls.UI.MaskType.Numeric Then  
            Dim editor As Telerik.WinControls.UI.RadMaskedEditBoxEditor = TryCast(Me.ActiveEditor, Telerik.WinControls.UI.RadMaskedEditBoxEditor)  
            Dim oszlop As Telerik.WinControls.UI.GridViewMaskBoxColumn = TryCast(Me.CurrentColumn, Telerik.WinControls.UI.GridViewMaskBoxColumn)  
            If editor IsNot Nothing And oszlop IsNot Nothing Then  
                Dim editorElement As Telerik.WinControls.UI.RadMaskedEditBoxItem = TryCast(editor.EditorElement, Telerik.WinControls.UI.RadMaskedEditBoxItem)  
 
                RemoveHandler editorElement.KeyPress, AddressOf CellaKeyPress  
                AddHandler editorElement.KeyPress, AddressOf CellaKeyPress  
                'editorElement.KeyPressEvent -New KeyPressEventHandler(Me.CellaKeyPress)  
                'editorElement.KeyPressEvent += New KeyPressEventHandler(Me.CellaKeyPress)  
            End If  
        End If  
    End Sub 

then we can use our cellKeyPress event

Private Sub CellaKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)  
         
    End Sub 
0
Alexander
Telerik team
answered on 03 Jun 2010, 09:37 AM
Hello gabi,

You can also add handling of the "comma" key by creating a custom RadMaskedEditBoxEditor and use it instead of the default one. You can do it as it is shown below:

1. Specify that RadGridView will use the custom RadMaskedEditBoxEditor:
this.radGridView1.EditorRequired += new EditorRequiredEventHandler(radGridView1_EditorRequired);
 
private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
{
    if (e.EditorType == typeof(RadMaskedEditBoxEditor))
        e.EditorType = typeof(CustomRadMaskedEditBoxEditor);
}

2. Implement the CustomRadMaskedEditBoxEditor, derived from RadMaskedEditBoxEditor and override BeginEdit() and EndEdit(). You can add an additional handler for the KeyDown event of the RadMaskedEditBoxEditor's RadMaskTextBox and implement the necessary logic in it:
public class CustomRadMaskedEditBoxEditor : RadMaskedEditBoxEditor
{
    private string[] separators = new string[] { ".", "," };
 
    public override void BeginEdit()
    {
        base.MaskTextBox.KeyDown += new KeyEventHandler(MaskTextBox_KeyDown);
        base.BeginEdit();
    }
 
    public override bool EndEdit()
    {
        base.MaskTextBox.KeyDown -= new KeyEventHandler(MaskTextBox_KeyDown);
        return base.EndEdit();
    }
 
    private void MaskTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyValue == 190 || e.KeyValue == 188 || e.KeyValue == 110)
        {
            int pointPosition = -1;
            for (int i = 0; i < base.MaskTextBox.TextLength; i++)
            {
                foreach (string separator in separators)
                {
                    if (base.MaskTextBox.Text[i].ToString().Equals(separator))
                    {
                        pointPosition = i;
                        break;
                    }
                }
            }
 
            if (pointPosition > -1 && pointPosition + 1 < base.MaskTextBox.TextLength)
            {
                base.MaskTextBox.SelectionStart = pointPosition + 1;
            }
        }
    }
}

Regards,
Alexander
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
GridView
Asked by
gabi
Top achievements
Rank 1
Answers by
gabi
Top achievements
Rank 1
Alexander
Telerik team
Share this question
or