unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
ActnList, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
S1: TToggleBox;
S2: TToggleBox;
S3: TToggleBox;
S4: TToggleBox;
S5: TToggleBox;
S6: TToggleBox;
S7: TToggleBox;
procedure Button1Click(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure S1Change(Sender: TObject);
procedure S2Change(Sender: TObject);
procedure S3Change(Sender: TObject);
procedure S4Change(Sender: TObject);
procedure S5Change(Sender: TObject);
procedure S6Change(Sender: TObject);
procedure S7Change(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage('Finde die richtige Kombination')
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Form1.caption='2' then
begin
if (S1.caption='OFF') and (S2.caption='OFF') and (S3.caption='ON') and (S4.caption='OFF') and (S5.caption='ON') and (S6.caption='OFF') and (S7.caption='ON') then
begin
ShowMessage('Glückwunsch :D');
Form1.caption:='3'
end else
begin
ShowMessage('Schade :(');
if Form1.caption='1' then
begin
if (S1.caption='OFF') and (S2.caption='ON') and (S3.caption='ON') and (S4.caption='ON') and (S5.caption='OFF') and (S6.caption='OFF') and (S7.caption='ON') then
begin
ShowMessage('Glückwunsch :D');
Form1.caption:='2'
end else
begin
ShowMessage('Schade :(')
end;
end;
end;
end;
end;
procedure TForm1.S1Change(Sender: TObject);
begin
if S1.Caption='ON' then
begin
S1.caption:='OFF'
end else
begin
S1.caption:='ON'
end;
end;
procedure TForm1.S2Change(Sender: TObject);
begin
if S2.Caption='ON' then
begin
S2.caption:='OFF'
end else
begin
S2.caption:='ON'
end;
end;
procedure TForm1.S3Change(Sender: TObject);
begin
if S3.Caption='ON' then
begin
S3.caption:='OFF'
end else
begin
S3.caption:='ON'
end;
end;
procedure TForm1.S4Change(Sender: TObject);
begin
if S4.Caption='ON' then
begin
S4.caption:='OFF'
end else
begin
S4.caption:='ON'
end;
end;
procedure TForm1.S5Change(Sender: TObject);
begin
if S5.Caption='ON' then
begin
S5.caption:='OFF'
end else
begin
S5.caption:='ON'
end;
end;
procedure TForm1.S6Change(Sender: TObject);
begin
if S6.Caption='ON' then
begin
S6.caption:='OFF'
end else
begin
S6.caption:='ON'
end;
end;
procedure TForm1.S7Change(Sender: TObject);
begin
if S7.Caption='ON' then
begin
S7.caption:='OFF'
end else
begin
S7.caption:='ON'
end;
end;
end.
Ich hab mal versucht ein einfaches Kombinationsschloss zu programmieren (bin Neuling)
Allerdings scheint der TButton1 nicht zu klappen.
Aber er öffnet es normal und zeigt auch nicht an was falsch ist es klappt nur nicht
Hallo JpJScream,
erstmals herzlich willkommen im Lazarusforum.
Nun zu deinem Problem, das ich eigentlich nicht verstehe, weil du nicht sagst wo dein Problem ist und was du überhaupt programmieren willst. Dein Programmcode hast du sehr unübersichtlich und sehr schwer lesbar dargestellt.
Bedenke, dass wenn du dich nicht anstrengst dein Programm strukturiert darzustellen, müssen viele die deinen Beitrag lesen wollen (so sie es überhaupt tun) sich unnötig anstrengen, deinen Code zu verstehen.
Um ein lesbares Programm zu schreiben musst du sinnvoll einrücken, was in jedem Pascal Handbuch beschrieben und erklärt wird.
Nun zu deinem Problem (das ich nun trotzdem versuche zu verstehen): in deiner FormCreate-Procedure wird nur die ShowMessage-Methode aufgerufen. Nach einem Click auf den Button1, ist Form1.Caption immer noch mit Form1 belegt. Es wird sofort in den else-Zweig gesprungen. Wie kommst du überhaupt auf die Idee das unveränderte Form1.Caption abzufragen. Bitte denke noch einmal scharf nach.
Gruß, Linkat
PS. Lass dich nicht von ein paar Anfängerproblemen abschrecken. Mit jedem Fehler den du machst und korrigierst, lernst du dazu.
JpJScream hat geschrieben:
Allerdings scheint der TButton1 nicht zu klappen.
Aber er öffnet es normal und zeigt auch nicht an was falsch ist es klappt nur nicht
???
Kannst du das mal auf deutsch oder so übersetzen?
Wie kann ein Button "klappen"?
Wer ist "er" und was ist "es"?
ich habe sieben TToggleBox (S1-S7)
und einen TButton (TButton1)
die caption von Form1 ist am Anfang automatisch '1'
jetzt zu dem programm:
also wenn man einen der ToggleBoxen drückt ändert sich die Caption der ToggleBox von 'ON' zu 'OFF'
----> Ich habe nun versucht den Button1 so zu schreiben dass er wenn eine bestimmte kombination der ToggleBoxen erfüllt ist (OFF-ON-ON-ON-OFF-OFF-ON) er die ShowMessage 'Glückwunsch' anzeigt und die Caption der Form1 in '2' ändert, sodass ich eine neue Kombination abfragen kann.
procedure TForm1.Button1Click(Sender: TObject);
if (S1.caption='OFF') and (S2.caption='ON') and (S3.caption='ON') and (S4.caption='ON') and (S5.caption='OFF') and (S6.caption='OFF') and (S7.caption='ON') then
begin
ShowMessage('Glückwunsch :D');
end else
begin
ShowMessage('Schade :(')
end;
Der Code oben hat geklappt also dachte ich mir ich füge wenn die Kombination gelöst ist noch eine zweite zum lösen ein
Das Resultat war dann dieser Code, der leider nicht mehr klappt :
procedure TForm1.Button1Click(Sender: TObject);
begin
if Form1.caption='2' then
begin
if (S1.caption='OFF') and (S2.caption='OFF') and (S3.caption='ON') and (S4.caption='OFF') and (S5.caption='ON') and (S6.caption='OFF') and (S7.caption='ON') then
begin
ShowMessage('Glückwunsch :D');
Form1.caption:='3'
end else
begin
ShowMessage('Schade :(');
if Form1.caption='1' then
begin
if (S1.caption='OFF') and (S2.caption='ON') and (S3.caption='ON') and (S4.caption='ON') and (S5.caption='OFF') and (S6.caption='OFF') and (S7.caption='ON') then
begin
ShowMessage('Glückwunsch :D');
Form1.caption:='2'
end else
begin
ShowMessage('Schade :(')
end;
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Form1.caption='2'
then
begin // von if caption = 2
if ( (S1.caption='OFF')
and (S2.caption='OFF')
and (S3.caption='ON')
and (S4.caption='OFF')
and (S5.caption='ON')
and (S6.caption='OFF')
and (S7.caption='ON')
)
then
begin
ShowMessage('Glückwunsch :D');
Form1.caption:='3'
end
else
begin
ShowMessage('Schade :(');
if Form1.caption='1'
then
begin
if ( (S1.caption='OFF')
and (S2.caption='ON')
and (S3.caption='ON')
and (S4.caption='ON')
and (S5.caption='OFF')
and (S6.caption='OFF')
and (S7.caption='ON')
)
then
begin
ShowMessage('Glückwunsch :D');
Form1.caption:='2'
end
else
begin
ShowMessage('Schade :(')
end;
end;
end;
end; // von if caption = 2
end;
Man sieht dann relativ schnell, dass wenn Form1.caption = '2' ist,
in 'then' reingesprungen, und ansonsten nichts getan wird.
Dein Code funktioniert also perfekt: Beim Klick auf den Button macht er genau nichts, weil form1.caption nicht '2' ist.
procedure TForm1.Button1Click(Sender: TObject);
begin
if Form1.caption='2' then
begin // hier ist der begin der if-Schleife
if (S1.caption='OFF') and (S2.caption='OFF') and (S3.caption='ON') and (S4.caption='OFF') and (S5.caption='ON') and (S6.caption='OFF') and (S7.caption='ON') then
begin
ShowMessage('Glückwunsch :D');
Form1.caption:='3'
end else
begin
ShowMessage('Schade :(')
end;
end; // und hier ist das Ende der if-Schleife
if Form1.caption='1' then
begin
if (S1.caption='OFF') and (S2.caption='ON') and (S3.caption='ON') and (S4.caption='ON') and (S5.caption='OFF') and (S6.caption='OFF') and (S7.caption='ON') then
begin
ShowMessage('Glückwunsch :D');
Form1.caption:='2'
end else
begin
ShowMessage('Schade :(')
end; // vorher war das ende der Schleife hier weshalb er ALLES nur ausgeführt hat wenn Form1.caption='2' war
end;
Danke an alle die mir geholfen haben und besonders an Eb der mir den Fehler erklärt hat
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
ActnList, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Label1: TLabel;
S1: TToggleBox;
S2: TToggleBox;
S3: TToggleBox;
S4: TToggleBox;
S5: TToggleBox;
S6: TToggleBox;
S7: TToggleBox;
procedure Button1Click(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure S1Change(Sender: TObject);
procedure S2Change(Sender: TObject);
procedure S3Change(Sender: TObject);
procedure S4Change(Sender: TObject);
procedure S5Change(Sender: TObject);
procedure S6Change(Sender: TObject);
procedure S7Change(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage('Finde die richtige Kombination')
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Form1.caption='2' then
begin
if (S1.caption='OFF') and (S2.caption='OFF') and (S3.caption='ON') and (S4.caption='OFF') and (S5.caption='ON') and (S6.caption='OFF') and (S7.caption='ON') then
begin
ShowMessage('Glückwunsch :D');
Form1.caption:='3'
end else
begin
ShowMessage('Schade :(')
end;
end;
if Form1.caption='1' then
begin
if (S1.caption='OFF') and (S2.caption='ON') and (S3.caption='ON') and (S4.caption='ON') and (S5.caption='OFF') and (S6.caption='OFF') and (S7.caption='ON') then
begin
ShowMessage('Glückwunsch :D');
Form1.caption:='2'
end else
begin
ShowMessage('Schade :(')
end;
end;
if Form1.caption='3' then
begin
if (S1.caption='OFF') and (S2.caption='ON') and (S3.caption='OFF') and (S4.caption='ON') and (S5.caption='OFF') and (S6.caption='ON') and (S7.caption='OFF') then
begin
ShowMessage('Glückwunsch :D');
Form1.caption:='4'
end else
begin
ShowMessage('Schade :(')
end;
end;
if Form1.caption='4' then
begin
if (S1.caption='OFF') and (S2.caption='OFF') and (S3.caption='OFF') and (S4.caption='ON') and (S5.caption='OFF') and (S6.caption='ON') and (S7.caption='ON') then
begin
ShowMessage('Glückwunsch :D');
Form1.caption:='5'
end else
begin
ShowMessage('Schade :(')
end;
end;
if Form1.caption='5' then
begin
if (S1.caption='OFF') and (S2.caption='OFF') and (S3.caption='OFF') and (S4.caption='ON') and (S5.caption='OFF') and (S6.caption='OFF') and (S7.caption='OFF') then
begin
ShowMessage('Du hast alles gelöst Glückwunsch :D');
Form1.caption:='1'
end else
begin
ShowMessage('Schade :(')
end;
end;
end;
procedure TForm1.S1Change(Sender: TObject);
begin
if S1.Caption='ON' then
begin
S1.caption:='OFF'
end else
begin
S1.caption:='ON'
end;
end;
procedure TForm1.S2Change(Sender: TObject);
begin
if S2.Caption='ON' then
begin
S2.caption:='OFF'
end else
begin
S2.caption:='ON'
end;
end;
procedure TForm1.S3Change(Sender: TObject);
begin
if S3.Caption='ON' then
begin
S3.caption:='OFF'
end else
begin
S3.caption:='ON'
end;
end;
procedure TForm1.S4Change(Sender: TObject);
begin
if S4.Caption='ON' then
begin
S4.caption:='OFF'
end else
begin
S4.caption:='ON'
end;
end;
procedure TForm1.S5Change(Sender: TObject);
begin
if S5.Caption='ON' then
begin
S5.caption:='OFF'
end else
begin
S5.caption:='ON'
end;
end;
procedure TForm1.S6Change(Sender: TObject);
begin
if S6.Caption='ON' then
begin
S6.caption:='OFF'
end else
begin
S6.caption:='ON'
end;
end;
procedure TForm1.S7Change(Sender: TObject);
begin
if S7.Caption='ON' then
begin
S7.caption:='OFF'
end else
begin
S7.caption:='ON'
end;
end;
end.