2

I have 2 comboboxes for the font and the fontsize. When I click them it changes the font size or the font in my richtextbox. Now I want it to work like in word. If the line you just moved to is in a different font or size. It should detect that and change the comboxes to match the font and size of the current line. Somoeone else asked this same question and got a result which didn't work for me. It was as follows

    private void richTextBox1_SelectionChanged(object sender, EventArgs e)
    {
        MessageBox.Show("we got here"); // this is my added part to let me know if the code is even getting executed. It is not.
        richTextBox1.SelectionStart = 1;
        richTextBox1.SelectionLength = 1;
        comboBox1.Text = richTextBox1.SelectionFont.ToString();
        comboBox2.Text = null;
        comboBox2.Text = richTextBox1.SelectionFont.Size.ToString();

    }

I held out hope that it was my answer but I could not see how SelectionFont would make any difference when nothing was selected. Also the richTextBox1_SelectionChanged event seems to not be being called when I move through the document with the up/down arrows. The problem is not with the comboboxes, the problem is that as I arrow through my document I need to be able to know what font and size it is at the caret position so it can fire an event to change the combo boxes to match.

Larryrl
  • 121
  • 1
  • 7

3 Answers3

1

The code that you are using will always make the selection from character at index 1 and are of the length 1. instead for that you need to use which will give you the the following code without specifying the selection(so it will take the selection from the ritchTextBox).

string fontName = richTextBox1.SelectionFont.Name;
float fontsize = richTextBox1.SelectionFont.Size;
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

You should save the values for the new comboBox position temporarily in variables, otherwise if you do it directly

comboBox1.SelectedIndex = comboBox1.FindStringExact(richTextBox1.SelectionFont.Name);

the comboBox1_SelectedIndexChanged event will be immediately called and could affect the results.

So just try:

private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
    int comboBox1Index = comboBox1.FindStringExact(richTextBox1.SelectionFont.Name);
    int comboBox2Index = comboBox2.FindStringExact(richTextBox1.SelectionFont.Size.ToString());

    comboBox1.SelectedIndex = comboBox1Index;
    comboBox2.SelectedIndex = comboBox2Index;
}
MarkusEgle
  • 2,795
  • 4
  • 41
  • 61
  • The problem is that the selection changed event is never called. No matter where in my document I am it still shows the last font I used. – Larryrl Aug 17 '16 at 14:03
  • Then it is something related to your RichTextBox and the event handling. In my testing example it works fine. http://s000.tinyupload.com/index.php?file_id=87205811287792650699 – MarkusEgle Aug 17 '16 at 15:10
  • Looked at your example and copied everything over to my events and it still didn't work however in my solution comboBox1.SelectedIndex = 0; comboBox2.SelectedIndex = 0; throws an exception that the SelectIndex cannot be 0 – Larryrl Aug 17 '16 at 16:47
  • I know it works because it works in your form I just have to try and figure out what in my form is conflicting that the rtb selection changed event is never reached. – Larryrl Aug 18 '16 at 05:27
  • Check in the designer file if it the event is correctly added for the rtb. `this.richTextBox1.SelectionChanged += new System.EventHandler(this.richTextBox1_SelectionChanged);` in VS2015 the amount of references above the function is shown and should be 1. Otherwise right click on the `richTextBox1_SelectionChanged` function name and select "Find all references" – MarkusEgle Aug 18 '16 at 12:52
  • Thanks. Seriously I looked and it didn't even have it designer so I added myself. Works perfectly. My word processor is now complete. – Larryrl Aug 18 '16 at 15:56
0

I adapted Sujith's solution and half of Markus's solution and came up with the following which works just fine for me:

Private Sub Description_SelectionChanged(sender As Object, e As EventArgs) Handles Description.SelectionChanged

    Dim fontName As String = Description.SelectionFont.Name
    Dim fontSize As Single = Description.SelectionFont.Size

    tbSelectFont.Text = fontName
    tbSelectSize.Text = fontSize

End Sub
  • In my comment above, (Description = RichTextBox). To test the code, I wrote a short phrase in the RTB of my program, copied and pasted it several times one under the other, changed the font and size of several lines, then moved the cursor to each of the modified lines. My Font Picker ComboBox and FontSize Combo both scroll to the appropriate font and size for each line without selecting any text. I also checked this by changing the font and size of a single character on a line and it also works as expected. – John Michael Wilkinson Oct 31 '20 at 15:07