2008年11月20日星期四

Scroll TextBox programmatically

Today, one customer want to scroll the TextBox to the last line. It's easy to use hand, but a little bit hard if using code before we don't know how to do.

Sometimes, I think this requirment is very common for net work applications, if there are ports in the client and listen to the server and get information from the sever, and then display in the textBox. Actuall, the information will get displayed, but the scrollBar will not scroll even the height of the Text is above the height of the MultiLine TextBox.

Actually, it is very easy with the following code, we first move the caret to the last line, and use ScrollToCaret method to achieve the goal.
this.textBox1.Focus();
this.textBox1.SelectionStart = this.textBox1.Text.Length;
this.textBox1.ScrollToCaret();

2008年11月3日星期一

How do I show icon and text in the same cell?

Source From: DataGridView FAQ

The DataGridView control does not have any built-in support for showing an icon and text in the same cell. Through the different painting customization events, such as the CellPaint event, you can easily display an icon next to the text in the cell.

The following example extends the DataGridViewTextColumn and cell to paint an image next to the text. The sample uses the DataGridViewCellStyle.Padding property to adjust the text location and overrides the Paint method to paint an icon. This sample can be simplified by handling the CellPainting event and performing similar code.

public class TextAndImageColumn:DataGridViewTextBoxColumn
{
private Image imageValue;
private Size imageSize;

public TextAndImageColumn()
{
this.CellTemplate = new TextAndImageCell();
}

public override object Clone()
{
TextAndImageColumn c = base.Clone() as TextAndImageColumn;
c.imageValue = this.imageValue;
c.imageSize = this.imageSize;
return c;
}

public Image Image
{
get { return this.imageValue; }
set
{
if (this.Image != value) {
this.imageValue = value;
this.imageSize = value.Size;

if (this.InheritedStyle != null) {
Padding inheritedPadding = this.InheritedStyle.Padding;
this.DefaultCellStyle.Padding = new Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom);
}
}
}
}
private TextAndImageCell TextAndImageCellTemplate
{
get { return this.CellTemplate as TextAndImageCell; }
}
internal Size ImageSize
{
get { return imageSize; }
}
}

public class TextAndImageCell : DataGridViewTextBoxCell
{
private Image imageValue;
private Size imageSize;

public override object Clone()
{
TextAndImageCell c = base.Clone() as TextAndImageCell;
c.imageValue= this.imageValue;
c.imageSize = this.imageSize;
return c;
}

public Image Image
{
get {
if (this.OwningColumn == null this.OwningTextAndImageColumn == null) {

return imageValue;
}
else if (this.imageValue != null) {
return this.imageValue;
}
else {
return this.OwningTextAndImageColumn.Image;
}
}
set {
if (this.imageValue != value) {
this.imageValue = value;
this.imageSize = value.Size;

Padding inheritedPadding = this.InheritedStyle.Padding;
this.Style.Padding = new Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom);
}
}
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
// Paint the base content
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

if (this.Image != null) {
// Draw the image clipped to the cell.
System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer();

graphics.SetClip(cellBounds);
graphics.DrawImageUnscaled(this.Image, cellBounds.Location);

graphics.EndContainer(container);
}
}

private TextAndImageColumn OwningTextAndImageColumn
{
get { return this.OwningColumn as TextAndImageColumn; }
}
}