Using Custom Controls as an Edit Control
The dbicList uses a standard text box and combo box as its edit control. However, there may be cases where a third party control needs to be used. A good example of this may be a date picker control with a drop down calendar.
In order to use a third party control in the list, that control needs to expose the keys being pressed in it to the host. This is because the host control does not monitor key strokes of the third party control. To use a third party control the developer must interrupt the control’s current edit mode process, monitor the third party control for the navigation keys being used, determine the new cell to go to after a navigation key is pressed, and then put the list back into edit mode.
It should also be noted that the developer must be responsible for moving the data back and forth between the item and the third party control.
The following steps are required to use a third party control.
· A column object for that control must be created and added to the list. NOTE: The EditType property of that column must be set to “UserControl”.
· If the third party control has a “Parent” property, it is advised to set it to the host List. It will make it easier to position it later on.
· Use the BeforeCellEdit event to test if the focus is on the appropriate column. If so, the data from the cell must be moved to the user control.
The following code assumes we are working with a standard date time picker control.
C#
void dbicList1_BeforeCellEdit(object sender, Dbi.WinControl.BeforeCellEditEventArgs e)
{
string strText;
if (e.ColumnIndex == m_nUserControlColumn)
{
strText = e.NodeItem.GetCellText(e.ColumnIndex);
dateTimePicker1.Value = Convert.ToDateTime(strText);
}
}
VB.NET
Private Sub dbicList1_BeforeCellEdit(sender As System.Object, e As Dbi.WinControl.BeforeCellEditEventArgs) Handles dbicList1.BeforeCellEdit
Dim strText As String
If e.ColumnIndex = m_nUserControlColumn Then
strText = e.NodeItem.GetCellText(e.ColumnIndex)
dateTimePicker1.Value = Convert.ToDateTime(strText)
End If
End Sub
· Use the DisplayUserControl event to actually display the user control. Once again test to make sure the focus is in the correct column or else the user control will be showing up where it is not desired.
C#
void dbicList1_DisplayUserControl(object sender, Dbi.WinControl.DisplayUserControlEventArgs e)
{
if (e.ColumnIndex == m_nUserControlColumn)
{
dateTimePicker1.SuspendLayout();
dateTimePicker1.Visible = false;
dateTimePicker1.Location = new Point(e.X, e.Y);
dateTimePicker1.Width = e.Width;
dateTimePicker1.Visible = true;
dateTimePicker1.ResumeLayout();
dateTimePicker1.Focus();
}
}
VB.NET
Private Sub dbicList1_DisplayUserControl(sender As System.Object, e As Dbi.WinControl.DisplayUserControlEventArgs) Handles dbicList1.DisplayUserControl
If e.ColumnIndex = m_nUserControlColumn Then
dateTimePicker1.SuspendLayout()
dateTimePicker1.Visible = False
dateTimePicker1.Location = New Point(e.X, e.Y)
dateTimePicker1.Width = e.Width
dateTimePicker1.Visible = True
dateTimePicker1.ResumeLayout()
dateTimePicker1.Focus()
End If
End Sub
· Use the RemoveUserControls event to write back the data from the user control and then hide the user control. NOTE: The developer must test that the focus is in the proper column or the data from the user control may be written back to all cells being edited.
C#
void dbicList1_RemoveUserControls(object sender, Dbi.WinControl.RemoveUserControlsEventArgs e)
{
if (e.ColumnIndex == m_nUserControlColumn)
WriteDateTimeBack(e.ColumnIndex);
dateTimePicker1.Visible = false;
}
VB.NET
Private Sub dbicList1_RemoveUserControls(sender As System.Object, e As Dbi.WinControl.RemoveUserControlsEventArgs) Handles dbicList1.RemoveUserControls
If e.ColumnIndex = m_nUserControlColumn Then
WriteDateTimeBack(e.ColumnIndex)
End If
dateTimePicker1.Visible = False
End Sub
The above represents the code required for the events in the host control. All that remains is the code to monitor the keystrokes of the user control to support navigation to other cells. By default, the host control monitors for the <Enter>, <Esc>, <Tab> and <Shift Tab> keys. But if the developer is causing the host control to use other keys for navigation, those keys will have to be monitored and acted upon by the user control as well.