DataGridView custom cell - losing first character on edit

  • Thread starter Thread starter Richard Lewis Haggard
  • Start date Start date
R

Richard Lewis Haggard

I've created a RichTextBox DataGridView cell but the first character entered
when the cell is in display mode is lost when the cell makes the transition
to edit mode. What is the sequence of events that happens behind the scenes
when:
a) RichTextBox cell is displaying data and has focus.
b) User enters a keyboard character.
c) Cell transitions to edit mode.

When the edit process starts, the editor control collects the data that is
present in the cell and sets its own value to it. Initially, of course, this
is an empty string. Any subsequent keystrokes are properly handled by the
RichTextBox control but the initial keystroke that triggered the display of
this control is lost.

I did do one odd thing - I got tired of having that stupid page show up that
said design mode was unavailable for the RichTextBoxEditingControl so I
encapsulated the RichTextBox itself into a User Control. The
RichTextBoxEditingControl is derived from the RichTextBox encapsulating User
Control and this makes Visual Studio '05 happy.

public partial class RichTextBoxControl : UserControl
{
// Accessor to allow operations on the contained RichTextBox
// The child RichTextBox control was added to the UserControl form as
richTextBox.
public RichTextBox RichTextBox { get { return richTextBox; } }

public RichTextBoxControl()
{
InitalizeComponent();
}
}


public class RichTextBoxEditingControl : RichTextBoxControl,
IDataGridViewEditingControl
{
// Bunches of code to implement the IDataGridViewEditingControl
interface.
// Influenced by the MSDN DateTIme Calendar sample.
// ...
}

Any suggestions on where the lost character is going to and, more
importantly, how I can get it into the RichTextBox?
 
I figured out a hack but am not convinced that I understand what is actually
happening or that this is a reasonable fix.

The edit functionality is in RichTextBoxEditingControl which is derived from
RichTextBoxControl and IDataGridViewEditingControl. RichTextBoxControl is
derived from UserControl and has a RichTextBox embedded in its form. In this
class, I've overridden the OnKeyPress event. This event handler only gets
called once, just as the DataGridView is entering edit mode for the cell.

protected override void OnKeyPress( KeyPressEventArgs e )
{
base.OnKeyPress( e );
RichTestBox.Text = e.KeyChar.ToString();
RichTextBox.SelectionStart = 1;
}
 
Back
Top