Autor Beitrag
Arbengie
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 66



BeitragVerfasst: Di 19.01.10 17:30 
Hi, neues Problem:

Wenn ich den Button1 an klicke, soll sich dass Label1 5 Pixel nach links bewegen.

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

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var x:real; x1:real;

procedure TForm1.Button1Click(Sender: TObject);
begin
  x:=(Label1.Left);
  x1:=x+5;
  Label1.Left:=(x1);
end;

end.


Fehlermeldung ist:

[Error] Unit1.pas(33): Incompatible types: 'Integer' and 'Real'

Der Fehler liegt irgendwo an den Types, da hab ich meine Schwierigkeiten, kann mir vllt auch einer eine kleine Übersicht machen was, was ist?
Niko S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 566
Erhaltene Danke: 10

Win 7, Ubuntu
Lazarus, Turbo Delphi, Delphu 7 PE
BeitragVerfasst: Di 19.01.10 17:43 
Mach mal
ausblenden Delphi-Quelltext
1:
Label1.Left := trunc(x1);					
max1235
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37

win 98 (aber so gut wie nie dran), win xp, win vista, xandros
delphi(delphi3+7), c#(MV), c++(codeblocks;noch ganz am Anfang vom Lernen)
BeitragVerfasst: Di 19.01.10 17:51 
genau. also es ist so:

du hast die evariablen als real deklariert. der abstand zum rand muss aber integer sein. unterschied: real kann eine kommazahl sein. integer ist eine ganzzahl.
Arbengie Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 66



BeitragVerfasst: Di 19.01.10 17:52 
danke hat geklappt :)
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19315
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 19.01.10 17:52 
Vor allem:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.Button1Click(Sender: TObject);
var x:real; x1:real;
begin
  x := Label1.Left;
  x1 := x + 5;
  Label1.Left := Trunc(x1);
end;
Denn globale Variablen sollte man nur im Notfall benutzen...
Und die überflüssigen Klammern können manchmal Probleme machen, auch wenn sie meistens nur überflüssig sind.

Zudem kannst du Integer als Datentyp benutzen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.Button1Click(Sender: TObject);
var
  x, x1: Integer;
begin
  x := Label1.Left;
  x1 := x + 5;
  Label1.Left := x1;
end;


Und außerdem: Wozu überhaupt Variablen?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Left := Label1.Left + 5;
end;
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Di 19.01.10 17:52 
Hi :)

Die Deklaration als Real macht nur dann Sinn, wenn auch Kommazahlen erwünsht sind. Sonst nimmt man von vornherein Integer, und muss nicht erst eine Kommazahl in eine Ganzzahl verwandeln.

Und dann würde ich vielleicht noch nicht mit globalen Veriablen arbeiten. Viel einfacher ist das hier: Label1.Left := Label1.Left + 5;

mfG,

E: Ich war schon auf Absenden als 2x3x der orangene Kasten kam :cry:

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)