PDA

View Full Version : سوال: مشکل این کد چیه؟



قله بلند
دوشنبه 16 تیر 1393, 18:31 عصر
سلام
دوستان مشکل این کد چیه که خروجی ها روی هم می افتند. مثلاً به جای
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);
}


}
}

}
}
}

}

قله بلند
سه شنبه 17 تیر 1393, 03:22 صبح
با عرض سلام
می خواستم بگم که من فقط در نحوه تنظیم این بخش از کد مشکل دارم


rect1.X += fullsize.Width - size2.Width - size3.Width;



خصوصاً برای حالتی که زیر رشته وسط رشته اصلی رنگی می شه. در اینجا نتیجه، درست نشون داده نمی شه و یا فاصله می افته یا اینکه رویهم افتادگی اتفاق می افته.

قله بلند
سه شنبه 17 تیر 1393, 23:56 عصر
با سلام
این کد رو جایگزین کنید درست می شه


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);

if (e.RowIndex != -1 && e.Value != null && !string.IsNullOrEmpty(textBox1.Text))
{
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;
if (Index == 0)
{
textPart1 = text.Substring(Index, length);
textPart2 = text.Substring(Index + 1);
}
else if (0 < Index && Index < text.Length && Index != (text.Length - 1))
{
textPart2 = text.Substring(0, Index);
textPart1 = text.Substring(Index, length);
textPart3 = text.Substring(Index + 1);
}
else if (Index == (text.Length - 1))
{
textPart2 = text.Substring(0, Index);
textPart1 = text.Substring(Index, length);
}
else
{
textPart1 = textPart2 = textPart3 = text;
}

TextFormatFlags format = TextFormatFlags.Left | TextFormatFlags.Top | TextFormatFlags.NoPadding;
Size size2 = TextRenderer.MeasureText(e.Graphics, textPart2, Font, Size.Empty, format);
Size size1 = TextRenderer.MeasureText(e.Graphics, textPart1, Font, Size.Empty, format);
Size size3 = TextRenderer.MeasureText(e.Graphics, textPart3, Font, Size.Empty, format);


if (Index == 0)
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart1, Font, rect1, Color.Red, format);
rect1.X += size1.Width;
TextRenderer.DrawText(e.Graphics, textPart2, Font, rect1, Color.Black, format);
}
else if (0 < Index && Index < text.Length && Index != (text.Length - 1))
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart2, Font, rect1, Color.Black, format);
rect1.X += size2.Width;
TextRenderer.DrawText(e.Graphics, textPart1, Font, rect1, Color.Red, format);
rect1.X += size1.Width;
TextRenderer.DrawText(e.Graphics, textPart3, Font, rect1, Color.Black, format);
}
else if (Index == (text.Length - 1))
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart2, Font, rect1, Color.Black, format);
rect1.X += size2.Width;
TextRenderer.DrawText(e.Graphics, textPart1, Font, rect1, Color.Red, format);
}
else
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, text, Font, rect1, Color.Black, format);
}
}
}
}
}
}
}

قله بلند
چهارشنبه 18 تیر 1393, 01:37 صبح
دوستان حالا فرض کنید که ستون دوم دیتاگرید ویو، رشته های فارسی است. با
TextFormatFlags format = TextFormatFlags.Left | TextFormatFlags.Top | TextFormatFlags.NoPadding
که چپ چین می شه. از Right استفاده می کنم، کلاً از لبه راست می زنه بیرون و از همه جالب تر، «توان» رو «ان تو »چاپ می کنه.

قله بلند
پنج شنبه 19 تیر 1393, 00:15 صبح
سلام
دوستان، اگر در دیتاگرید ویوی خودتون دو ستون داشته باشید که ستون صفرم، حاوی رشته های انگلیسی و ستون یکم، حاوی رشته های فارسی باشه، می تونید توسط این کد، رشته ای رو که کاربر در textbox مورد جستجو قرار می ده رو در دیتاگرید ویو رنگی کنید.
نکته ای که هست اینه که برای رشته های فارسی، محدودیتی وجود دارد، مثلاً:
ب خ ار در خروجی می آید
به جای
بخار
که ظاهراً در این محیط نمی شه حلش کرد چون پیچیده است.


private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex==0 && 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);

if (e.RowIndex != -1 && e.Value != null && !string.IsNullOrEmpty(textBox1.Text))
{
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;
if (Index == 0)
{
textPart1 = text.Substring(0, length);
textPart2 = text.Substring(length);

}
else if (0 < Index && Index < text.Length && Index != (text.Length - 1))
{
textPart2 = text.Substring(0, Index);
textPart1 = text.Substring(Index, length);
if (Index + length < text.Length)
{
textPart3 = text.Substring(Index + length);
}
}
else if (Index == (text.Length - 1))
{
textPart2 = text.Substring(0, Index);
textPart1 = text.Substring(Index, length);
}
else
{
textPart1 = textPart2 = textPart3 = text;
}

TextFormatFlags format = TextFormatFlags.Left | TextFormatFlags.VerticalCenter| TextFormatFlags.NoPadding;
Size size2 = TextRenderer.MeasureText(e.Graphics, textPart2, e.CellStyle.Font, Size.Empty, format);
Size size1 = TextRenderer.MeasureText(e.Graphics, textPart1, e.CellStyle.Font, Size.Empty, format);
Size size3 = TextRenderer.MeasureText(e.Graphics, textPart3, e.CellStyle.Font, Size.Empty, format);




if (Index == 0)
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart1, e.CellStyle.Font, rect1, Color.Red, format);
rect1.X += size1.Width;
TextRenderer.DrawText(e.Graphics, textPart2, e.CellStyle.Font, rect1, Color.Black, format);
}
else if (0 < Index && Index < text.Length && Index != (text.Length - 1))
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart2, e.CellStyle.Font, rect1, Color.Black, format);
rect1.X += size2.Width;
TextRenderer.DrawText(e.Graphics, textPart1, e.CellStyle.Font, rect1, Color.Red, format);
if (Index + length < text.Length)
{
rect1.X += size1.Width;
TextRenderer.DrawText(e.Graphics, textPart3, e.CellStyle.Font, rect1, Color.Black, format);
}
}
else if (Index == (text.Length - 1))
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart2, e.CellStyle.Font, rect1, Color.Black, format);
rect1.X += size2.Width;
TextRenderer.DrawText(e.Graphics, textPart1, e.CellStyle.Font, rect1, Color.Red, format);
}
else
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, text, e.CellStyle.Font, rect1, Color.Black, format);
}
}
}
}
}
}
else if (e.ColumnIndex == 1 && 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);

if (e.RowIndex != -1 && e.Value != null && !string.IsNullOrEmpty(textBox1.Text))
{
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;
if (Index == 0)
{
textPart1 = text.Substring(0, length);
textPart2 = text.Substring(length);

}
else if (0 < Index && Index < text.Length && Index != (text.Length - 1))
{
textPart2 = text.Substring(0, Index);
textPart1 = text.Substring(Index, length);
if (Index + length < text.Length)
{
textPart3 = text.Substring(Index + length);
}
}
else if (Index == (text.Length - 1))
{
textPart2 = text.Substring(0, Index);
textPart1 = text.Substring(Index, length);
}
else
{
textPart1 = textPart2 = textPart3 = text;
}

TextFormatFlags format = TextFormatFlags.Right | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPadding | TextFormatFlags.RightToLeft;
Size size2 = TextRenderer.MeasureText(e.Graphics, textPart2, e.CellStyle.Font, Size.Empty, format);
Size size1 = TextRenderer.MeasureText(e.Graphics, textPart1, e.CellStyle.Font, Size.Empty, format);
Size size3 = TextRenderer.MeasureText(e.Graphics, textPart3, e.CellStyle.Font, Size.Empty, format);
int totalWidth = size2.Width + size1.Width + size3.Width;



if (Index == 0)
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart1, e.CellStyle.Font, rect1, Color.Red, format);
rect1.X -= size1.Width;
TextRenderer.DrawText(e.Graphics, textPart2, e.CellStyle.Font, rect1, Color.Black, format);
}
else if (0 < Index && Index < text.Length && Index != (text.Length - 1))
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart2, e.CellStyle.Font, rect1, Color.Black, format);
rect1.X -= size2.Width;
TextRenderer.DrawText(e.Graphics, textPart1, e.CellStyle.Font, rect1, Color.Red, format);
if (Index + length < text.Length)
{
rect1.X -= size1.Width;
TextRenderer.DrawText(e.Graphics, textPart3, e.CellStyle.Font, rect1, Color.Black, format);
}
}
else if (Index == (text.Length - 1))
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart2, e.CellStyle.Font, rect1, Color.Black, format);
rect1.X -= size2.Width;
TextRenderer.DrawText(e.Graphics, textPart1, e.CellStyle.Font, rect1, Color.Red, format);
}
else
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, text, e.CellStyle.Font, rect1, Color.Black, format);
}
}
}
}
}
}