Re: Custom controls in datagrid
From: Ernest Morariu (ernest_at_gesora.com)
Date: 06/02/04
- Next message: Ernest Morariu: "Re: Custom controls in datagrid"
- Previous message: Volker Jobst: "Re: Combobox with empty or null values"
- In reply to: CJ: "Re: Custom controls in datagrid"
- Next in thread: Ernest Morariu: "Re: Custom controls in datagrid"
- Reply: Ernest Morariu: "Re: Custom controls in datagrid"
- Reply: CJ: "Re: Custom controls in datagrid"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 2 Jun 2004 11:50:16 +0200
I have made some changes :
1. I changed the RichTextDragBox in RichTextBox to have the possibility to
make tests.
2. In the Edit method ,at the begining I added:
if(this.isEditing )
{
this.rtdb.Focus();
return ;
}
When you type the first character on a new row, the framework calls the Edit
for the second time(when the isEditing is already true) , without calling
Commit first.
3. In the Edit method :
rtdb.Bounds=bounds;
to make the rtdb to occupie all the surface of the cell.
4. In the Edit method :
I changed the position of the :
rtdb.TextChanged += new EventHandler(RichTextBoxChanged);
to the end of the method.
In the old version you associated the event handler and then rtdb.Text =
val. That made to fire the event RichTextBoxChanged and the row apeared as
"modified" when it got the focus and , in fact, it isn't modified.
5. in the event handler RichTextBoxChanged I added:
rtdb.TextChanged -= new EventHandler(RichTextBoxChanged)
The code in this event handler needs to executed just once at any new
editing session. In the old version, you executed this code at any character
pressed on the keyboard.
This is the new version of your class :
////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////
using System.Windows.Forms ;
using System.Drawing ;
using System;
using System.Diagnostics ;
namespace ExtendedDataGrid.ColumnStyles
{
public class DataGridRichTextBoxColumn : DataGridTextBoxColumn
{
public RichTextBox rtdb = new RichTextBox();
// The isEditing field tracks whether or not the user is
// editing data with the hosted control.
private bool isEditing;
public DataGridRichTextBoxColumn() : base()
{
rtdb.Visible = false;
this.NullText = "";
}
protected override void Abort(int rowNum)
{
isEditing = false;
rtdb.TextChanged -= new EventHandler
(RichTextBoxChanged);
Invalidate();
}
protected override bool Commit(CurrencyManager dataSource,
int rowNum)
{
Console.WriteLine("Commit");
rtdb.Bounds = Rectangle.Empty;
rtdb.TextChanged -= new EventHandler
(RichTextBoxChanged);
if (!isEditing)
return true;
isEditing = false;
try
{
string val = rtdb.Text;
SetColumnValueAtRow(dataSource, rowNum, val);
}
catch (Exception)
{
Abort(rowNum);
return false;
}
Invalidate();
return true;
}
protected override void Edit(CurrencyManager source, int
rowNum, Rectangle bounds,
bool readOnly, string instantText, bool
cellIsVisible)
{
if(this.isEditing )
{
this.rtdb.Focus();
return ;
}
string val;
object o = GetColumnValueAtRow(source, rowNum);
if (Convert.IsDBNull(o))
val = this.NullText;
else
val = (string)o;
Console.WriteLine("IT: _" + instantText + "_ val: "
+ val);
if (cellIsVisible)
{
// rtdb.Bounds = new Rectangle
// (bounds.X + 2, bounds.Y + 2,
// bounds.Width - 4, bounds.Height - 4);
rtdb.Bounds=bounds;
rtdb.Text = val;
rtdb.Visible = true;
rtdb.BringToFront();
rtdb.Focus();
}
else
{
rtdb.Text = val;
rtdb.Visible = false;
}
if (rtdb.Visible)
DataGridTableStyle.DataGrid.Invalidate
(bounds);
rtdb.TextChanged += new EventHandler
(RichTextBoxChanged);
}
private void RichTextBoxChanged(object sender,
System.EventArgs e)
{
Console.WriteLine("DragBoxChanged");
this.isEditing = true;
base.ColumnStartedEditing(rtdb);
rtdb.TextChanged -= new EventHandler
(RichTextBoxChanged);
}
protected override Size GetPreferredSize(Graphics g, object
val)
{
return new Size(100, rtdb.PreferredHeight + 4);
}
protected override int GetMinimumHeight()
{
return rtdb.PreferredHeight + 4;
}
protected override int GetPreferredHeight(Graphics g,
object val)
{
return rtdb.PreferredHeight + 4;
}
protected override void Paint(Graphics g, Rectangle bounds,
CurrencyManager source, int rowNum)
{
Paint(g, bounds, source, rowNum, false);
}
protected override void Paint(Graphics g, Rectangle bounds,
CurrencyManager source,
int rowNum, bool alignToRight)
{
Paint(g,bounds, source, rowNum, Brushes.Red,
Brushes.Blue, alignToRight);
}
protected override void Paint(Graphics g, Rectangle bounds,
CurrencyManager source,
int rowNum, Brush backBrush, Brush foreBrush, bool
alignToRight)
{
string val;
object o = GetColumnValueAtRow(source, rowNum);
if (Convert.IsDBNull(o))
val = this.NullText;
else
val = (string)o;
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(val,
this.DataGridTableStyle.DataGrid.Font, foreBrush, rect);
}
protected override void SetDataGridInColumn(DataGrid val)
{
base.SetDataGridInColumn(val);
if (rtdb.Parent != null)
{
rtdb.Parent.Controls.Remove(rtdb);
}
if (val != null)
{
val.Controls.Add(rtdb);
}
}
}
}
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
Ernest
- Next message: Ernest Morariu: "Re: Custom controls in datagrid"
- Previous message: Volker Jobst: "Re: Combobox with empty or null values"
- In reply to: CJ: "Re: Custom controls in datagrid"
- Next in thread: Ernest Morariu: "Re: Custom controls in datagrid"
- Reply: Ernest Morariu: "Re: Custom controls in datagrid"
- Reply: CJ: "Re: Custom controls in datagrid"
- Messages sorted by: [ date ] [ thread ]