gpt4 book ai didi

c# - 用大写字母和粗体格式化 RichTextBox 的第一行

转载 作者:太空宇宙 更新时间:2023-11-03 11:36:12 25 4
gpt4 key购买 nike

如果我有一个 RichTextBox 并且想要输出第一行是大写字母和粗体的字体,而下一行相反,我该怎么办?

输出如下:

我的名字是
戴安娜
我的地址是
中国

最佳答案

嘿,试试这个,它有效,但如果用户键入速度非常快,例如按住“Enter”等,您可能会看到文本闪烁一秒钟。

    private void Form_Load(object sender, EventArgs e)
{
// append the function to the RichTextBox's TextChanged event
MyRichTextBox.TextChanged += Capitalize_Bold_FirstLine;
}

private void Capitalize_Bold_FirstLine(object sender, EventArgs e)
{
RichTextBox box = sender as RichTextBox;
if (box != null && box.Text != "")
{
// get the current selection text of the textbox
int ss = box.SelectionStart;
int sl = box.SelectionLength;
// get the position where the first line ends
int firstLineEnd = box.Text.IndexOf('\n');
if (firstLineEnd < 0)
firstLineEnd = box.Text.Length;

// split the lines
string[] lines = box.Text.Split('\n');
// capitalize the first line
lines[0] = lines[0].ToUpper();
// join them back and set the new text
box.Text = String.Join("\n", lines);
// select the first line and make it bold
box.SelectionStart = 0;
box.SelectionLength = firstLineEnd;
box.SelectionFont = new Font(box.Font, FontStyle.Bold);
// select the rest and make it regular
box.SelectionStart = firstLineEnd;
box.SelectionLength = box.Text.Length - firstLineEnd;
box.SelectionFont = new Font(box.Font, FontStyle.Regular);
// go back to what the user had selected
box.SelectionStart = ss;
box.SelectionLength = sl;
}
}

关于c# - 用大写字母和粗体格式化 RichTextBox 的第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6226924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com