سلام
دوستان مشکل این کد چیه که خروجی ها روی هم می افتند. مثلاً به جای
jealous
je a lous
می آید در خروجی یا اینکه کاراکترها روی هم می افتند.
در واقع مشکل اینجاست: rect1.X += . چه طوری باید تنظیمش کنم؟
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (this.dataGridView1.Columns[0].Index ==
e.ColumnIndex && e.RowIndex >= 0)
{
Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
e.CellBounds.Y + 1, e.CellBounds.Width - 4,
e.CellBounds.Height - 4);
using (
Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);
// Draw the inset highlight box.
//e.Graphics.DrawRectangle(Pens.Blue, newRect);
// Draw the text content of the cell, ignoring alignment.
///if (e.Value != null)
///{
///e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
///Brushes.Black, e.CellBounds.X + 2,
///e.CellBounds.Y + 2, StringFormat.GenericDefault);
///}
///e.Handled = true;
if (e.RowIndex != -1 && e.Value != null)
{
if (!e.Handled)
{
e.Handled = true;
e.PaintBackground(e.CellBounds, dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected);
}
if ((e.PaintParts & DataGridViewPaintParts.ContentForeground) != DataGridViewPaintParts.None)
{
string text = e.Value.ToString();
string textPart1="", textPart2="", textPart3="";
int Index = text.IndexOf(textBox1.Text);
int length = textBox1.Text.Length;
Size fullsize = TextRenderer.MeasureText(text, e.CellStyle.Font);
if (Index == 0)
{
textPart1 = text.Substring(Index, length);
textPart2 = text.Substring(Index + 1);
}
else
{
textPart2 = text.Substring(0, Index);
textPart1 = text.Substring(Index, length);
textPart3 = text.Substring(Index + 1);
}
Size size1 = TextRenderer.MeasureText(textPart1, e.CellStyle.Font);
Size size2 = TextRenderer.MeasureText(textPart2, e.CellStyle.Font);
Size size3 = TextRenderer.MeasureText(textPart3, e.CellStyle.Font);
if (Index == 0)
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
using (Brush cellForeBrush = new SolidBrush(e.CellStyle.ForeColor))
{
e.Graphics.DrawString(textPart1, e.CellStyle.Font, Brushes.Red, rect1, StringFormat.GenericDefault);
}
rect1.X += fullsize.Width - size2.Width;
e.Graphics.DrawString(textPart2, e.CellStyle.Font, Brushes.Black, rect1);
}
else
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
using (Brush cellForeBrush = new SolidBrush(e.CellStyle.ForeColor))
{
e.Graphics.DrawString(textPart2, e.CellStyle.Font, Brushes.Black, rect1, StringFormat.GenericDefault);
}
rect1.X += fullsize.Width - size2.Width - size3.Width;
e.Graphics.DrawString(textPart1, e.CellStyle.Font, Brushes.Red, rect1, StringFormat.GenericDefault);
rect1.X += fullsize.Width - size1.Width - size2.Width;
e.Graphics.DrawString(textPart3, e.CellStyle.Font, Brushes.Black, rect1, StringFormat.GenericDefault);
}
}
}
}
}
}
}