本文整理汇总了C#中System.Windows.Forms.RichTextBox.Select方法的典型用法代码示例。如果您正苦于以下问题:C# RichTextBox.Select方法的具体用法?C# RichTextBox.Select怎么用?C# RichTextBox.Select使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.RichTextBox
的用法示例。
在下文中一共展示了RichTextBox.Select方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HighlightText
/// <summary>
/// Highlight all words that matches.
/// </summary>
/// <param name="myRtb">Source text box</param>
/// <param name="word">Word to match.</param>
public void HighlightText(RichTextBox myRtb, string word)
{
myRtb.SelectAll();
myRtb.SelectionBackColor = Color.White;
myRtb.SelectionColor = Color.Black;
myRtb.DeselectAll();
if (word == "")
{
return;
}
int s_start = myRtb.SelectionStart, startIndex = 0, index;
while ((index = myRtb.Text.IndexOf(word, startIndex)) != -1)
{
myRtb.Select(index, word.Length);
myRtb.SelectionColor = Color.White;
myRtb.SelectionBackColor = Color.Green;
startIndex = index + word.Length;
}
myRtb.SelectionStart = s_start;
myRtb.SelectionLength = 0;
myRtb.SelectionColor = Color.Black;
myRtb.SelectionBackColor = Color.White;
}
示例2: setFontStyle
private void setFontStyle(RichTextBox rtb, FontStyle style)
{
if (rtb.SelectionLength == 0)
setCharFontStyle(rtb, style);
else
{
// to avoid screen refreshing, we create a fake RichTextBox
using (RichTextBox a = new RichTextBox())
{
a.SuspendLayout();
a.SelectedRtf = rtb.SelectedRtf;
a.SelectAll();
int selectionStart = a.SelectionStart;
int selectionLength = a.SelectionLength;
int selectionEnd = selectionStart + selectionLength;
for (int x = selectionStart; x < selectionEnd; ++x)
{
// Set temporary selection
a.Select(x, 1);
// Toggle font style of the selection
setCharFontStyle(a, style);
}
// Restore the original selection
a.SelectAll();
rtb.SelectedRtf = a.SelectedRtf;
}
}
}
示例3: FormattedAnswer
public string FormattedAnswer()
{
RichTextBox rtb = new RichTextBox();
rtb.Text = Answer;
rtb.Font = new Font("Segoe UI", 10);
if (IsCorrect) {
rtb.Select(0, Answer.Length);
rtb.SelectionColor = Color.Green;
rtb.SelectionAlignment = HorizontalAlignment.Center;
rtb.DeselectAll();
return rtb.Rtf;
}
rtb.Text += " " + RightAnswer;
rtb.Select(0, Answer.Length);
rtb.SelectionFont = new Font(rtb.SelectionFont, FontStyle.Strikeout);
rtb.Select(Answer.Length + 1, RightAnswer.Length);
rtb.SelectionColor = Color.Green;
rtb.SelectAll();
rtb.SelectionAlignment = HorizontalAlignment.Center;
rtb.DeselectAll();
return rtb.Rtf;
}
示例4: SpellCompare
/// <summary>
/// Compares two spells
/// </summary>
/// <param name="rtb1">RichTextBox 1 in left</param>
/// <param name="rtb2">RichTextBox 2 in right</param>
/// <param name="spell1">Compare Spell 1</param>
/// <param name="spell2">Compare Spell 2</param>
public SpellCompare(RichTextBox rtb1, RichTextBox rtb2, SpellInfoHelper spell1, SpellInfoHelper spell2)
{
new SpellInfo(rtb1, spell1);
new SpellInfo(rtb2, spell2);
var strsl = rtb1.Text.Split('\n');
var strsr = rtb2.Text.Split('\n');
var pos = 0;
foreach (var str in strsl)
{
pos += str.Length + 1;
rtb1.Select(pos - str.Length - 1, pos - 1);
if (rtb2.Find(str, RichTextBoxFinds.WholeWord) != -1)
rtb1.SelectionBackColor = str.ContainsText(_words) ? rtb1.BackColor : Color.Cyan;
else
rtb1.SelectionBackColor = Color.Salmon;
}
pos = 0;
foreach (var str in strsr)
{
pos += str.Length + 1;
rtb2.Select(pos - str.Length - 1, pos - 1);
if (rtb1.Find(str, RichTextBoxFinds.WholeWord) != -1)
rtb2.SelectionBackColor = str.ContainsText(_words) ? rtb2.BackColor : Color.Cyan;
else
rtb2.SelectionBackColor = Color.Salmon;
}
}
示例5: AppendMsg
/// <summary>
/// 在富文本上打印消息
/// </summary>
/// <param name="richTextBox1">所在打印的富文本</param>
/// <param name="color">打印字体颜色</param>
/// <param name="text">要打印的文本</param>
/// <param name="AutoTime">是否在每条打印结果前追加时间</param>
public void AppendMsg(RichTextBox richTextBox1, Color color, string text,bool AutoTime)
{
richTextBox1.BeginInvoke(new ThreadStart(() =>
{
lock (richTextBox1)
{
//为控件输入焦点
richTextBox1.Focus();
//检查文本框过长
if (richTextBox1.TextLength > 100000 )
{
richTextBox1.Clear();
}
//得到有格式的文本
using (var temp = new RichTextBox())
{
temp.SelectionColor = color;
if (AutoTime)
temp.AppendText(DateTime.Now.ToString("yyyyMMdd HH:mm:ss"));
temp.AppendText(text);
//追加文本
richTextBox1.Select(richTextBox1.Rtf.Length, 0);
richTextBox1.SelectedRtf = temp.Rtf;
}
//设定光标所在位置
//richTextBox1.SelectionStart = richTextBox1.TextLength;
//滚动到当前光标处
//richTextBox1.ScrollToCaret();
}
}));
}
示例6: BuildSyntaxView
/// <summary>
/// Uses Roslyn technology to build a syntax tree from, and acquire
/// diagnostics about, the source code.
/// Diagnostics are rendered into the rich text box. If the compiler
/// has highlighted a non-empty span then it is highlighted otherwise
/// a red X is display at the error pixel point of an empty span.
/// </summary>
/// <param name="tbSource">Text box with C# source code.</param>
/// <param name="richResult">Rich text box to render syntax highlighting into.</param>
/// <remarks>
/// Only the first source code syntax error is highlighted.
/// </remarks>
static void BuildSyntaxView(TextBox tbSource, RichTextBox richResult)
{
richResult.Text = tbSource.Text;
// Roslyn!! - SyntaxTree.*
var syntaxTree = SyntaxTree.ParseText(tbSource.Text);
var diags = syntaxTree.GetDiagnostics();
// Display all compiler diagnostics in console
Console.WriteLine("============================");
Console.WriteLine("Compiler has {0} diagnostic messages.", diags.Count());
// Roslyn again!!
foreach (var d in diags)
Console.WriteLine("> {0}", d.Info.GetMessage());
Console.WriteLine("-------------------------");
List<Point> issuePoints = new List<Point>();
picX.Visible = false;
foreach (var d in diags)
{
// More Roslyn !! - d.*
if (d.Location.IsInSource)
{
var origFore = Console.ForegroundColor;
var origBack = Console.BackgroundColor;
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write(tbSource.Text.Substring(d.Location.SourceSpan.Start, d.Location.SourceSpan.Length));
// Hey Roslyn !! - d.*
int lineEndOffset = d.Location.GetLineSpan(true).StartLinePosition.Line;
// Note: line endings in rich text box use one character where textbox input source uses two
richResult.Select(d.Location.SourceSpan.Start - lineEndOffset, d.Location.SourceSpan.Length);
richResult.SelectionColor = Color.Yellow;
richResult.SelectionBackColor = Color.Red;
if (d.Location.SourceSpan.Length == 0)
issuePoints.Add(richResult.GetPositionFromCharIndex(d.Location.SourceSpan.Start));
Console.BackgroundColor = origBack;
Console.ForegroundColor = origFore;
Console.WriteLine(tbSource.Text.Substring(d.Location.SourceSpan.Start + d.Location.SourceSpan.Length));
break; // Stop after first error is highlighted
// No dealing with multiple errors or overlaps in this sample.
}
}
// Put the X on the first error
if (issuePoints.Count != 0)
{
picX.Location = issuePoints[0];
picX.Visible = true;
}
Console.WriteLine("============================");
}
示例7: appendTextBox
public void appendTextBox(RichTextBox textBox, string strInput, Color fontColor, Font fontType)
{
int p1 = textBox.TextLength; //取出未添加时的字符串长度
textBox.AppendText(strInput); //保留每行的所有颜色
int p2 = strInput.Length; //取出要添加的文本的长度
textBox.Select(p1, p2); //选中要添加的文本
textBox.SelectionColor = fontColor; //设置要添加的文本的字体色
textBox.SelectionFont = fontType; //设置要添加的文本的字体
}
示例8: AppendText
void AppendText(RichTextBox box, Color color, string text)
{
int start = box.TextLength;
box.AppendText(text);
int end = box.TextLength;
box.Select(start, end-start);
box.SelectionColor = color;
box.SelectionLength = 0;
}
示例9: ChangeFontStyle
public static void ChangeFontStyle(FontStyle style, RichTextBox richTextBox1)
{
if (style != FontStyle.Bold && style != FontStyle.Italic &&
style != FontStyle.Underline)
throw new System.InvalidProgramException("字体格式错误");
RichTextBox tempRichTextBox = new RichTextBox(); //将要存放被选中文本的副本
int curRtbStart = richTextBox1.SelectionStart;
int len = richTextBox1.SelectionLength;
int tempRtbStart = 0;
Font font = richTextBox1.SelectionFont;
if (len <= 1 && font != null) //与上边的那段代码类似,功能相同
{
if (style == FontStyle.Bold && font.Bold ||
style == FontStyle.Italic && font.Italic ||
style == FontStyle.Underline && font.Underline)
{
richTextBox1.SelectionFont = new Font(font, font.Style ^ style);
}
else if (style == FontStyle.Bold && !font.Bold ||
style == FontStyle.Italic && !font.Italic ||
style == FontStyle.Underline && !font.Underline)
{
richTextBox1.SelectionFont = new Font(font, font.Style | style);
}
return;
}
tempRichTextBox.Rtf = richTextBox1.SelectedRtf;
tempRichTextBox.Select(len - 1, 1); //选中副本中的最后一个文字
//克隆被选中的文字Font,这个tempFont主要是用来判断
//最终被选中的文字是否要加粗、去粗、斜体、去斜、下划线、去下划线
Font tempFont = (Font)tempRichTextBox.SelectionFont.Clone();
//清空2和3
for (int i = 0; i < len; i++)
{
tempRichTextBox.Select(tempRtbStart + i, 1); //每次选中一个,逐个进行加粗或去粗
if (style == FontStyle.Bold && tempFont.Bold ||
style == FontStyle.Italic && tempFont.Italic ||
style == FontStyle.Underline && tempFont.Underline)
{
tempRichTextBox.SelectionFont =
new Font(tempRichTextBox.SelectionFont,
tempRichTextBox.SelectionFont.Style ^ style);
}
else if (style == FontStyle.Bold && !tempFont.Bold ||
style == FontStyle.Italic && !tempFont.Italic ||
style == FontStyle.Underline && !tempFont.Underline)
{
tempRichTextBox.SelectionFont =
new Font(tempRichTextBox.SelectionFont,
tempRichTextBox.SelectionFont.Style | style);
}
}
tempRichTextBox.Select(tempRtbStart, len);
richTextBox1.SelectedRtf = tempRichTextBox.SelectedRtf; //将设置格式后的副本拷贝给原型
richTextBox1.Select(curRtbStart, len);
}
示例10: AppendInternal
public static void AppendInternal(RichTextBox richTextBox, Color colorFore, Color colorBack, FontStyle newStyle, string text)
{
if(richTextBox != null && !string.IsNullOrEmpty(text) && !richTextBox.IsDisposed)
{
if(richTextBox.InvokeRequired)
{
richTextBox.BeginInvoke(new MethodInvoker(delegate() { AppendInternal(richTextBox, colorFore, colorBack, newStyle, text); }));
}
else
{
lock(richTextBox)
{
richTextBox.SuspendLayout();
//Truncate as necessary
if(richTextBox.Text.Length + text.Length > _maxConsoleTextLength)
{
int truncateLength = _maxConsoleTextLength / 4;
int endmarker = richTextBox.Text.IndexOf('\n', truncateLength) + 1;
if(endmarker < truncateLength)
endmarker = truncateLength;
richTextBox.Select(0, endmarker);
richTextBox.Cut();
}
int originalTextEnd = richTextBox.Text.Length;
richTextBox.AppendText(text);
richTextBox.Select(originalTextEnd, text.Length);
if(colorFore != Color.Empty)
richTextBox.SelectionColor = colorFore;
if(colorBack != Color.Empty)
richTextBox.SelectionBackColor = colorBack;
//if(newStyle != richTextBox.Font.Style)
richTextBox.SelectionFont = new Font(richTextBox.Font, newStyle);
richTextBox.SelectionLength = 0;
richTextBox.ScrollToCaret();
richTextBox.ResumeLayout();
richTextBox.Update();
}
}
}
}
示例11: SpellCompare
string[] words = new[] { "=====" }; // todo: more wodrs
#endregion Fields
#region Constructors
/// <summary>
/// Compares two spells
/// </summary>
/// <param name="rtb1">RichTextBox 1 in left</param>
/// <param name="rtb2">RichTextBox 2 in right</param>
/// <param name="spell1">Compare Spell 1</param>
/// <param name="spell2">Compare Spell 2</param>
public SpellCompare(RichTextBox rtb1, RichTextBox rtb2, SpellEntry spell1, SpellEntry spell2)
{
new SpellInfo(rtb1, spell1);
new SpellInfo(rtb2, spell2);
string[] strsl = rtb1.Text.Split('\n');
string[] strsr = rtb2.Text.Split('\n');
int pos = 0;
foreach (string str in strsl)
{
pos += str.Length + 1;
rtb1.Select(pos - str.Length - 1, pos - 1);
if (rtb2.Find(str, RichTextBoxFinds.WholeWord) != -1)
{
if (str.ContainsText(words))
{
rtb1.SelectionBackColor = rtb1.BackColor;
}
else
{
rtb1.SelectionBackColor = Color.Cyan;
}
}
else
{
rtb1.SelectionBackColor = Color.Salmon;
}
}
pos = 0;
foreach (string str in strsr)
{
pos += str.Length + 1;
rtb2.Select(pos - str.Length - 1, pos - 1);
if (rtb1.Find(str, RichTextBoxFinds.WholeWord) != -1)
{
if (str.ContainsText(words))
{
rtb2.SelectionBackColor = rtb2.BackColor;
}
else
{
rtb2.SelectionBackColor = Color.Cyan;
}
}
else
{
rtb2.SelectionBackColor = Color.Salmon;
}
}
}
示例12: AppendText
void AppendText(RichTextBox box, Color color, string text)
{
int start = box.TextLength;
box.AppendText(text + Environment.NewLine);
int end = box.TextLength;
box.Select(start, end - start);
{
box.SelectionColor = color;
}
box.SelectionLength = 0;
}
示例13: Update
internal static void Update(RichTextBox box)
{
if (appendQueue.IsEmpty) return;
var start = box.SelectionStart;
var len = box.SelectionLength;
var scroll = box.TextLength == start;
LogLine result;
box.Select(box.TextLength, 0);
while (appendQueue.TryDequeue(out result)) {
box.SelectionColor = result.color;
box.AppendText(result.line+"\r\n");
writer.WriteLine(result.line);
lines++;
}
writer.Flush();
if (lines > maxLines) {
box.ReadOnly = false;
while (lines > maxLines) {
var lineLen = box.Text.IndexOf('\n') + 1;
box.Select(0, lineLen);
box.Cut();
start -= lineLen;
lines--;
}
box.ReadOnly = true;
}
if (scroll) {
box.Select(box.TextLength, 0);
box.ScrollToCaret();
} else if (start + len > 0) {
if (start < 0) {
len += start;
start = 0;
}
box.Select(start, len);
}
}
示例14: AppendTextWithGivenColor
private void AppendTextWithGivenColor(RichTextBox box, Color color, string text)
{
int start = box.TextLength;
box.AppendText(text);
int end = box.TextLength;
box.Select(start, end - start);
{
box.SelectionColor = color;
// could set box.SelectionBackColor, box.SelectionFont too.
}
box.SelectionLength = 0; // clear
}
示例15: applyStyleToTextbox
public static void applyStyleToTextbox(BBCodeStyle style, RichTextBox tf, int selStart, int selEnd)
{
if (style == null || tf == null || selEnd <= selStart || selEnd < 0)
return;
tf.Select(selStart, selEnd);
FontStyle fontStyle = tf.Font.Style;
String fontName = tf.Font.Name;
float fontSize = tf.Font.Size;
if (style.isBold == StateMode.ON)
fontStyle |= FontStyle.Bold;
else if (style.isBold == StateMode.OFF)
fontStyle &= ~FontStyle.Bold;
if (style.isItalic == StateMode.ON)
fontStyle |= FontStyle.Italic;
else if (style.isItalic == StateMode.OFF)
fontStyle &= ~FontStyle.Italic;
if (style.isUnderlined == StateMode.ON)
fontStyle |= FontStyle.Underline;
else if (style.isUnderlined == StateMode.OFF)
fontStyle &= ~FontStyle.Underline;
if (style.isStriked == StateMode.ON)
fontStyle |= FontStyle.Strikeout;
else if (style.isStriked == StateMode.OFF)
fontStyle &= ~FontStyle.Strikeout;
if (style.fontSize > 0)
fontSize = (float)style.fontSize;
else if (style.fontSize < 0)
fontSize += (float)style.fontSize;
if (style.fontName != null && style.fontName.Length > 0)
fontName = style.fontName;
if (style.foreColor != null)
tf.SelectionColor = Color.FromArgb((int)((uint)(0xFF000000) | (uint)(style.foreColor.color & 0xFFFFFF)));
if (style.backColor != null)
tf.SelectionBackColor = Color.FromArgb((int)((uint)(0xFF000000) | (uint)(style.backColor.color & 0xFFFFFF)));
Font font = new Font(fontName, (float)(fontSize > 1.0f ? fontSize : 1.0f), fontStyle);
tf.SelectionFont = font;
}