1

Why in C# i can do this:

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

But i can not do that:

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

enter image description here

I am using visual studio 2015

Please help me> Thank!

if i use: RichTextBox1.Text = RichTextBox1.Text.ToUpper();

enter image description here

when i click button1

enter image description here

but i want:

enter image description here

EDIT 2: I try:

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());
   }

and

enter image description here

when i click button1

enter image description here

it do not work with "tôi tên là"

HOW i can do that???? please

Kevin
  • 1,068
  • 5
  • 14
  • 16
Quang
  • 183
  • 2
  • 10
  • 1
    try `RichTextBox1.Text.ToUpper();` – Mohit S Dec 14 '16 at 04:51
  • can you provide code for the table – Usman Dec 14 '16 at 05:57
  • @Quang: You should not delete the content of the question. But remain as it is. So that later if somebody needs it they can refer to the question and answer of this post. The way you tell the community that your issue is resolved is by accepting the solution as answer. Please rollback your edit. – Mohit S Dec 16 '16 at 03:01
  • @Mohit Shrivastava, i understand what you say. But this time i can not do that...i am so sory... – Quang Dec 17 '16 at 14:52

6 Answers6

1

This might do the trick for you if the RTF is a formatted string.

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 would contain data something like this {\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... and when we try to do the operation on this kind of string it will throw error which would be File format is not valid but to get saved from this problem we can use SelectedText or Text of RichTextBox where we only get the Inner Text of the RichTextBox. and then we can replace text with Upper Text.

Edit

After the requirements of changing Unicode Strings to Uppercase. The RTF change the tôi tên là to something like this \cf1\f1\fs20 t\'f4i t\'ean l\'e0 and when we run the code Replace(strText, strText.ToUpper()); for line tôi tên là cannot be found in the RichTextBox1.Rtf so I used a if condition to see if the string exsist in the rtf or not if yes then normal code would work but if the unicode string exsist then changed it to rtf to see what is the rtf version of the unicode string.

Another one is Stephan Bauer rightly suggested that if r or rtf or t,f,rt,tf exsist in the line than it would give error which changing the case. so Added that functionality as well.

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());
            }
        }
}

This is how we get all the Substrings in the String of 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);
    }
}
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • 1
    I would say this can be a more appropriate answer if you could explain why RichTextBox1.Rtf.ToUpper(); doesn't work. – Sethu Bala Dec 14 '16 at 04:59
  • @Quang: Have a look at the updated answer. adding a `\t` to split is helping to get the required output. – Mohit S Dec 14 '16 at 06:13
  • This does not work if your text contains a single `"r"` (or `"rtf"`) in a line – Stephan Bauer Dec 14 '16 at 07:39
  • Yes I tried it. If the text is *only* r, it fails because the RTF will start with `{\Rtf1\ ` instead of `{\rtf1\ `. Nevertheless, if you replace the beginning of the rtf-string to ensure that it starts with `{\rtf1\ `, this solution works really fine! – Stephan Bauer Dec 14 '16 at 07:46
  • I'm afraid it also won't work if the line contains formatting (e.g. `This \b is \b0 a test` (-> This **is** a test) will not be uppercased since the rtf does not contain the text that is to be replaced – Stephan Bauer Dec 14 '16 at 07:54
  • Thank all. But it do not work with "tôi tên là"...please look at the updated answer – Quang Dec 14 '16 at 10:00
  • @Quang: when we write `tôi tên là` in RichTextBox it is converted to RTF like this `\cf1\f1\fs20 t\'f4i t\'ean l\'e0` so it is like difficult to convert to Uppercase. You can have a look at [How to convert a string to RTF in C#?](http://stackoverflow.com/a/4795785/3796048) might help you – Mohit S Dec 15 '16 at 04:38
  • Hey @Quang Felt happy to help you. BTW another way of saying thanks on SO could be upvoting the answer. if this answer has solved your question please consider [accepting it](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Mohit S Dec 16 '16 at 02:53
0

It should be this as Mohit has suggested:

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

You cannot directly set the text value to "Rtf" property. When you check the Rtf property while debugging the value is like

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

Follow below links for more information on working with Rtf . http://www.codeproject.com/KB/cs/RTFSyntaxColour.aspx

Reset RTF in RichTextBox?

your fastest choice here is working with plain text of your RichTextBox:

RichTextBox.Text = RichTextBox.Text.ToUpper();
Community
  • 1
  • 1
AmiNadimi
  • 5,129
  • 3
  • 39
  • 55
0

The reason why you are getting error on Rft.ToUpper() because RTF code is

case-sensitive

as an example

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

it will return This is text when richTextBox1.Rtf = rft; as you can see that the format is in lowercase so when you apply Rft.ToLower() it dont make difference but when you apply Rft.ToUpper() it dosnt follow the RFT code format thus it gives error

File format is not valid

so if you want to get text from richbox as UpperCase use

richTextBox1.Text = richTextBox1.Text.ToUpper();
Usman
  • 4,615
  • 2
  • 17
  • 33
0

if you want to convert all alpha keys to caps on the fly, I do this

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

I use this approach:

    Private Sub rtbTextChanged(sender As Object, e As EventArgs) Handles rtb1.TextChanged, rtb2.TextChanged
        Dim oRTB As RichTextBox = sender
        oRTB.Text = oRTB.Text.ToUpper
    End Sub
Chris Raisin
  • 384
  • 3
  • 7