Autor Beitrag
lapadula
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 180
Erhaltene Danke: 10



BeitragVerfasst: Mi 03.08.16 21:39 
Hallo, ich habe ein Verständnisproblem beim folgenden Problem.

In einer RichTextBox werden die Zeilen, die je nachdem was bei der If-Abfrage rauskommt grün, rot oder schwarz gefärbt.

Eine Möglichkeit dafür wäre zB.
ausblenden C#-Quelltext
1:
// deleted					

Moderiert von user profile iconTh69: s. unten

Gibt es dafür eine elegantere Lösung, bzw eine kompaktere?


Moderiert von user profile iconChristian S.: Topic aus C# - Die Sprache verschoben am Mi 03.08.2016 um 22:17
C#
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 561
Erhaltene Danke: 65

Windows 10, Kubuntu, Android
Visual Studio 2017, C#, C++/CLI, C++/CX, C++, F#, R, Python
BeitragVerfasst: Do 04.08.16 07:51 
Bin ich der Einzige der keinen Beispielcode sieht oder hat ihn der TE einfach vergessen?

_________________
Der längste Typ-Name im .NET-Framework ist: ListViewVirtualItemsSelectionRangeChangedEventHandler
Palladin007
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1282
Erhaltene Danke: 182

Windows 11 x64 Pro
C# (Visual Studio Preview)
BeitragVerfasst: Do 04.08.16 08:22 
Ich würde eher darauf tippen, dass jemand was kaputt gemacht hat, hab gestern den Code nämlich noch gesehen :D
Vielleicht Christian? :roll:
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 04.08.16 08:35 
:gruebel: Ich hab eigentlich nur verschoben und zwei Tags eingefügt. :nixweiss:

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Palladin007
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1282
Erhaltene Danke: 182

Windows 11 x64 Pro
C# (Visual Studio Preview)
BeitragVerfasst: Do 04.08.16 08:39 
Hat sich da ein Käfer versteckt? :P


Der Code war auf jeden Fall da, er hat die Selection von der RichTextBox geändert um dann für die aktuelle Auswahl die Farbe zu ändern.
Wann genau weiß ich aber auch nicht mehr, war glaube eher später.


Naja, dann muss wohl lapadula nach helfen ^^
lapadula Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 180
Erhaltene Danke: 10



BeitragVerfasst: Fr 05.08.16 23:34 
ups da hab ich wohl beim editieren was falsch gemacht :lol:

hier nochmal der Beispielcode

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 highlightLineContaining(RichTextBox rtb, int line, string search, Color color)
        {
            int c0 = rtb.GetFirstCharIndexFromLine(line);
            int c1 = rtb.GetFirstCharIndexFromLine(line + 1);
            if (c1 < 0) c1 = rtb.Text.Length;
            rtb.SelectionStart = c0;
            rtb.SelectionLength = c1 - c0;
            if (rtb.SelectedText.Contains(search))
                rtb.SelectionColor = color;
            rtb.SelectionLength = 0;
        }

        private void DisplayScript()
        {
            this.Script.Clear();
            for(int i = 0; i < this.Scripter.Script.Querys.Length; i++)
            {
                string sLine = i.ToString("00000") + " ["
                    + this.Scripter.Script.Querys[i].iAffectedRows.ToString("000") + "]   |"
                    + this.Scripter.Script.Querys[i].sQuery + " --> "
                    + this.Scripter.Script.Querys[i].sError + "\n";
                this.Script.Text += sLine;
                
                if(this.Scripter.Script.Querys[i].bExecuted)
                    this.highlightLineContaining(this.Script, i, sLine, Color.Green);
                else if (this.Scripter.Script.Querys[i].sError != string.Empty)
                    this.highlightLineContaining(this.Script, i, sLine, Color.Red);                
                else
                    this.highlightLineContaining(this.Script, i, sLine, Color.Black);                
            }
        }


Hab das nun alles in die DisplayScript()-Methode gepackt und per if-Bedingung abgefragt, so klappt das und nun wird auch der Text gefärbt auch wenn es einen Zeilenumbruch gab, vorher hat er das nicht gemacht.

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:
32:
33:
34:
35:
36:
private void DisplayScript()
        {
            this.rtbScript.Clear();

            for(int i = 0; i < this.Scripter.Script.Querys.Length; i++)
            {
                sLine = this.Scripter.Script.Querys[i].sQuery + " " + Scripter.Script.Querys[i].sError + "\n";

                if (this.Scripter.Script.Querys[i].bExecuted)
                {

                    int start = rtbScript.TextLength;
                    rtbScript.AppendText(sLine);
                    rtbScript.Select(start, sLine.Length);
                    
                    rtbScript.SelectionColor = Color.Green;
                    
                }
                else if (this.Scripter.Script.Querys[i].sError != string.Empty)
                {
                    int start = rtbScript.TextLength;
                    rtbScript.AppendText(sLine);
                    rtbScript.Select(start, sLine.Length);

                    rtbScript.SelectionColor = Color.Red;

                }
                else
                {
                    int start = rtbScript.TextLength;
                    rtbScript.AppendText(sLine);
                    rtbScript.Select(start, sLine.Length);

                    rtbScript.SelectionColor = Color.Black;
 
                }
Frühlingsrolle
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Fr 05.08.16 23:53 
- Nachträglich durch die Entwickler-Ecke gelöscht -
lapadula Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 180
Erhaltene Danke: 10



BeitragVerfasst: Di 16.08.16 16:48 
Danke, so wäre es noch kompakter.