Autor Beitrag
Dude566
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Mi 20.08.08 17:39 
Also im folgenden Programm möchte ich das die Klicks auf den Button1 gezählt werden und beim
Draufklicken in einem Editfeld ausgegeben werden.

Wie stell ich das an?
PS: Bin Anfänger!

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:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  zahl1 : integer;
  zahl2 : integer;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  zahl1 := StrToInt (Edit1.text);
  zahl2 := strToInt (Edit2.Text);
  if zahl2 = zahl1 then ShowMessage ('RICHTIG!!!');
  if zahl2 < zahl1 then ShowMessage ('Die gesuchte Zahl ist größer');
  if zahl2 > zahl1 then ShowMessage ('Die gesuchte Zahl ist kleiner');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.PasswordChar := '*';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Randomize;
  zahl1 := Random (100)+1;
  Edit1.Text := IntToStr (zahl1);
end;

end.
platzwart
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1054
Erhaltene Danke: 78

Win 7, Ubuntu 9.10
Delphi 2007 Pro, C++, Qt
BeitragVerfasst: Mi 20.08.08 18:18 
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:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  zahl1 : integer;
  zahl2 : integer;
  counter: Integer;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  zahl1 := StrToInt (Edit1.text);
  zahl2 := strToInt (Edit2.Text);
  if zahl2 = zahl1 then ShowMessage ('RICHTIG!!!');
  if zahl2 < zahl1 then ShowMessage ('Die gesuchte Zahl ist größer');
  if zahl2 > zahl1 then ShowMessage ('Die gesuchte Zahl ist kleiner');
  Inc(counter);  
  Edit3.Text:= IntToStr(counter);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.PasswordChar := '*';
  counter:= 0;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Randomize;
  zahl1 := Random (100)+1;
  Edit1.Text := IntToStr (zahl1);
end;

end.


In Edit3 steht dann der Wert...
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Mi 20.08.08 18:29 
1000 Dank, klappt alles!
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Mi 20.08.08 18:36 
Hi! Ein paar Anmerkungen hätte ich aber noch zu machen.
Randomize; gehört ins FormCreate und sollte nur einmal(!) aufgerufen werden. Zudem sind gobale Variablen unschön. Besser so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
  private
    { Private declarations }
    fCount: Integer; // f = field
  public
    { Public declarations }
    property Count: Integer read fCount write fCount;
  end;
Wobei ich jetzt nicht weiß, ob sich Properties in diesem Fall unbedingt lohnen.

Grüße,
Marc
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Mi 20.08.08 18:38 
Man brauch einen Anfänger nun aber nicht auch gleich mit Propertys überfluten, es reicht wohl, wenn er counter als private deklariert.
Logikmensch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 390

Win XP
Delphi 2007 Prof., XE2, XE5
BeitragVerfasst: Do 21.08.08 05:48 
Gerade wenn er Anfänger ist, sollte man ihm die richtige Praxis doch gleich nahebringen. Den Sinn von Properties wird er ohnehin bald rausfinden. :-)

_________________
Es gibt keine Probleme - nur Lösungen!
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Do 21.08.08 14:00 
Aber die Property ist in dem Fall ÜBERFLÜSSIG!!!!!!
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Do 21.08.08 14:01 
Danke für die Informationen, aber unser Lehrer hats uns so gezeigt.
Wir hatten auch erst 5h Info-Unterricht.
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Do 21.08.08 14:09 
user profile iconBoldar hat folgendes geschrieben:
Aber die Property ist in dem Fall ÜBERFLÜSSIG!!!!!!

Er hat es doch schon gesagt, dass er es nur zu Demonstratiosnzwekcen verwendet hat. Und wenn du schon so darauf bestehst, fünf von deinen sechst Ausrufezeichen sind auch überflüssig.

Und man könnte es auch ganz ohne Variablen machen, in dem man die Tag-Eigenschaft von TButton nutzt:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.FormCreate(Sender: TObject);
begin
  Button1.Tag := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.Tag := Button1.Tag + 1;
  Edit1.Text := IntToStr(Button1.Tag);
end;
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Do 21.08.08 17:42 
Aha, und welche Methode ist jetzt besser, falls es einen Unterschied gibt?
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Do 21.08.08 17:46 
Wenn du mit Tag arbeitest, lässt sich der Counter recht einfach auf mehrere Buttons übertragen. Das ist in deinem Fall nicht unbedingt notwendig. Ich würde eine private Formular-Variable wählen, da es sich bei deinem Programm um ein Spiel handelt. Wenn die Spielstände quer über Komponenten und Formulare verstreut sind, kommt man schnell durcheinander. Private Symbole dagegen sind schön untereinander aufgelistet, die findet man schnell.

Um wirklich Buttonklicks zu zählen wäre Tag wohl besser, da es sich dann ja wirklich auf den Button bezieht. Du möchtest aber eigentlich nicht die Buttonklicks, sondern die Rateversuche zählen.
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Do 21.08.08 20:29 
Danke für die ganzen Antworten jetzt weis ich bescheid. :wink:
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Fr 22.08.08 08:13 
user profile iconYogu hat folgendes geschrieben:
Wenn du mit TagDas ist in deinem Fall nicht unbedingt notwendig. Ich würde eine private Formular-Variable wählen, da es sich bei deinem Programm um ein Spiel handelt. Wenn die Spielstände quer über Komponenten und Formulare verstreut sind, kommt man schnell durcheinander.

Und ich würde das Zählen dem Spiel, also der Spielklasse, überlassen und nicht der Oberfläche.
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Fr 22.08.08 10:39 
user profile iconLuckie hat folgendes geschrieben:
Und ich würde das Zählen dem Spiel, also der Spielklasse, überlassen und nicht der Oberfläche.

Es handelt sich hierbei nicht um ein 3D-Action-Spiel, sondern um "Rate eine Zahl zwischen 1 und 1000". Ich bezweifle, dass user profile iconDude566 bei diesem Spiel eine aufwändige Spielverwaltung erstellt hat. ;)
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Fr 22.08.08 15:16 
user profile iconYogu hat folgendes geschrieben:
user profile iconLuckie hat folgendes geschrieben:
Und ich würde das Zählen dem Spiel, also der Spielklasse, überlassen und nicht der Oberfläche.

Es handelt sich hierbei nicht um ein 3D-Action-Spiel, sondern um "Rate eine Zahl zwischen 1 und 1000". Ich bezweifle, dass user profile iconDude566 bei diesem Spiel eine aufwändige Spielverwaltung erstellt hat. ;)


Wie Yogu schon sagte, es ist nichts besonderes, war nur eine Aufgabe des Info-Lehrers die ich etwas modifiziert habe.


Zuletzt bearbeitet von Dude566 am Fr 22.08.08 15:19, insgesamt 1-mal bearbeitet
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Fr 22.08.08 15:17 
Sorry für Doppelpost.