Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Hosting a Custom Control with Properties in a Custom DataGridViewCell

$
0
0

I have a custom control called RichTextBoxMasked which works like a MaskedTextBox with added features like multiple lines, spell checking, currency formatting, etc.  These features are triggered by setting properties in the control, and everything works as planned when I add the control to a form. 

I also have a custom DataGridView to which I want to add multiple RichTextBoxMasked columns.  I have readHow to: Host Controls in Windows Forms DataGridView Cells but I cannot determine how to associate a new RichTextboxMasked control with associated properties to a column.  The code below shows where I am trying to use a RichTextBoxMasked control with a Masked parameter of AllCaps in a RichTextBoxMaskedColumn.

private void Form1_Load(object sender, EventArgs e)
{
    RichTextBoxMasked rtbm = new RichTextBoxMasked();
    rtbm.Masked = Mask.AllCaps;
    RichTextBoxMaskedColumn column = new RichTextBoxMaskedColumn();
    column.HeaderText = "Column 1";
    column.Width = 200;
    RichTextBoxMaskedCell cell = new RichTextBoxMaskedCell();
    cell.Tag = "c1";
    column.CellTemplate = cell;
    artDataGridView1.Columns.Add(column);
}


...
...


namespace CustomControls
{
public class RichTextBoxMaskedColumn : DataGridViewColumn
{
    public RichTextBoxMaskedColumn()
        : base(new RichTextBoxMaskedCell())
    {
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            if (value != null &&
                !value.GetType().IsAssignableFrom(typeof(RichTextBoxMaskedCell)))
            {
                throw new InvalidCastException("Must be a RichTextBoxMaskedCell");
            }
            base.CellTemplate = value;
        }
    }

    public override object Clone()
    {
        RichTextBoxMaskedColumn col = (RichTextBoxMaskedColumn)base.Clone();
        col.CellTemplate = (RichTextBoxMaskedCell)this.CellTemplate.Clone();
        return col;
    }
}

public class RichTextBoxMaskedCell : DataGridViewTextBoxCell
{
    public RichTextBoxMaskedCell()
        : base()
    {
        // Do something here?
    }

    public override void InitializeEditingControl(int rowIndex, object
        initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        base.InitializeEditingControl(rowIndex, initialFormattedValue,
            dataGridViewCellStyle);

        RichTextBoxMaskedEditingControl ctl = DataGridView.EditingControl as RichTextBoxMaskedEditingControl;
            
        if (this.Value == null)
        {
            ctl.Text = "";
        }
        else
        {
            ctl.Text = this.Value.ToString();
        }
    }

    public override Type EditType
    {
        get
        {
            return typeof(RichTextBoxMaskedEditingControl);
        }
    }

    public override Type ValueType
    {
        get
        {
            return typeof(RichTextBoxMasked);
        }
    }

    public override object DefaultNewRowValue
    {
        get
        {
            return "";
        }
    }
}

class RichTextBoxMaskedEditingControl : RichTextBoxMasked, IDataGridViewEditingControl
{
    DataGridView dataGridView;
    private bool valueChanged = false;
    int rowIndex;

    public RichTextBoxMaskedEditingControl()
    {            
    }

    public object EditingControlFormattedValue
    {
        get
        {
            return this.Text.ToString();
        }
        set
        {
            if (value is String)
            {
                try
                { 
                    this.Text = value.ToString();
                }
                catch
                {
                    this.Text = "";
                }
            }
        }
    }

    public object GetEditingControlFormattedValue(
        DataGridViewDataErrorContexts context)
    {
        return EditingControlFormattedValue;
    }

    public void ApplyCellStyleToEditingControl(
        DataGridViewCellStyle dataGridViewCellStyle)
    {
        this.Font = dataGridViewCellStyle.Font;
        this.ForeColor = dataGridViewCellStyle.ForeColor;
        this.BackColor = dataGridViewCellStyle.BackColor;
    }

    public int EditingControlRowIndex
    {
        get
        {
            return rowIndex;
        }
        set
        {
            rowIndex = value;
        }
    }

    public bool EditingControlWantsInputKey(
        Keys key, bool dataGridViewWantsInputKey)
    {
        // Let the RichTextBoxMasked handle the keys listed. 
        switch (key & Keys.KeyCode)
        {
            case Keys.Left:
            case Keys.Up:
            case Keys.Down:
            case Keys.Right:
            case Keys.Home:
            case Keys.End:
            case Keys.PageDown:
            case Keys.PageUp:
                return true;
            default:
                return !dataGridViewWantsInputKey;
        }
    }

    public void PrepareEditingControlForEdit(bool selectAll)
    {
        // No preparation needs to be done.
    }
 
    public bool RepositionEditingControlOnValueChange
    {
        get
        {
            return false;
        }
    }
 
    public DataGridView EditingControlDataGridView
    {
        get
        {
            return dataGridView;
        }
        set
        {
            dataGridView = value;
        }
    }
 
    public bool EditingControlValueChanged
    {
        get
        {
            return valueChanged;
        }
        set
        {
            valueChanged = value;
        }
    }
 
    public Cursor EditingPanelCursor
    {
        get
        {
            return base.Cursor;
        }
    }

    protected override void OnTextChanged(EventArgs eventargs)
    {
        valueChanged = true;
        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        base.OnTextChanged(eventargs);
    }
}
}

                                                                                                                                    


Rob E.




Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>