Autor Beitrag
bladel
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Do 21.12.06 13:07 
Also, ich hab von TShape ein Array erstellt
ausblenden Delphi-Quelltext
1:
2:
3:
4:
var
  Form1: TForm1;
  r,i,c,f:integer;
  Sh: Array of TShape;

und möchte, je nach dem welches Shape der Sender ist eine Variable festlegen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
begin
Sh[f]:= TShape.Create(self);
Sh[f].Parent := self;
if Sender = Shape1 then f:=1;
if Sender = Shape2 then f:=2;
if Sender = Shape3 then f:=3;
if Sender = Shape4 then f:=4;
if Sender = Shape5 then f:=5;
if Sender = Shape6 then f:=6;
if Sender = Shape7 then f:=7;
if Sender = Shape8 then f:=8;
if Sender = Shape9 then f:=9;
if (Sh[f].Color<>clred)or(Sh[f].Color<>clblue)and(c=1)then

allerdings meckert Delphi bei "Sh[f]:= TShape.Create(self);" rum. Hab ich da was falsch gemacht?
Miri
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 657


Delphi 3 Prof., Delphi 2005 PE
BeitragVerfasst: Do 21.12.06 13:18 
user profile iconbladel hat folgendes geschrieben:
allerdings meckert Delphi bei "Sh[f]:= TShape.Create(self);" rum.


geht das etwas präziser? was meckert er denn?!

_________________
Anonymes Eckenkind
bladel Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Do 21.12.06 13:27 
"EAccessViolation", hab mal eine wenig rumprobiert. Wenn man
Sh: Array[1..9] of TShape;
einstellt tritt der Fehler nicht mehr auf, allerdings klappt das ganze dann nur für Shape1.
Miri
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 657


Delphi 3 Prof., Delphi 2005 PE
BeitragVerfasst: Do 21.12.06 13:29 
hmpf... das hilft nicht viel weiter... zugriffsverletzung kann alles und nichts heißen...
zeig mal ein bisschen mehr code, vielleicht kommen wir dann weiter...

_________________
Anonymes Eckenkind
bladel Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Do 21.12.06 13:30 
ausblenden volle Höhe 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:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
unit Chatmain;

interface

uses
  Classes, QControls, QStdCtrls, QExtCtrls, QButtons, QForms, Sockets,SysUtils,
  QGrids, Graphics, Windows, Dialogs;


type
  TForm1 = class(TForm)
    memRecv: TMemo;
    Panel1: TPanel;
    memSend: TMemo;
    Panel2: TPanel;
    btnSend: TButton;
    Panel3: TPanel;
    Label1: TLabel;
    edtRemoteHost: TEdit;
    TcpClient1: TTcpClient;
    TcpServer1: TTcpServer;
    Shape1: TShape;
    Shape2: TShape;
    Shape3: TShape;
    Shape4: TShape;
    Shape5: TShape;
    Shape6: TShape;
    Shape7: TShape;
    Shape8: TShape;
    Shape9: TShape;
    Button1: TButton;
    Label2: TLabel;
    Shape10: TShape;
    procedure btnSendClick(Sender: TObject);
    procedure TcpServer1Accept(sender: TObject;
      ClientSocket: TCustomIpClient);
    procedure ShapeMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  // you must create your own thread to synch
  // writing to a gui component
  TClientDataThread = class(TThread)
  private
  public
    ListBuffer :TStringList;
    TargetList :TStrings;
    procedure synchAddDataToControl;
    constructor Create(CreateSuspended: Boolean);
    procedure Execute; override;
    procedure Terminate;
  end;

var
  Form1: TForm1;
  r,i,c,f:integer;
  Sh: Array[1..9] of TShape;
  //DataThread: TClientDataThread;

implementation

{$R *.xfm}

//------------- TClientDataThread impl -----------------------------------------
constructor TClientDataThread.Create(CreateSuspended: Boolean);
begin
  inherited Create(CreateSuspended);
  FreeOnTerminate := true;
  ListBuffer := TStringList.Create;
end;

procedure TClientDataThread.Terminate;
begin
  ListBuffer.Free;
  inherited;
end;

procedure TClientDataThread.Execute;
begin
  Synchronize(synchAddDataToControl);
end;
procedure TClientDataThread.synchAddDataToControl;
begin
 TargetList.AddStrings(ListBuffer);
end;
//------------- end TClientDataThread impl -------------------------------------


procedure TForm1.btnSendClick(Sender: TObject);
begin
  try
    if TcpClient1.Connect then
      for I := 0 to memSend.Lines.Count - 1 do
      TcpClient1.Sendln(memSend.Lines[I]);
  finally
    TcpClient1.Disconnect;
  end;
end;

procedure TForm1.TcpServer1Accept(sender: TObject;
  ClientSocket: TCustomIpClient);
var
  s: string;
  DataThread: TClientDataThread;
begin
  DataThread:= TClientDataThread.Create(true);
  DataThread.TargetList := memRecv.lines;
    // Load the Threads ListBuffer
  s:=ClientSocket.Receiveln;
  while s <> '' do
  begin
    DataThread.ListBuffer.Add(s);
    i:=StrToInt(s);
    s := ClientSocket.Receiveln;
  end;
  DataThread.Resume;
  case i of
   1:begin
     Shape10.Color:=clred;
     r:=2;
     end;
   2:begin
     Shape10.Color:=clblue;
     r:=1;
     end;
   11: Shape1.Color:=clblue;
   12: Shape2.Color:=clblue;
   13: Shape3.Color:=clblue;
   14: Shape4.Color:=clblue;
   15: Shape5.Color:=clblue;
   16: Shape6.Color:=clblue;
   17: Shape7.Color:=clblue;
   18: Shape8.Color:=clblue;
   19: Shape9.Color:=clblue;
   21: Shape1.Color:=clred;
   22: Shape2.Color:=clred;
   23: Shape3.Color:=clred;
   24: Shape4.Color:=clred;
   25: Shape5.Color:=clred;
   26: Shape6.Color:=clred;
   27: Shape7.Color:=clred;
   28: Shape8.Color:=clred;
   29: Shape9.Color:=clred;
    else
    end;
    c:=0;
end;

procedure TForm1.ShapeMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
f:=1;
Sh[f]:= TShape.Create(self);
Sh[f].Parent := self;
if Sender = Shape1 then f:=1;
if Sender = Shape2 then f:=2;
if Sender = Shape3 then f:=3;
if Sender = Shape4 then f:=4;
if Sender = Shape5 then f:=5;
if Sender = Shape6 then f:=6;
if Sender = Shape7 then f:=7;
if Sender = Shape8 then f:=8;
if Sender = Shape9 then f:=9;
if (Sh[f].Color=clred)or(Sh[f].Color=clblue)and(c=1)then
else
begin
if r = 2 then
 begin
 Sh[f].Color:=clblue;
 memSend.Lines.Add(inttostr(10+f));
 end
else
 begin
 Sh[f].Color:=clred;
 memSend.Lines.Add(inttostr(20+f));
 end;
  try
    if TcpClient1.Connect then
      for I := 0 to memSend.Lines.Count - 1 do
      TcpClient1.Sendln(memSend.Lines[I]);
  finally
    TcpClient1.Disconnect;
   end;
c:=1;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TcpClient1.RemoteHost := edtRemoteHost.Text;
  Shape10.Visible:=true;
  R:=1+random(2);
  case R of
  1: begin
     Shape10.Color:=clblue;
     memSend.Lines.Add('1');
     end;
  2: begin
     Shape10.Color:=clred;
     memSend.Lines.Add('2');
     end;
  end ;
  try
    if TcpClient1.Connect then
      for I := 0 to memSend.Lines.Count - 1 do
      TcpClient1.Sendln(memSend.Lines[I]);
  finally
    TcpClient1.Disconnect;
  end;
  Button1.Visible:=false;
  memSend.Lines.Clear;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  TcpServer1.LocalPort := '5000';
  TcpServer1.Active := True;
  TcpClient1.RemotePort := '5000';
  c:=0;
end;

end.