Vorweg einmal: Hallo an Alle, das ist mein erster Post hier.
Ich hoffe ich hab mir das richtige Board für mein Posting ausgesucht.
Und nun kommen wir ohne umwege zu meinem Problem, also ich arbeite im Moment an einem Textformatierungs-Algorithmus.
Momentan sieht er so aus:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31:
| private void setFontStyle(string newStyle){ int start = txt.SelectionStart; int length = txt.SelectionLength; Font tempFont; FontStyle fs = FontStyle.Regular;
int pos = start; int endPos = pos + length; while (pos < endPos) { txt.SelectionStart = pos; txt.SelectionLength = 1; tempFont = txt.SelectionFont; pos += 1; bool bold = txt.SelectionFont.Bold; bool italic = txt.SelectionFont.Italic; bool underline = txt.SelectionFont.Underline;
if (newStyle == "bold") bold = true; if (newStyle == "italic") italic = true; if (newStyle == "underline") underline = true;
if (bold) fs |= FontStyle.Bold; if (italic) fs |= FontStyle.Italic; if (underline) fs |= FontStyle.Underline;
txt.SelectionFont = new Font(tempFont.FontFamily, tempFont.Size, fs); fs = FontStyle.Regular; } } |
Wobei txt eine RichTextBox ist.
Den Aufruf starte ich auf den Buttons so:
C#-Quelltext
1: 2: 3: 4: 5:
| private void b_fett_Click(object sender, EventArgs e) { this.setFontStyle("bold"); this.b_fett.FlatStyle = FlatStyle.Flat; this.txt.Focus(); } |
Das funktioniert soweit auch schon alles prima, abgesehen von der Tatsache:
Wenn ein Button (z.B fett) betätigt wurde, wird fett aktiviert.
Wenn man nun ein zweitesmal auf den Button drückt, soll fett natürlich wieder deaktiviert werden.
Also im Prinzip ist meine Frage:
Wie kann ich einen FontStyle "abziehen"
Nach dem Motto:
if (bold) fs - FontStyle.Bold;
Jemand eine Idee für mich?
Danke schonmal im vorraus!
Uni
Moderiert von Christian S.: Topic aus Algorithmen, Optimierung und Assembler verschoben am Do 19.07.2007 um 15:27