RichTextBox每行使用不同的文字顏色

逯文杰發表於2024-08-09
private void btnStart_Click(object sender, EventArgs e)
        {
            var shaps = new List<Shape>
            {
                new Rectangle(),
                new Triangle(),
                new Cicrle()
            };

            foreach (Shape s in shaps)
            {
                rtxtDisplay.SelectionStart = rtxtDisplay.Text.Length;

                if (s.Draw().Contains(""))
                {
                    rtxtDisplay.SelectionColor = Color.Red;
                }
                else if (s.Draw().Contains("矩形"))
                {
                    rtxtDisplay.SelectionColor = Color.Green;
                }
                else
                {
                    rtxtDisplay.SelectionColor = Color.Yellow;
                }
                rtxtDisplay.AppendText(s.Draw());
                rtxtDisplay.AppendText(System.Environment.NewLine);
            }
            rtxtDisplay.AppendText(System.Environment.NewLine);
        }

相關文章