Autor Beitrag
THF
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 110

Vista
Delphi 2005 professional
BeitragVerfasst: Mo 14.02.05 19:10 
hallo,

wie kann ich zweidimensionales Array in ein eindimensionales
array umwandeln ?
Ich möchte die 64 Feldern meines Spielfeldes "StringGrid"
anders benennen.

z.B. StringGrid1.Cells[1,1] := Feld[5]
StringGrid1.Cells[1,2] := Feld[6] u.s.w.
Irgendwie funktioniert es nicht !

Gruß THF
:o
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Mo 14.02.05 19:16 
Sollte kein Problem sein:

ausblenden Delphi-Quelltext
1:
2:
for i := 0 to 63 do
  StringGrid1.Cells[i div Spaltenanzahl, i mod Spaltenanzahl] := Feld[i];


Oder nicht?

AXMD
THF Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 110

Vista
Delphi 2005 professional
BeitragVerfasst: Mo 14.02.05 19:21 
Hallo AXMD,

genauso hab ich es gemacht aber wenn ich dann in das Feld[i]
etwas reinschreiben will erscheint nichts.
z.B. Feld[22]:=' Hallo';
Gruß

THF
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Mo 14.02.05 19:24 
:wall: Du willst es ja umgekehrt haben... ich sollte besser lesen...

ausblenden Delphi-Quelltext
1:
2:
for i := 0 to 63 do  
  Feld[i] := StringGrid1.Cells[i div Spaltenanzahl, i mod Spaltenanzahl];


So, das müsste gehen ;)

AXMD
THF Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 110

Vista
Delphi 2005 professional
BeitragVerfasst: Mo 14.02.05 21:11 
Hallo AXMD,
es funktioniert nicht.
Er schreibt nichts rein in die Zellen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.FormCreate(Sender: TObject);
var Feld:array[1..64] of string;
var   i:integer;
begin
for i:= 1 to 64 do
Feld[i]:=StringGrid1.Cells[i div StringGrid1.ColCount ,i mod StringGrid1.RowCount];
Feld[22]:= 'Hallo';

end;


Könnt Ihr mir helfen , woran das liegt ?
Gruß
THF

Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt.
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Mo 14.02.05 21:33 
Ich hab nicht ohne Grund von 0 bis 63 gezählt. StrungGrid-Zellen fangen nämlich - wie alle anderen Indizes in Delphi auch - bei 0 an.
Außerdem würd ich das StringGrid danach noch refreshen oder repainten lassen

AXMD
THF Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 110

Vista
Delphi 2005 professional
BeitragVerfasst: Mo 14.02.05 21:39 
Hallo AXMD,

funktioniert leider immer noch nicht.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.FormCreate(Sender: TObject);
var Feld:array[0..63] of string;
var   i:integer;
begin
for i:= 0 to 63 do
Feld[i]:=StringGrid1.Cells[i div StringGrid1.ColCount ,i mod StringGrid1.RowCount];

StringGrid1.Refresh  ;
StringGrid1.Repaint;
Feld[22]:= 'Hallo';

end;

Gruß

THF

Moderiert von user profile iconAXMD: Code- durch Delphi-Tags ersetzt.
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Mo 14.02.05 21:44 
Was funktioniert an dem hier nicht?

AXMD hat folgendes geschrieben:
Sollte kein Problem sein:

ausblenden Delphi-Quelltext
1:
2:
for i := 0 to 63 do
  StringGrid1.Cells[i div Spaltenanzahl, i mod Spaltenanzahl] := Feld[i];


Oder nicht?

AXMD


AXMD
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mo 14.02.05 21:52 
Hallo!

@THF: Ich habe ein wenig den Eindruck, Du möchtest, wenn Du feld[22] änderst, dass sich das entsprechende Feld im StringGrid sich direkt mitändert.

Habe ich Recht?

Wenn ja: Das geht nicht so, wie Du das machst. Eine Zuweisung feld[j] := StringGrid1.Cells[k,m] kopiert den Inhalt der Zelle in feld[j], erzeugt in feld[j] aber keine Referenz auf die Zelle!

Mir fällt im Moment keine Möglichkeit ein, dass so zu machen. Ich würde mir dann einfach eine Prozedur "setCell(index : Integer; value : String)" basteln, welche mir die zum Index passende Zelle mit dem Wert füllt.

MfG
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
THF Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 110

Vista
Delphi 2005 professional
BeitragVerfasst: Mo 14.02.05 22:02 
Hallo,

ich wollte in die Zellen Strings einfügen.
z.B. in Feld [22]:= '999';
ohne jedesmal
StringGrid1.Cell[x,y]:='999' zu schreiben.

Gruß
THF
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Mo 14.02.05 22:11 
Das ist genau das, was Christian gemeint hat. Er hat die Vorgehensweise beschrieben, ich erläutere sie daher nicht nocheinmal.

AXMD