Share via


How to get uppercase font while typing in rich text box, c# Visual Studio?

Question

Tuesday, November 1, 2016 11:06 AM

Hi

I am working on a project. I am new to visual studio and c#. My requirement is to force user to type in upper case at certain point with in rich text box. There will be mixture of lower case and upper case. I am stuck with upper case issue. Need help in this regard.

I want uppercase font as I type in rich text box. I searched entire internet but no help.

All replies (22)

Tuesday, November 1, 2016 1:14 PM ✅Answered

Hi Diya,

 private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (comboBox1.SelectedIndex ==1)
            {
                e.KeyChar = Char.ToUpper(e.KeyChar);
            }
            
        }

Chang if condition it will fix your problem


Tuesday, November 1, 2016 1:25 PM ✅Answered

Hi Diya,

Go to Properties window of RichText box and Click that "Thunder" symbol -> scroll down to Key press event -> double click it.

Thanks,
Sabah Shariq

[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


Tuesday, November 1, 2016 11:16 AM

Hi Diya,

Please clarify what do you mean by 

>>My requirement is to force user to type in upper case at certain point with in rich text box.

Thanks,
Sabah Shariq

[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


Tuesday, November 1, 2016 11:26 AM

Hi Sabah,

Thanks for you response. Actually, I am knew to the forum. I searched for but could not able to find, so voted. I will sure mark my previous question as answered. My apology.

In this current question, I am currently working on a project in which, a user is needed to type. It is quite similar to ms word. So a user can type with various mixture of fonts.

At certain point, user need to type but font should appear in uppercase.I achieved fonts in uppercase but all fonts are changing to upper case. I want uppercase in certain lines inside richtext box.

For example. My user typed first paragraph in certain font and in lowercase. Then He need to type heading. Heading must appear in another type font but in uppercase. Then next paragraph in lower case. Then next heading in uppercase.

Hope you understand my question. Again sory, for my english language.


Tuesday, November 1, 2016 11:40 AM

Hi Diya,

According to your question, you want to show all letter in upper case while user typing in <g class="gr_ gr_105 gr-alert gr_spell gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="105" id="105">richtextbox</g> text.  Following code will work.

 private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.KeyChar = Char.ToUpper(e.KeyChar);
        }

Here, change richTextBox1 to your RichTextBox control name. This should work.  Here we are taking Pressed Key and convert it in uppercase.

Thanks


Tuesday, November 1, 2016 11:58 AM

Thanks for your response. I am knew to visual studio and c#. Could not able to figure out where to  paste the mentioned code. Following is the code, please check.

 public Form1()        {            InitializeComponent();        }        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)        {            if (comboBox1.SelectedIndex == 0) {                richTextBox1.SelectionFont = new Font("courier", 12, FontStyle.Bold);                                richTextBox1.Focus();            }            if (comboBox1.SelectedIndex == 1)            {                richTextBox1.SelectionFont = new Font("courier", 12, FontStyle.Italic);                richTextBox1.Focus();            }        }        private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)        {            e.KeyChar = Char.ToUpper(e.KeyChar);        }

Tuesday, November 1, 2016 11:58 AM

 public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0) {
                richTextBox1.SelectionFont = new Font("courier", 12, FontStyle.Bold);
                
                richTextBox1.Focus();

            }
            if (comboBox1.SelectedIndex == 1)
            {
                richTextBox1.SelectionFont = new Font("courier", 12, FontStyle.Italic);
                richTextBox1.Focus();

            }
        }
        private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.KeyChar = Char.ToUpper(e.KeyChar);
        }

Tuesday, November 1, 2016 12:00 PM

Hi Diya,

I got your point. You have to maintain a flag when user wants to write heading. In this case, you can give of checkbox to select when user wants to write heading.

And then you have to check flag is selected or not. Depend on that you will make it uppercase.

  private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (UpperCase.Checked)
            {
                e.KeyChar = Char.ToUpper(e.KeyChar);
            }
            
        }

Let me know if that helps you or you need more 


Tuesday, November 1, 2016 12:02 PM

Hi Diya,

One option you could do is add an option to select the fonts when typing like below. So when user type heading they can select the font they want to use. Hope this helps you.

Thanks,
Sabah Shariq

[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


Tuesday, November 1, 2016 12:22 PM

Hi

Thanks for your response. No its not working for some reason. I tried with your provided suggestion. But the problem I cant tell user to tick mark it. This is some thing which should be automatic. I tried with below code also. Please have a look.

      

private bool turnOnCaps;        
public Form1()       
 {            
InitializeComponent();           
 turnOnCaps = false;        }       
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)        {           
 if (comboBox1.SelectedIndex == 0) 
{                
richTextBox1.SelectionFont = new Font("courier", 12, FontStyle.Bold);                
turnOnCaps = true;                richTextBox1.Focus();                            }            
if (comboBox1.SelectedIndex == 1)            {                
richTextBox1.SelectionFont = new Font("courier", 12, FontStyle.Regular);                richTextBox1.Focus();            }        }  

      private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)        {            
if (turnOnCaps==true)            {

                e.KeyChar = Char.ToUpper(e.KeyChar);                MessageBox.Show("working");            }        }

Tuesday, November 1, 2016 12:24 PM

richTextBox1_KeyPress(object sender, KeyPressEventArgs e)

This is not functioning otherwise it would have shown the message.(Sorry for not having proper words). 

I may be wrong but I guess, this should be called some how. This is not called and so rest code is no meaning.


Tuesday, November 1, 2016 12:28 PM

 private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (UpperCase.Checked)
            {
                e.KeyChar = Char.ToUpper(e.KeyChar);
            }
            
        }

Tuesday, November 1, 2016 12:41 PM

As you suggested earlier, I did. The exact code you mentioned above. There is check box which is tickmarked. But still the letters are not appearing in caps.


Tuesday, November 1, 2016 12:54 PM

Hi Diya,

Look into below code. When you write Header -> Check the check box. After that uncheck the check box and write the paragraph. Hope this helps you.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FontSelect
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox1.Checked == true)
            {
                richTextBox1.SelectionFont = new Font("Tahoma", 17, FontStyle.Bold);
                richTextBox1.SelectionColor = System.Drawing.Color.Red;
            }
            else
            {
                richTextBox1.SelectionFont = new Font("Tahoma", 10, FontStyle.Bold);
                richTextBox1.SelectionColor = System.Drawing.Color.Black;
            }
        }
    }
}

Thanks,
Sabah Shariq

[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


Tuesday, November 1, 2016 1:09 PM

Hi Sabah,

Thanks for the response. Let me put my question this way, in your mentioned example, when 'font for heading' is tickmarked or checked, then header 1 should appear  all letter in capital. I mean, your output is "Header 1". But I want, "HEADER 1".


Tuesday, November 1, 2016 1:16 PM

Hi Diya,

See the updated code below. Add RichTextbox Key press event handler.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FontSelect
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox1.Checked == true)
            {
                richTextBox1.SelectionFont = new Font("Tahoma", 17, FontStyle.Bold);
                richTextBox1.SelectionColor = System.Drawing.Color.Red;                
            }
            else
            {
                richTextBox1.SelectionFont = new Font("Tahoma", 10, FontStyle.Bold);
                richTextBox1.SelectionColor = System.Drawing.Color.Black;
            }
        }

        private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(checkBox1.Checked == true)
            {
                e.KeyChar = Char.ToUpper(e.KeyChar);
            }
        }
    }
}

Thanks,
Sabah Shariq

[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


Tuesday, November 1, 2016 1:19 PM

Rokon, Thanks for response.

Yes I change but still lower case.


Tuesday, November 1, 2016 1:21 PM

I am not sure, but guess, that method,

private void richTextBox1_keyPress(...) should be called some where.


Tuesday, November 1, 2016 1:23 PM

Still it is printing in lower case. I guess, the problem is '

richTextBox1_KeyPress

is not called anywhere.

I have placed messagebox inside if condition but it is not showing message.'


Tuesday, November 1, 2016 1:23 PM

Send me your full code. And let me know which comboBox1.SelectedIndex you want uppercase.


Tuesday, November 1, 2016 1:27 PM

 public partial class Form1 : Form    { 
               public Form1()        {  
          InitializeComponent();   
                 }        
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)        {
            if (comboBox1.SelectedIndex == 0) 
{               
 richTextBox1.SelectionFont = new Font("courier", 12, FontStyle.Bold);                
turnOnCaps = true;
       richTextBox1.Focus();  
                          }           
 if (comboBox1.SelectedIndex == 1)            {               
 richTextBox1.SelectionFont = new Font("courier", 12, FontStyle.Regular);
                richTextBox1.Focus();   
         }        }       
 private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)        {            
if (comboBox1.SelectedIndex==0)            {                e.KeyChar = Char.ToUpper(e.KeyChar); 
               MessageBox.Show("working");  
          }        }    }

I want comboBox1.SelectedIndex==1 - all in uppercase


Tuesday, November 1, 2016 1:31 PM

Yipee It worked. Thanks Tons Sabah.