Autor Beitrag
CrazyLuke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 247

Windows XP Pro
Turbo Delphi Explorer, D2005 PE
BeitragVerfasst: So 12.11.06 14:31 
Hi!

Wie kann ich Code optimieren, indem ich mehrere Befehle mit Hilfe einer Variable auf mehrere RichEdits anwende?

Hier der Code, der nicht funktioniert:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
 for [i] := 1 to 6 do
  begin
    RichEdit[i].WordWrap := True;
    RichEdit[i].SelStart := Length(RichEdit[i].Text)-1;
    RichEdit[i].SelLength := 2;
    RichEdit[i].ClearSelection;
  end;


Vielen Dank im Voraus!

_________________
"Was kommt vor 'Schmetterlinge im Bauch'? Raupen im A*sch!" (Bastian Pastewka, "Pastewka", Sat1)
Leuchtturm
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1087

Win Vista, Knoppix, Ubuntu
Delphi 7 Pe, Turbo Delphi, C#(VS 2005 Express), (X)HTML + CSS, bald Assembler
BeitragVerfasst: So 12.11.06 14:34 
Was willst du genau :?: Ich habe das jetzt nicht kapiert :gruebel:

_________________
Ich bin dafür verantwortlich was ich sage - nicht dafür was du verstehst.
CrazyLuke Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 247

Windows XP Pro
Turbo Delphi Explorer, D2005 PE
BeitragVerfasst: So 12.11.06 14:37 
Ich möchte diesen ursprünglichen Code mit Hilfe einer Variable verkürzen:
ausblenden Delphi-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:
begin
    RichEdit1.WordWrap := True;
    RichEdit1.SelStart := Length(RichEdit1.Text)-1;
    RichEdit1.SelLength := 2;
    RichEdit1.ClearSelection;
    RichEdit2.WordWrap := True;
    RichEdit2.SelStart := Length(RichEdit2.Text)-1;
    RichEdit2.SelLength := 2;
    RichEdit2.ClearSelection;
    RichEdit3.WordWrap := True;
    RichEdit3.SelStart := Length(RichEdit3.Text)-1;
    RichEdit3.SelLength := 2;
    RichEdit3.ClearSelection;
    RichEdit4.WordWrap := True;
    RichEdit4.SelStart := Length(RichEdit4.Text)-1;
    RichEdit4.SelLength := 2;
    RichEdit4.ClearSelection;
    RichEdit5.WordWrap := True;
    RichEdit5.SelStart := Length(RichEdit5.Text)-1;
    RichEdit5.SelLength := 2;
    RichEdit5.ClearSelection;
    RichEdit6.WordWrap := True;
    RichEdit6.SelStart := Length(RichEdit6.Text)-1;
    RichEdit6.SelLength := 2;
    RichEdit6.ClearSelection;
  end;

_________________
"Was kommt vor 'Schmetterlinge im Bauch'? Raupen im A*sch!" (Bastian Pastewka, "Pastewka", Sat1)
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: So 12.11.06 14:40 
Suche in der Entwickler-Ecke FINDCOMPONENT ;)
grüße marc
CrazyLuke Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 247

Windows XP Pro
Turbo Delphi Explorer, D2005 PE
BeitragVerfasst: So 12.11.06 14:49 
Etwas komplizierter als gedacht, aber es funktioniert - danke :)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
 for i := 1 to 6 do
  begin
    TRichEdit(FindComponent('RichEdit' + IntToStr(i))).WordWrap := True;
    TRichEdit(FindComponent('RichEdit' + IntToStr(i))).SelStart := Length(TRichEdit(FindComponent('RichEdit' + IntToStr(i))).Text)-1;
    TRichEdit(FindComponent('RichEdit' + IntToStr(i))).SelLength := 2;
    TRichEdit(FindComponent('RichEdit' + IntToStr(i))).ClearSelection;
  end;

_________________
"Was kommt vor 'Schmetterlinge im Bauch'? Raupen im A*sch!" (Bastian Pastewka, "Pastewka", Sat1)
Reinhard Kern
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 591
Erhaltene Danke: 14



BeitragVerfasst: So 12.11.06 15:26 
user profile iconCrazyLuke hat folgendes geschrieben:
Etwas komplizierter als gedacht, aber es funktioniert - danke :)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
 for i := 1 to 6 do
  begin
    TRichEdit(FindComponent('RichEdit' + IntToStr(i))).WordWrap := True;
    TRichEdit(FindComponent('RichEdit' + IntToStr(i))).SelStart := Length(TRichEdit(FindComponent('RichEdit' + IntToStr(i))).Text)-1;
    TRichEdit(FindComponent('RichEdit' + IntToStr(i))).SelLength := 2;
    TRichEdit(FindComponent('RichEdit' + IntToStr(i))).ClearSelection;
  end;


Hallo,

es geht auch weniger kompliziert, dafür mit etwas Arbeitsaufwand: du deklarierst dir einfach ein Array mit deinen Controls. Das musst du zwar beim Start füllen, dann kannst du aber wie gewünscht damit arbeiten. Ein weiterer Vorteil: die RichEdits können sinnvolle Namen erhalten, weil es egal ist, wie sie heissen.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
type
MyRichEdits = array [1..6] of TRichEdit;
  {...}
MyRichEdits[1] := RichEdit1;
  {...}
for i := 1 to 6 do
  with MyRichEdits[i] as TRichEdit do {...};


Gruss Reinhard
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 12.11.06 15:41 
Also was die beliebigen Namen angeht: Da reicht auch ein konstantes Array von Strings das man oben im Fenster vorher definiert...
Ansonsten ist es denke ich Geschmackssache, ich persönlich finde FindComponent + String-Array schöner und einfacher als ein extra Array der Komponenten.