Autor Beitrag
Unitet20
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Do 19.07.07 13:41 
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:

ausblenden volle Höhe C#-Quelltext
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; // speichert den alten Zustand des Zeichens
        pos += 1// weiter zum naechsten Zeichen

        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);
        //MessageBox.Show(fs.ToString());
        fs = FontStyle.Regular;
    }
}


Wobei txt eine RichTextBox ist.

Den Aufruf starte ich auf den Buttons so:
ausblenden 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; // lasse button gedrückt aussehen
    this.txt.Focus(); // fokusiere die textbox
}


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 user profile iconChristian S.: Topic aus Algorithmen, Optimierung und Assembler verschoben am Do 19.07.2007 um 15:27
Darkpara
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 61

Win XP

BeitragVerfasst: Do 19.07.07 14:32 
hm also ich hätte das jetzt so gelöst ka ob bessere wege gibt :)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
procedure TForm1.Button1Click(Sender: TObject);
begin
  if Label1.Font.Style <> [fsBold] then
  Label1.Font.Style := Label1.Font.Style +[fsBold]
  else Label1.Font.Style := Label1.Font.Style -[fsBold];
end;


hier wird allerdings das folgende prob auftreten:

wenn Label1.Font.Style z.b. [fsitalic] enthält ist mein if immer falsch,
glaub da gabs was mit
if [fsBold] in Label1.Font.Style then.. aber weis ned mehr wie das genau ging

edit:
ah habs wieder :)
ausblenden Delphi-Quelltext
1:
2:
3:
  if fsBold in Label1.Font.Style then
  Label1.Font.Style := Label1.Font.Style -[fsBold]
  else Label1.Font.Style := Label1.Font.Style +[fsBold];


so sollte es in allen fällen gehen
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 19.07.07 15:28 
@Darkpara: C# kennt keine Sets ;-)

Ich denke, das sollte so gehen:
ausblenden C#-Quelltext
1:
fs = fs & (!FontStyle.Bold);					

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Unitet20 Threadstarter
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Do 19.07.07 16:37 
Danke schonmal für eure Hilfe,
funktioniert allerdings immernoch nicht so richtig.

fs = fs & (~FontStyle.Bold);

Funktioniert nicht so ganz, komischerweise löscht er dann ALLE formatierungen, also wenn der Style:
Bold, Italic ist und ich nur bold rausnehmen will. Macht er daraus wieder die den Style: Regular...

ne Idee wodran das nun wieder liegen könnte? hmm...
Ich bin verwirrt und verzweifelt. :D
Danke
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 19.07.07 20:52 
Bei mir funktioniert das hier:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
            FontStyle fs = FontStyle.Regular;

            fs |= FontStyle.Italic;
            fs |= FontStyle.Bold;
            fs |= FontStyle.Underline;
            fs &= ~FontStyle.Bold;

            MessageBox.Show(fs.ToString());


Im Code ganz oben setzt Du fs am Ende manuell auf Regular, liegt's daran?

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Unitet20 Threadstarter
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Do 19.07.07 21:39 
Danke! Nun hab ich den Fehler auch gefunden.

Das Problem lag an:
ausblenden C#-Quelltext
1:
2:
3:
        bool bold = txt.SelectionFont.Bold;  
        bool italic = txt.SelectionFont.Italic; 
        bool underline = txt.SelectionFont.Underline;


mit:
ausblenden C#-Quelltext
1:
2:
3:
        bool bold = false;  
        bool italic = false;; 
        bool underline = false;;

Funktionierts ;)

Also nochmal meine Lösung für Alle:
ausblenden C#-Quelltext
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:
// Funktion: Sorgt dafür das der ausgewählte Text richtig formatiert wird
private void setFontStyle(string make, string newStyle){
    Font tempFont = txt.SelectionFont;
    FontStyle fs = tempFont.Style;

    bool bold = false
    bool italic = false;
    bool underline = false;

    if (newStyle == "bold") bold = true;
    if (newStyle == "italic") italic = true;
    if (newStyle == "underline") underline = true;

    if(make == "do"){
        if (bold) { fs |= FontStyle.Bold; }
        if (italic) { fs |= FontStyle.Italic; }
        if (underline) { fs |= FontStyle.Underline; }
    }

    if(make == "undo"){
        if (bold) { fs = fs &= ~FontStyle.Bold; }
        if (italic) { fs = fs &= ~FontStyle.Italic; }
        if (underline) { fs &= ~FontStyle.Underline; }
    }

    txt.SelectionFont = new Font(tempFont.FontFamily, tempFont.Size, fs);
}

txt = der Name der RichTextBox

Aufrufen tu ich das ganze so:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
// Klick auf den Button "Fett"
private void b_fett_Click(object sender, EventArgs e) {
    if ((this.b_fett.FlatStyle == FlatStyle.Standard) && (this.txt.SelectionFont.Style != FontStyle.Bold)) {
        this.setFontStyle("do""bold"); // Änder ausgewählten text zu fett
        this.b_fett.FlatStyle = FlatStyle.Flat; // lasse button gedrückt aussehen
        this.txt.Focus(); // focus auf textbox
    }
    else {
        this.setFontStyle("undo""bold");
        this.b_fett.FlatStyle = FlatStyle.Standard; // lasse normal aussehen
        this.txt.Focus(); // focus auf textbox
    }
}



Dank Euch, vorallem dir Christian ;)
Uni
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 19.07.07 21:45 
ausblenden C#-Quelltext
1:
if (bold) { fs = fs &= ~FontStyle.Bold; }					

Das ist jetzt aber doppelt gemoppelt ;-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Unitet20 Threadstarter
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Fr 20.07.07 08:36 
Hoppala, stimmt. :D
Habe ich garnicht gesehen, danke :D
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Fr 20.07.07 12:58 
Dein Code ist ein wenig kompliziert, ich würde das so machen:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
void b_fett_Click(object sender, EventArgs e) {
  if (txt.SelectionFont != null)
    txt.SelectionFont.Style ^= FontStyle.Bold;
  else
    // ...   mehrere Fonts auf einmal ausgewählt
    // denk dir eine schöne Lösung für diesen Fall aus ;) 
}

Ein Bugfix + eine Ersparnis von 30 Zeilen, das muss mir ein Komprimierungsalgorithmus erst einmal nachmachen :zwinker: .
Unitet20 Threadstarter
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Mo 23.07.07 10:35 
Danke für den Tipp... ^^
Ich weiß das der Code nicht der Beste ist ^^
Bin auch noch totaler C# Anfänger,
was ich da so mache ist eigentlich alles nur learning by doing :)