iTE*_*Egg 1 .net format richtextbox winforms
有史以来第一次,我正在研究RichTextBoxC#Windows窗体中的控件.我知道我的应用程序中需要这个控件,因为TextBox它太简单了.
我有以下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _19_richtextbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void richTextBoxHome_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == (char)(Keys.Return))
{
richTextBoxChat.AppendText("Home:" + richTextBoxHome.Text + "\n");
richTextBoxHome.Clear();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
目前我只想要在一个中输入的任何内容在击中时RichTextBox显示在另一个RichTextBox上Return.
问题是,每次我点击时Return,数据都被转移到另一个控件,但第一个控件在光标前留下了回车符.每次我击中都会发生这种情况Return.两个控件都接受多行输入.我如何让它停止这样做?
我想用一种方法将"Home:"部分用粗体表示.
我在搜索中发现的信息非常少.以下是我能理解的唯一实际代码.
rtb1.Rtf = @"{\rtf1\ansi {\b hello} {\i World}}" ;
richTextBox1.Rtf = @"{\rtf1\ansi This is in \b bold\b0.}";
Run Code Online (Sandbox Code Playgroud)
我不知道如何继续这个信息.我只想让richtextbox以粗体显示"Home:"和"Away:",并能够处理文本中的URL.
请告知我在谷歌搜索这个potic时应该指定什么,或者你能想到的任何参考都会有很大的帮助.
再次感谢您的兴趣.
我在我的问题上取得了进展,并想分享.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _19_richtextbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void richTextBoxHome_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)(Keys.Enter))
{
e.Handled = true;
// richTextBoxChat.Rtf = @"{\rtf1 \b Home: \bO}" + @richTextBoxHome;
// do not delete next 2 lines
//string test = @"{\rtf1 \b Home: \b0";
//test = test + richTextBoxHome.Rtf + "}";
string chatBuffer = richTextBoxChat.Rtf;
string buffer = @"{\rtf1";
buffer = buffer + chatBuffer;
buffer = buffer + @"\b Home:\b0";
buffer = buffer + richTextBoxHome.Rtf + "}";
// MessageBox.Show(buffer);
richTextBoxChat.Rtf = buffer;
//do not delete the following 2 lines
//richTextBoxChat.AppendText("Home:" + richTextBoxHome.Text);
richTextBoxHome.Clear();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
只需要弄清楚如何摆脱文本中的新行/回车.
任何提示最受欢迎.
谢谢.
没锻炼的那么好.结果使用了以下代替.
this.richTextBoxChat.SelectionFont = new Font(this.richTextBoxChat.Font.FontFamily, this.Font.Size, FontStyle.Bold);
richTextBoxChat.AppendText("Home: ");
this.richTextBoxChat.SelectionFont = new Font(this.richTextBoxChat.Font.FontFamily, this.Font.Size, FontStyle.Regular);
richTextBoxChat.AppendText(richTextBoxHome.Text);
richTextBoxChat.ScrollToCaret();
richTextBoxHome.Clear();
Run Code Online (Sandbox Code Playgroud)
Mic*_*l G 10
您可以设置要处理的事件,以防止进一步处理返回的keydown事件
试试这个:
private void richTextBoxHome_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == (char)(Keys.Return))
{
richTextBoxChat.AppendText("Home:" + richTextBoxHome.Text + "\n");
richTextBoxHome.Clear();
e.Handled = true;
e.SuppressKeyPress = true;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:添加e.SuppressKeyPress = true; 科里查尔顿在他的帖子中指出了这一点.
编辑:同样,正如其他人提到的那样,使用KeyPress事件处理程序,因此如果某人按住enter/return,则会一次又一次地触发事件.用法示例如下:
private void richTextBoxHome_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.Equals((Char) Keys.Enter))
{
richTextBoxChat.AppendText("Home:" + richTextBoxHome.Text + "\n");
richTextBoxHome.Clear();
e.Handled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:此外,您可以在RichTextBox上设置一个属性,以便不允许将MultiLine字符串输入文本框.当使用多行粘贴内容时,这确实会产生问题,它只会占用第一行.
对于粗体字: 您可以更改RichTextBox的SelectionFont以设置其字体属性:
private readonly Font BoldSelectionFont = new Font("Arial", 9.0f, FontStyle.Bold);
private readonly Font RegSelectionFont = new Font("Arial", 9.0f, FontStyle.Regular);
...
richTextBoxChat.SelectionFont = BoldSelectionFont;
richTextBoxChat.AppendText("Home: ");
richTextBoxChat.SelectionFont = RegSelectionFont;
richTextBoxChat.AppendText(richTextBoxHome.Text + "\n");
...
Run Code Online (Sandbox Code Playgroud)