我如何从字符串[]读取每第二行

本文关键字:二行 读取 字符串 | 更新日期: 2023-09-27 18:08:40

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

但是这段代码将把所有的文本所有的行都涂成红色。我要一条红线一条绿线。使用DrawString

有10行。假设我要读每两行。现在读取每一行改为只读取第2、4、6、8、10行然后,如果我想知道如何读取例如每3行:3,6,9

我想把第一个字符串涂成红色,把第二个字符串涂成绿色。

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

所有的行都是这样,一红一绿

我如何从字符串[]读取每第二行

试试这个-

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

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

int jump = |whatever you want|
for (int i = 0; i < m_text.Length; i+jump)
{
    test = m_text[i];
}
尝试使用for循环本身。试着自己. .1. 首字母应该是22. 步长23.将该行涂成绿色(因为它是2的倍数),将第一行由mtext[i-1]涂成红色。