我怎样才能从字符串[]中读取每一行?

时间:2014-06-13 11:17:47

标签: c# .net winforms

private void OnPaint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.FillRectangle(new SolidBrush(this.BackColor), this.ClientRectangle);
            GraphicsPath path = new GraphicsPath();
            int visibleLines = 0;

            for (int i = m_text.Length - 1; i >= 0; i--)
            {

                Point pt = new Point((int)((this.ClientSize.Width - e.Graphics.MeasureString(m_text[i], m_font).Width) / 2),
                    (int)(m_scrollingOffset + this.ClientSize.Height - (m_text.Length - i) * m_font.Size));
                if ((pt.Y + this.Font.Size > 0) && (pt.Y < this.Height))
                {
                    path.AddString(m_text[i], m_font.FontFamily, (int)m_font.Style, m_font.Size,
                        pt, StringFormat.GenericTypographic);

                    visibleLines++;
                }
                    Color ccc = Color.Red;
                    if (m_text.Length > 1)
                    test = m_text[i];
                    Font drawFonts1 = new Font("Arial", 16);
                    e.Graphics.DrawString(test, drawFonts1, new SolidBrush(ccc), pt);
            }
            if ((visibleLines == 0) && (m_scrollingOffset < 0))
            {
                m_scrollingOffset = (int)this.Font.SizeInPoints * m_text.Length;
            }           
            int topSizeWidth = (int)(this.Width * m_topPartSizePercent / 100.0f);
            path.Warp(
                new PointF[4] 
                { 
                    new PointF((this.Width - topSizeWidth) / 2, 0),
                    new PointF(this.Width - (this.Width - topSizeWidth) / 2, 0),
                    new PointF(0, this.Height),
                    new PointF(this.Width, this.Height)
                },
                new RectangleF(this.ClientRectangle.X, this.ClientRectangle.Y, this.ClientRectangle.Width, this.ClientRectangle.Height),
                null,
                WarpMode.Perspective
                );
            e.Graphics.FillPath(new SolidBrush(this.ForeColor), path);
            path.Dispose();
        }

在这个事件中我添加了这段代码:

Color ccc = Color.Red;
if (m_text.Length > 1)
test = m_text[i];
Font drawFonts1 = new Font("Arial", 16);
e.Graphics.DrawString(test, drawFonts1, new SolidBrush(ccc), pt);

但是这段代码会为Red中的所有行着色。我希望Red中的一行为Green。 使用DrawString。

共有10行。让我们说我想读每两行。 而是现在读取每一行只读取行:2,4,6,8,10 然后,如果我想知道如何阅读每3行:3,6,9

我想用红色为第一个字符串着色,并用绿色为第二个字符串着色。

Line 1: Red
Line 2: Green
Line 3: Red
Line 4: Green

所有线路上都有一个红色的绿色。

3 个答案:

答案 0 :(得分:2)

试试这个 -

var brushes = new []{new SolidBrush(Color.Red), new SolidBrush(Color.Green)};

for (int i = 0; i < m_text.Lenght; i++)
{
    ... //other codes
    e.Graphics.DrawString(test, drawFonts1, brushes [i % 2], pt);
}

答案 1 :(得分:1)

嗯,这不是一个难题。 声明一个变量,其中包含您想要“跳转”的行数,然后在for循环中使用它。

int jump = |whatever you want|
for (int i = 0; i < m_text.Length; i+jump)
{
    test = m_text[i];
}

答案 2 :(得分:0)

Try using for loop itself. Try yourself..
1. Initial should be 2
2. Step value 2
3. Color the line in Green Color(since it is in multiples of 2), Color the first line by mtext[i-1] with red color.