none
problem implementing ctrl-I c# RRS feed

  • Question

  • Here's the code - implemented for my app's richtextbox class.

    When I hit ctrl-I, it blanks the selected text and then the cursor changes to insert mode (i.e. it's in italics).

    I tried leaving out the baseOnKeyUp(e); and it works (incorrectly IMHO) the same way.

    I can't find anyplace in Windows doc that indicates any pre-set meaning for ctrl-I. Maybe I just missed it. If there is one then how would I override it? Omitting the base.OnKeyUp was my thought.

                protected override void OnKeyUp(KeyEventArgs e)
                {
                    base.OnKeyUp(e);
    
                    if (e.KeyCode == Keys.I && e.Modifiers == Keys.Control)
                    {
                        this.SelectionFont =
                            new Font(this.Font.Name, this.Font.Size, FontStyle.Italic);
                    }
                    


    Saturday, May 23, 2020 2:17 PM

Answers

  • We've been going about this all wrong, Ken. I had thought earlier about overriding the ProcessCmdKey method, but I didn't have an example I could use to test, so I just went down the same path as you did with the KeyUp/KeyDown stuff. This is so simple, I should have found time over the weekend to give it a try, but luckily had some time today. Sorry about not trying this sooner.

    Here it is:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (ActiveControl is RichTextBox)
        {
            if (keyData == (Keys.Control | Keys.I))
            {
                ((RichTextBox)ActiveControl).SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Italic);
                return true;
            }
        }
    
        return base.ProcessCmdKey(ref msg, keyData);
    }



    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by ken quirici Tuesday, June 2, 2020 1:05 PM
    Tuesday, June 2, 2020 12:11 AM

All replies

  • Hi ken quirici,
    Based on your description, do you wan to blank the selected text and then the cursor changes to insert mode when you press the "control" and "i"?
    If so, you can use richTextBox1_KeyUp event and implemente ctrl-I in this event.
    Here is a simple code example you can refer to.

    private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
    {
            if (e.KeyCode == Keys.I && e.Modifiers == Keys.Control)
            {
                 richTextBox1.SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Italic);
             }
    }

    The result:


    Do you want to achieve it like result? If not, please explain in detail.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 25, 2020 5:44 AM
  • Try this:

    if (e1.KeyCode == Keys.I && e1.Modifiers == Keys.Control)
    {
        string save = richTextBox1.SelectedText;
        richTextBox1.SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Italic);
        richTextBox1.SelectedText = save;
        richTextBox1.SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Regular);
    }
    

    It leaves extra white space at the end of the selection (anything I tried did), and I think it's a tab character. See if you can figure out how to get rid of it.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Tuesday, May 26, 2020 2:51 PM
  • Hi Bonne,

    Well your hunch that it's putting in a tab character might be useful in my researching the problem. I had not thought of that to explain the 'spaces'.

    I didn't quite follow your example (what was the string of k's?) - pardon my occasional density but still

    Thanks.

    Regards,

    Ken

    Tuesday, May 26, 2020 6:51 PM
  • String of k's?!?!?  I don't know what you mean. You don't see code here?

    if (e.KeyCode == Keys.I && e.Modifiers == Keys.Control)
    {
        string save = richTextBox1.SelectedText;
        richTextBox1.SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Italic);
        richTextBox1.SelectedText = save;
        richTextBox1.SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Regular);
    }


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Tuesday, May 26, 2020 7:31 PM
  • Sorry that was in Daniel Zhang's post not yours

    Regards

    Ken

    Friday, May 29, 2020 8:53 PM
  • Good question, Ken.  Could you show your code again?

    Putting a SelectionLength = 0; doesn't work right at all for me (in fact, it duplicates what I had selected in the first place), so I'm probably doing something different than you are.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Friday, May 29, 2020 11:51 PM
  • Hi Bonnie,

    The code is below. I put the SelectionLength = 0 as the first line in the if-block and executed it. Then I deleted the line and re-executed the code and the code worked fine - that is, when I selected a word and typed ctrl-I, the selected word was italicized. This is the code and it's working fine. I really have no idea why the SelectionLength = 0 worked because it's nullified by reselecting a piece of text and typing ctrl-I. I hope that's clear. 

                    if (e.KeyCode == Keys.I && e.Modifiers == Keys.Control)
                    {
                        this.SelectionFont =
                            new Font(this.Font.Name, 
                                this.Font.Size,
                                FontStyle.Italic);
                    }
                    
    

    Saturday, May 30, 2020 1:03 AM
  • I finally got it figured out, Ken. We need to handle both the KeyUp and the KeyDown events, saving some values in the KeyDown and putting the rest of the processing in the KeyUp. Here's the code I used for testing:

    string saveSelectedText;
    int saveSelectionStart, saveSelectionLength;
    
    richTextBox1.KeyDown += delegate (object sender1, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.I && e.Modifiers == Keys.Control)
        {
            // Save the relevant settings
            this.saveSelectedText = richTextBox1.SelectedText;
            this.saveSelectionStart = richTextBox1.SelectionStart;
            this.saveSelectionLength = richTextBox1.SelectionLength;
        }
    };
    richTextBox1.KeyUp += delegate (object sender1, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.I && e.Modifiers == Keys.Control)
        {
            // first, we must get rid of the tab character that ends up replacing the SelectedText
            richTextBox1.Text = richTextBox1.Text.Remove(this.saveSelectionStart, 1);
    
            // Next, put the starting point in the correct spot, and set the saved SelectedText
            richTextBox1.SelectionStart = saveSelectionStart;
            richTextBox1.SelectedText = saveSelectedText;
    
            // Setting the SelectedText above changes the SelectionStart, so we need to reset it again
            richTextBox1.SelectionStart = saveSelectionStart;
    
            // Now, reset the length and set FontStyle to italicize it
            richTextBox1.SelectionLength = saveSelectionLength;
            richTextBox1.SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Italic);
    
            // And lastly, unselect, move the starting point to end of the Selection 
            // and put the FontStyle back to regular
            richTextBox1.SelectionLength = 0;
            richTextBox1.SelectionStart = this.saveSelectionStart + this.saveSelectionLength;
            richTextBox1.SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Regular);
        }
    };
    

    This was fun (albeit a bit frustrating at first). I think I'll write a blog post about this (if you don't mind, Ken).

    Can I ask what purpose you're using this functionality for, or was it just an experiment on your part just for the heck of it?

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, May 31, 2020 8:33 PM
  • Here's your code modified to my case.

    As far as I can see it does absolutely nothing. You put the SelectedText in save, then you issue the SelectionFont = , and then

    you undo it - you replace the SelectedText with the old save. 

                    if (e.KeyCode == Keys.I && e.Modifiers == Keys.Alt)
                    {
                         String save = myForm.myTextBox.SelectedText;
                         myForm.myTextBox.SelectionFont = new Font(this.Font.Name, 
                             this.Font.Size, 
                             FontStyle.Italic);
                         myForm.myTextBox.SelectedText = save;
                         myForm.myTextBox.SelectionFont = new Font(this.Font.Name, 
                             this.Font.Size, 
                             FontStyle.Regular);
                    }
                    
                    myForm.currFile.currNote.textBox.Rtf = myForm.noteTextBox.Rtf;
                    myForm.currFile.currNote.RtfSize =
                           myForm.currFile.currNote.textBox.TextLength;
                }
     

    I tried it just issuing the SelectionFont and it also did nothing. But Alt-I doesn't seem to be a Windows cmd.

                    if (e.KeyCode == Keys.I && e.Modifiers == Keys.Alt)
                    {
                         String save = myForm.myTextBox.SelectedText;
                         myForm.myTextBox.SelectionFont = 
    new Font(this.Font.Name, 
                             this.Font.Size, 
                             FontStyle.Italic);
                         myForm.myTextBox.SelectedText = save;
                         myForm.myTextBox.SelectionFont = 
    new Font(this.Font.Name, 
                             this.Font.Size, 
                             FontStyle.Regular);
                    }              


    Sunday, May 31, 2020 11:47 PM
  • Hi Ken,

    I was just about to reply to you, but let me address what you just wrote first:

    The reason it doesn't work with Alt+I is because it behaves differently than Ctrl+I and all the hoops I jumped through to get it to work with Ctrl+I were necessary for only Ctrl+I.

    Now, here's what I was going to say:

    Unfortunately, Ctrl+I is a standard in many Windows programs for italicizing a selection. Programs like Outlook and Word and I'm sure there are many more. It seems that Notepad is an exception. It's always a good idea to follow a standard, because so many people are used to the behavior being what they expect it to be.

    It would be nice if Windows Forms had that functionality "out of the box", but apparently it doesn't.

    Using Alt+I instead is problematic too, because Alt is for use with menus. Use of any of those programs I mentioned (Notepad included) and just hit the Alt key. You'll notice the menus have their hot-keys underlined and continuing typing after hitting Alt will enter a menu if it has a hot-key for whatever you type next (I in this case).

    So, bottom-line, you should use Ctrl+I and my code ... in my opinion anyway.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, June 1, 2020 12:04 AM
  • Bonnie,

    I'm curious about something. I'm not sure that's a legal 'question' in this sort of forum but here it is;

    Wouldn't NOT executing base.OnKeyUp(e) and NOT executing base.OnKeyDown(e) suppress this default behavior of control-I (or Alt-I) that we've seen in this thread, that interferes w/ my attempt at a 'custom' functionality for these keys?

    Ken

    Monday, June 1, 2020 9:34 PM
  • Hello,

     Has the issue been resolved? I have some food for thought;

    When you implement any new behavior for text editors, it is

    always a good idea to research what "short-cut keys" have

    already been assigned. However, not all text editor treat Control-I

    the same. Example, F9 in Word Perect actually Itaticizes the selected

    text. The following link may help on your project key configuration;

     https://www.computerhope.com/issues/ch000306.htm

     

     Hope this helps :)

    Monday, June 1, 2020 10:49 PM
  • We've been going about this all wrong, Ken. I had thought earlier about overriding the ProcessCmdKey method, but I didn't have an example I could use to test, so I just went down the same path as you did with the KeyUp/KeyDown stuff. This is so simple, I should have found time over the weekend to give it a try, but luckily had some time today. Sorry about not trying this sooner.

    Here it is:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (ActiveControl is RichTextBox)
        {
            if (keyData == (Keys.Control | Keys.I))
            {
                ((RichTextBox)ActiveControl).SelectionFont = new Font(this.Font.Name, this.Font.Size, FontStyle.Italic);
                return true;
            }
        }
    
        return base.ProcessCmdKey(ref msg, keyData);
    }



    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by ken quirici Tuesday, June 2, 2020 1:05 PM
    Tuesday, June 2, 2020 12:11 AM
  • Hi Bonnie,

    Works like a charm! Gave you a point - wish I could give you more!

    Regards,

    Ken

    Fantastic! What A relief!

    Tuesday, June 2, 2020 1:13 PM
  • Glad I could help, Ken! I just wish I had come up with it sooner!  =0)

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Tuesday, June 2, 2020 2:28 PM
  • Hi Bonnie,

    No it was me distracting you with side issues! :-(

    Regards

    Ken

    Tuesday, June 2, 2020 4:24 PM
  • That sounds like an excellent idea. I've saved the link you provided as a tab in this collection of tabs that has this discussion on the MS Forums.

    Thanks!

    Regards,

    Ken

    Wednesday, June 3, 2020 11:26 AM