如何更改RichTextBox中文本的大小写?

| 因此,当在上下文菜单中单击该选项时,我试图使选定数量的文本(在富文本框中)变为大写或小写。 这是我要使用的代码:
private void toUPPERCASEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (rtxtMain.SelectedText != \"\")
            {
                rtxtMain.SelectedText.ToUpper();
            }
        }

private void toLowercaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (rtxtMain.SelectedText != \"\")
            {
                rtxtMain.SelectedText.ToLower();
            }
        }
但是,当我尝试时,文本不会更改...如何更改它?     
已邀请:
        您不能更改现有的字符串实例。 ToUpper()和ToLower()返回一个新的字符串实例。 尝试
rtxtMain.SelectedText = rtxtMain.SelectedText.ToUpper();
    

bab

        字符串在C#中是不可变的。因此,所有内置操作,不仅包括
ToLower
ToUpper
,还包括
Replace
Trim
等,都将返回包含修改后数据的新字符串。他们不会更改您现有的字符串。 正如其他海报所指出的,这就是为什么您的答案是
rtxtMain.SelectedText = rtxtMain.SelectedText.ToUpper();
    
        
rtxtMain.text =ttxtMain.text.Replace(rtxtmain.SelectedText,rtxtmain.SelectedText.ToUpper())
    

要回复问题请先登录注册