text color in textbox

  • Thread starter Thread starter Park
  • Start date Start date
P

Park

In the multiple line textbox, is it possible that every sentences shows
different color ?
For example,
test sentence 1 : red
test sentence 2 : blue
....
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication3
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListView listView1;

ArrayList show = new ArrayList();
private System.Windows.Forms.ColumnHeader columnHeader1;
int flag;

public Form1()
{
InitializeComponent();
}


protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 32);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
this.textBox1.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(this.keyPressed1);
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 192);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "Show";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1});
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(32, 64);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(216, 120);
this.listView1.TabIndex = 3;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Width = 208;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.listView1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

void keyPressed1(object o, KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
show.Add(textBox1.Text);
textBox1.Clear();
}
}

private void button1_Click(object sender, System.EventArgs e)
{
for(int k= 0; k<show.Count; k++)
{
string [] temp = new string[1];
temp[0] = show[k].ToString();
ListViewItem list = new ListViewItem(temp[0]);
if(flag==0)
{
list.ForeColor = Color.Red;
flag = 1;
listView1.Items.Add(list);
continue;
}

if(flag==1)
{
list.ForeColor = Color.Blue;
flag = 0;
listView1.Items.Add(list);
continue;
}
}
}
}
}
 
Add a RichTextBox and a Button to a form.

Put this code in...

private void richTextBox1_TextChanged(object sender, System.EventArgs e)
{
string[] lines = richTextBox1.Lines;
int pos = 0;
for (int i = 0; i < lines.Length; i++)
{
int lineLength = lines.Length;
richTextBox1.SelectionStart = pos;
richTextBox1.SelectionLength = lineLength;
pos += lineLength + 1;
if ((i % 2) == 0)
richTextBox1.SelectionColor = Color.Red;
else
richTextBox1.SelectionColor = Color.Blue;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++)
sb.Append("line: 1 - red" + Environment.NewLine + "line: 2 - blue" +
Environment.NewLine);
richTextBox1.Text = sb.ToString();
}

.... wire up the events.

HTH;
Eric Cadwell
http://www.origincontrols.com
 
Back
Top