webForumDet fria alternativet

Problem med SelectionFont

1 svar · 340 visningar · startad av saw

sawMedlem sedan mars 20011 880 inlägg
#1

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

sawMedlem sedan mars 20011 880 inlägg
#2

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

131 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
129 ms — hämta tråd, inlägg och bilagor (db)