【问题标题】:How to convert all input to a RichTextBox to upper case?如何将所有输入到 RichTextBox 转换为大写?
【发布时间】:2022-03-18 03:23:36
【问题描述】:

为什么在 C# 中我可以做到这一点:

richTextBox1.Rtf = richTextBox1.Rtf.ToLower();

但我做不到:

richTextBox1.Rtf = richTextBox1.Rtf.ToUpper();

我正在使用 Visual Studio 2015

请帮帮我>谢谢!

如果我使用: RichTextBox1.Text = RichTextBox1.Text.ToUpper();

当我点击按钮1时

但我想要:

编辑 2: 我试试:

richTextBox1.SelectAll();
string[] textArray = richTextBox1.SelectedText.Split(new char[] { '\n', '\t' });
foreach (string strText in textArray)
   {
   if (!string.IsNullOrEmpty(strText) && strText != "rtf")
   richTextBox1.Rtf = richTextBox1.Rtf.Replace(strText, strText.ToUpper());
   }

当我点击按钮1时

它不适用于“tôi tên là”

我该怎么做????请

【问题讨论】:

  • 试试RichTextBox1.Text.ToUpper();
  • 能否提供表格的代码
  • @Quang:你不应该删除问题的内容。但保持原样。这样以后如果有人需要,他们可以参考这篇文章的问答。您告诉社区您的问题已解决的方式是接受解决方案作为答案。请回滚您的编辑。
  • @Mohit Shrivastava,我明白你的意思。但这一次我不能那样做……我很抱歉……

标签: c#


【解决方案1】:

如果RTF 是格式化字符串,这可能会为您解决问题。

RichTextBox1.SelectAll();
string[] textArray = RichTextBox1.SelectedText.Split(new char[] { '\n', '\t' });
foreach(string strText in textArray)
{
    if(!string.IsNullOrEmpty(strText) && strText != "rtf")
        RichTextBox1.Rtf = RichTextBox1.Rtf.Replace(strText, strText.ToUpper());
}

RichTextBox.Rtf 将包含类似 {\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang17417{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}} \viewkind4\uc1\trowd\trgaph108\trleft5\trbrdrt\brdrs\brdrw10 \trbrdrl\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10 \clbrdrt\brdrw15\brdrs\clbrdrl\brdrw15\brdrs\clbrdrb\brdrw15\brdrs\clbrdrr\brdrw15\brdrs... 的数据,当我们尝试对这种字符串进行操作时,它会抛出错误,即 文件格式无效但是为了避免这个问题,我们可以使用 RichTextBox 的 SelectedTextText,我们只获得 RichTextBox 的内部文本。然后我们可以用大写文本替换文本。

编辑

在将 Unicode 字符串更改为大写的要求之后。 RTF 将tôi tên là 更改为类似\cf1\f1\fs20 t\'f4i t\'ean l\'e0 的内容,当我们运行tôi tên là 行的代码Replace(strText, strText.ToUpper()); 时,在RichTextBox1.Rtf 中找不到行,所以我使用了if 条件来查看字符串是否存在于rtf 与否,如果是,则正常代码将起作用,但如果 unicode 字符串存在,则将其更改为 rtf 以查看 unicode 字符串的 rtf 版本。

另一个是 Stephan Bauer 正确地建议如果 rrtft,f,rt,tf 存在于该行中,则它会给出错误,从而改变大小写。所以也添加了该功能。

string somevar = "rtf"; //string to eliminate
List<string> subStrings = new List<string>();
for(int i = 0; i < somevar.Length; i++)
{
    subStrings.AddRange(GetAllSubStrings(somevar, i + 1));
}
RichTextBox1.Rtf = RichTextBox1.Rtf.ToLower();
string[] textArray = RichTextBox1.Text.Split(new char[] { '\n', '\t' });
foreach(string strText in textArray)
{
    if(!string.IsNullOrEmpty(strText))
        if(!subStrings.Any(x => strText == x))
        {
            if(RichTextBox1.Rtf.Contains(strText))
            {
                RichTextBox1.Rtf = RichTextBox1.Rtf.Replace(strText, strText.ToUpperInvariant());
            }
            else
            {
                RichTextBox rt = new RichTextBox();
                rt.Text = strText;
                string rtftext = rt.Rtf.Substring(rt.Rtf.IndexOf("fs17") + 4);
                rtftext = rtftext.Substring(0, rtftext.IndexOf("par")-1);
                RichTextBox1.Rtf = RichTextBox1.Rtf.Replace(rtftext, strText.ToUpperInvariant());
            }
        }
}

这就是我们获取rtf字符串中所有子字符串的方式

public static IEnumerable<string> GetAllSubStrings(string input, int length)
{
    for(var i = 0; i < input.Length - length + 1; i++)
    {
        yield return input.Substring(i, length);
    }
}

【讨论】:

  • 如果你能解释为什么 RichTextBox1.Rtf.ToUpper(); 这可能是一个更合适的答案。不起作用。
  • @Quang:看看更新后的答案。在 split 中添加 \t 有助于获得所需的输出。
  • 如果您的文本在一行中包含单个"r"(或"rtf"),则此方法不起作用
  • 是的,我试过了。如果文本是only r,它会失败,因为RTF 将以{\Rtf1\ 而不是{\rtf1\ 开头。不过,如果您替换 rtf 字符串的开头以确保它以 {\rtf1\ 开头,则此解决方案非常有效!
  • 恐怕如果该行包含格式(例如This \b is \b0 a test (-> This is a test) 也不会大写,因为 rtf不包含要替换的文本
【解决方案2】:

应该是 Mohit 建议的:

private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = richTextBox1.Text.ToUpper();
        }

【讨论】:

    【解决方案3】:

    Rft.ToUpper() 出现错误的原因是因为RTF 代码是

    区分大小写

    举个例子

      string  rft = {\\rtf1\\ansi\\deff0\r\n{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;}\r\nThis is text}
    

    它会在richTextBox1.Rtf = rft; 时返回This is text,因为您可以看到格式为lowercase,所以当您应用Rft.ToLower() 时它没有区别,但是当您应用Rft.ToUpper() 时它不会遵循RFT代码格式因此它给出错误

    文件格式无效

    所以如果你想从richbox 获取text 作为UpperCase 使用

    richTextBox1.Text = richTextBox1.Text.ToUpper();
    

    【讨论】:

      【解决方案4】:

      您不能直接将文本值设置为"Rtf" 属性。 当您在调试时检查Rtf 属性时,该值类似于

      "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17\\par\r\n}\r\n"
      

      点击下面的链接了解更多关于使用 Rtf 的信息。 http://www.codeproject.com/KB/cs/RTFSyntaxColour.aspx

      Reset RTF in RichTextBox?

      您最快的选择是使用 RichTextBox 的纯文本:

      RichTextBox.Text = RichTextBox.Text.ToUpper();
      

      【讨论】:

        【解决方案5】:

        如果您想即时将所有字母键转换为大写字母,我会这样做

        private void RTB1_KeyPress(object sender, KeyPressEventArgs e)
         {
          if (e.KeyChar > 96 && e.KeyChar<123) e.KeyChar = (char)((int)e.KeyChar - 32);
         }
        

        【讨论】:

          猜你喜欢
          • 2015-12-20
          • 1970-01-01
          • 2020-12-01
          • 2013-07-16
          • 1970-01-01
          • 1970-01-01
          • 2022-01-24
          • 2014-02-19
          • 2022-01-01
          相关资源
          最近更新 更多