110,567
社区成员
发帖
与我相关
我的任务
分享
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((char)e.KeyData >= '0' && (char)e.KeyData <= '9')
{
richTextBox1.Select(richTextBox1.Text.Length, 1);
richTextBox1.SelectionColor = Color.Red;
}
else
{
richTextBox1.Select(richTextBox1.Text.Length, 1);
richTextBox1.SelectionColor = Color.Cyan;
}
}
/// <summary>
/// 键入时文本根据类型变色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
//数字
if (e.KeyValue > 95 && e.KeyValue < 106)
{
richTextBox1.Select(richTextBox1.Text.Length, 1);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.Show();
}
else//非数字
{
richTextBox1.Select(richTextBox1.Text.Length , 1);
richTextBox1.SelectionColor = Color.Purple;
}
}
private void button1_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if (cd.ShowDialog() == DialogResult.OK)
{
richTextBox1.SelectionColor = cd.Color;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DifferentColor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
richTextBox1.Focus();
}
private void richTextBox1_Enter(object sender, EventArgs e)
{
richTextBox1.SelectionColor = colorDialog1.Color;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DifferentColor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if (cd.ShowDialog() == DialogResult.OK)
{
textBox1.ForeColor = cd.Color;
}
}
}
}