---
title: "Problem med SelectionFont"
type: "forum-thread"
url: "https://www.webforum.nu/amne/dotnet/180111-problem-med-selectionfont"
topic: ".NET"
topic_url: "https://www.webforum.nu/amne/dotnet"
author: "saw"
published: "2009-09-05T16:10:04.000Z"
updated: "2009-09-05T17:41:33.000Z"
replies: 1
views: 349
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/dotnet/180111-problem-med-selectionfont"
---

# Problem med SelectionFont

## #1 — saw, 2009-09-05T16:10Z

Jag har nu länge och väl försökt att lösa mitt problem, men jag känner att
jag är tvungen att störa er.
**Problem.**
Jag har en toolstrip med en del formateringsknappar, fet kursiv mm.
Som det nu är, så när jag trycker på någon av knapparna, så får jag fram
önskad effekt, men jag kan inte "stänga" av denna effekt. (Kryptiskt?).

```
 private void makeBold()
        {
                if (rTBDoc.SelectionFont.Bold == true)
                {
                    System.Drawing.Font currentFont = rTBDoc.SelectionFont;
                    System.Drawing.FontStyle newFontStyle;
                    newFontStyle = rTBDoc.SelectionFont.Style ^ FontStyle.Regular;
                    rTBDoc.SelectionFont = new Font(currentFont.FontFamily, currentFont.Size, newFontStyle);
                    this.bBold.Checked = false;
                }
                else
                {
                    System.Drawing.Font currentFont = rTBDoc.SelectionFont;
                    System.Drawing.FontStyle newFontStyle;
                    newFontStyle = rTBDoc.SelectionFont.Style ^ FontStyle.Bold;
                    rTBDoc.SelectionFont = new Font(currentFont.FontFamily, currentFont.Size, newFontStyle);
                    this.bBold.Checked = true;
                }
        }
        private void makeItalic()
        {
            if (this.rTBDoc.SelectionFont.Italic == true)
            {
                System.Drawing.Font currentFont = rTBDoc.SelectionFont;
                System.Drawing.FontStyle newFontStyle;
                newFontStyle = rTBDoc.SelectionFont.Style ^ FontStyle.Regular;
                rTBDoc.SelectionFont = new Font(currentFont.FontFamily, currentFont.Size, newFontStyle);
                this.bItalic.Checked = false;
            }
            else
            {
                System.Drawing.Font currentFont = rTBDoc.SelectionFont;
                System.Drawing.FontStyle newFontStyle;
                newFontStyle = rTBDoc.SelectionFont.Style ^ FontStyle.Italic;
                rTBDoc.SelectionFont = new Font(currentFont.FontFamily, currentFont.Size, newFontStyle);
                this.bItalic.Checked = true;
            }

        }
```

rTBDoc är min richTextBox.
Jag har även försökt med att ändra ifsatsen, så att den kolla om knappen är
nere, men det hjälper inte. Knapparna styrs så här:

```
this.bBold.Checked = ((rTBDoc.SelectionFont.Style & FontStyle.Bold) == FontStyle.Bold);
this.bItalic.Checked = ((rTBDoc.SelectionFont.Style & FontStyle.Italic) == FontStyle.Italic);
this.bUnderline.Checked = ((rTBDoc.SelectionFont.Style & FontStyle.Underline) == FontStyle.Underline);
this.bStrikeThrough.Checked = ((rTBDoc.SelectionFont.Style & FontStyle.Strikeout) == FontStyle.Strikeout);
```

Jag vore väldigt tacksam för lite hjälp  :birp

Permalänk: https://www.webforum.nu/p/180111

## #2 — saw, 2009-09-05T17:41Z

Jag svarar mig själv då jag kom på lösningen:

```
private void makeFormat(string format)
        {
            try
            {
                if (!(rTBDoc.SelectionFont == null))
                {
                    System.Drawing.Font currentFont = rTBDoc.SelectionFont;
                    System.Drawing.FontStyle newFontStyle;
                    newFontStyle = rTBDoc.SelectionFont.Style ^
                        FontStyle.Regular;
                    switch (format)
                    {
                        case "Bold":
                            newFontStyle = rTBDoc.SelectionFont.Style ^
                        FontStyle.Bold;
                            break;
                        case "Italic":
                            newFontStyle = rTBDoc.SelectionFont.Style ^
                        FontStyle.Italic;
                            break;
                        case "Underline":
                            newFontStyle = rTBDoc.SelectionFont.Style ^
                         FontStyle.Underline;
                            break;
                        case "Strikethroug":
                            newFontStyle = rTBDoc.SelectionFont.Style ^
                         FontStyle.Strikeout;
                            break;
                    }
                    rTBDoc.SelectionFont = new Font(currentFont.FontFamily,
                       currentFont.Size, newFontStyle);
                }

            }
            catch (Exception fel)
            {
                MessageBox.Show("Error " + fel.Message.ToString(), "MusicTranspose 2.0 - makeFormat " + format);
            }
```

Jag lade också till CheckOnClick i tollStripen så blev det riktigt bra.  :L

Permalänk: https://www.webforum.nu/p/2200968

---

Tråden på webben: https://www.webforum.nu/amne/dotnet/180111-problem-med-selectionfont
