نمایش نتایج 1 تا 5 از 5

نام تاپیک: مشکل این کد چیه؟

  1. #1

    مشکل این کد چیه؟

    سلام
    دوستان مشکل این کد چیه که خروجی ها روی هم می افتند. مثلاً به جای
    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);
    }


    }
    }

    }
    }
    }

    }


  2. #2

    نقل قول: مشکل این کد چیه؟

    با عرض سلام
    می خواستم بگم که من فقط در نحوه تنظیم این بخش از کد مشکل دارم

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



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

  3. #3

    نقل قول: مشکل این کد چیه؟

    با سلام
    این کد رو جایگزین کنید درست می شه

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

  4. #4

    نقل قول: مشکل این کد چیه؟

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

  5. #5

    نقل قول: مشکل این کد چیه؟

    سلام
    دوستان، اگر در دیتاگرید ویوی خودتون دو ستون داشته باشید که ستون صفرم، حاوی رشته های انگلیسی و ستون یکم، حاوی رشته های فارسی باشه، می تونید توسط این کد، رشته ای رو که کاربر در 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);
    }
    }
    }
    }
    }
    }

تاپیک های مشابه

  1. مشکل این کد چیه؟
    نوشته شده توسط evilboy در بخش برنامه نویسی در 6 VB
    پاسخ: 4
    آخرین پست: پنج شنبه 14 دی 1385, 11:01 صبح
  2. مشکل این برنامه چیه؟
    نوشته شده توسط shahram126 در بخش برنامه نویسی اسمبلی خانواده x86
    پاسخ: 5
    آخرین پست: چهارشنبه 22 آذر 1385, 02:32 صبح
  3. مشکل این کد چیه؟
    نوشته شده توسط dastiary در بخش برنامه نویسی در 6 VB
    پاسخ: 4
    آخرین پست: دوشنبه 22 آبان 1385, 19:53 عصر
  4. مشکل این کد چیه؟
    نوشته شده توسط Tarrah در بخش برنامه نویسی با Borland C++‎ Builder
    پاسخ: 5
    آخرین پست: یک شنبه 07 آبان 1385, 06:57 صبح

برچسب های این تاپیک

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •