Peter Schilling
2020-05-05T08:05:51Z
Hello Igor,

what is the best way to add my own KeyPress-Event-Handler to the TextBox created by iGrid when editing is started?

My old source-code when I used DataGridView looked like this:

private void grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is TextBox)
{
CtrlUtil.removeAllKeyPressEvents((TextBox)e.Control);
e.Control.KeyPress += CtrlUtil.txtBox_onlyEMPS;
}
}

Greetings,
Peter
Peter Schilling
2020-05-05T08:57:24Z
Ok, I figured out myself..

Could you please tell if this is ok or if there is a more efficient way of ..?!

private void grid_ControlAdded(object sender, ControlEventArgs e)
{
String colName = grid.Cols[grid.CurCell.ColIndex].Key;
if( e.Control is TextBox)
{
CtrlUtil.removeAllKeyPressEvents((TextBox)e.Control);
}

if (colName == "GRUPPE")
{
e.Control.KeyPress += CtrlUtil.txtBox_onlyGroups;
TextB
}
}

Edit: But if I set the MaxLength Property of the TextBox it is ignored! Why?
Igor/10Tec
2020-05-05T13:15:39Z
It's interesting how you are trying to employ the ControlAdded event of the Control class for your purpose. Interesting because the internal iGrid editing infrastructure is not so simple as it may look outside. If you examine the private StartTextBoxEdit method of iGrid, you will see that the internal editing infrastructure is based on the iGCellEditorBase class, which is the base class for all cell editors (except drop-down lists). And in the general case, you deal not with an instance of the standard TextBox class. The MaxLength property is also overwritten by iGrid as a part of the whole editing infrastructure (see the iGCellEditorBase.MaxInputLength implementation in iGDefaultTextEditor).

Well, to give you a good answer, I'd like to know the whole task you are trying to solve. Why don't simply use the TextBoxKeyPress event provided by iGrid?
Igor/10Tec
2020-05-05T13:16:00Z
It's interesting how you are trying to employ the ControlAdded event of the Control class for your purpose. Interesting because the internal iGrid editing infrastructure is not so simple as it may look outside. If you examine the private StartTextBoxEdit method of iGrid, you will see that the internal editing infrastructure is based on the iGCellEditorBase class, which is the base class for all cell editors (except drop-down lists). And in the general case, you deal not with an instance of the standard TextBox class. The MaxLength property is also overwritten by iGrid as a part of the whole editing infrastructure (see the iGCellEditorBase.MaxInputLength implementation in iGDefaultTextEditor).

Well, to give you a good answer, I'd like to know the whole task you are trying to solve. Why don't simply use the TextBoxKeyPress event provided by iGrid?
Peter Schilling
2020-05-05T13:51:58Z
Hey Igor,

my goal is to to provide a specialized KeyPress-Handler for some columns where the user input is filtered.. for example, one column does not allow the input of one of these signs: /\?*<>"
Before, when I used the DataGridView I done all this in the grid_EditingControlShowing-Event..


The second goal is to limit the text the user can entry wich can differ from one column to another..

Greetings,
Peter
Peter Schilling
2020-05-06T09:31:43Z
No clue how to provide the MaxLength property to the TextBox in edit-mode? :(

Igor/10Tec
2020-05-06T15:11:00Z
The TextBoxFilterChar event should help you to solve all your problems, and you can even code different logic for different columns based on the e.ColIndex value. The electronic documentation provides two samples how to use this event. They are below.

The following example shows how to use the TextBoxFilterChar event to enter only letters into a cell and convert them to upper case on the fly:
private void iGrid1_TextBoxFilterChar(object sender, iGTextBoxFilterCharEventArgs e)
{
  if (char.IsLetter(e.Char))
    e.Char = char.ToUpper(e.Char);
  else
    e.Char = (char)0;
}

In the next example we allow the user to enter not more than 5 characters in the cells:
private void iGrid1_TextBoxFilterChar(object sender, iGTextBoxFilterCharEventArgs e)
{
  if (iGrid1.TextBox.Text.Length == 5)
    e.Char = (char)0;
}


But to set the maximal number of chars to input, there is a better, declarative way. This is the MaxInputLength property of the cell style object:
iGrid1.Cols[0].CellStyle.MaxInputLength = 5;
Peter Schilling
2020-05-07T11:17:15Z
Super, thank you!
Users browsing this topic