Discussion:
display array values in a label
(too old to reply)
Sandeepan Kashyap
2014-09-11 11:02:48 UTC
Permalink
Hello,

I am new to Delphi and trying to learn it through internet.
Could you please refer some good links for basic Delphi to learn it?

Trying - While hitting button1 and it should display all the arrays values in defined LABEL in a form of 1,2,3... etc. every digit should come beneath to each other in new line.
1
2
3

Issue - It's only displaying the last value, like if you enter 3, it's just displaying only 3. Please suggest what wrong I am doingg? Below Is my code.

procedure TForm1.Button1Click(Sender: TObject);
var
x,y,i,Leng : integer;
txt: string;
MyMatrix: packed array of char;
begin
Form1.Color := clGreen;
x := StrToInt(Edit1.Text);
y := StrToInt(Edit2.Text);

Leng := Length(Edit1.Text);
SetLength(MyMatrix, leng);
for i := 1 to 3 do begin
begin
MyMatrix[i] := Edit1.text[i];
end;
end;
end;
for i:= 1 to leng do begin
txt := txt + MyMatrix[i];
end;
Label3.caption := txt;
Hans-Peter Diettrich
2014-09-11 13:44:47 UTC
Permalink
Sandeepan Kashyap schrieb:

This code
Post by Sandeepan Kashyap
Leng := Length(Edit1.Text);
SetLength(MyMatrix, leng);
for i := 1 to 3 do begin
begin
MyMatrix[i] := Edit1.text[i];
end;
end;
end;
for i:= 1 to leng do begin
txt := txt + MyMatrix[i];
end;
Label3.caption := txt;
is equivalent to

Label3.Caption := Edit1.Text;

I don't understand what you want to do, please try to explain better.

DoDi
Sandeepan Kashyap
2014-09-12 06:24:13 UTC
Permalink
Thanks DoDi,

I am actually an experienced Mumps/Cache developer and have to learn Delphi for a new project. I am actually trying to print a series of number in ascending order, e.g. 1,2,3,4,5,6,7,8,9 but in every number should print in new followed line. like
1
2
3
.

I tried the below code. But the below code printing '0' always inside Label3, don't know why? what's is wrong with the code. Please enlighten me.

type
TIntegerArray = Array[1..9] of Integer;
var
x,y : integer;
txt: string;
i: integer;
arrayOfIntegers : TIntegerArray;
begin
Form1.Color := clGreen;
x := StrToInt(Edit1.Text);
y := StrToInt(Edit2.Text);

i := 1 ;
begin
while (i = 9) do
arrayOfIntegers[i] := i;
Inc(i);
end;
Label3.Caption := IntToStr(arrayOfIntegers[i]);

I appreciate your reply.

Sandeepan.
Post by Hans-Peter Diettrich
This code
Post by Sandeepan Kashyap
Leng := Length(Edit1.Text);
SetLength(MyMatrix, leng);
for i := 1 to 3 do begin
begin
MyMatrix[i] := Edit1.text[i];
end;
end;
end;
for i:= 1 to leng do begin
txt := txt + MyMatrix[i];
end;
Label3.caption := txt;
is equivalent to
Label3.Caption := Edit1.Text;
I don't understand what you want to do, please try to explain better.
DoDi
Hans-Peter Diettrich
2014-09-12 16:01:59 UTC
Permalink
Post by Sandeepan Kashyap
Thanks DoDi,
I am actually an experienced Mumps/Cache developer and have to learn
Delphi for a new project. I am actually trying to print a series of
number in ascending order, e.g. 1,2,3,4,5,6,7,8,9 but in every number
should print in new followed line. like
1
2
3
..
Let's look into this later, it depends on what you mean by "printing".
When you want to show multiple lines on a form, use a TListBox or TMemo,
not an TLabel.
Post by Sandeepan Kashyap
I tried the below code. But the below code printing '0' always inside Label3, don't know why? what's is wrong with the code. Please enlighten me.
type
TIntegerArray = Array[1..9] of Integer;
var
x,y : integer;
txt: string;
i: integer;
arrayOfIntegers : TIntegerArray;
begin
Form1.Color := clGreen;
x := StrToInt(Edit1.Text);
y := StrToInt(Edit2.Text);
i := 1 ;
begin
while (i = 9) do
arrayOfIntegers[i] := i;
Inc(i);
end;
Can you answer these questions:
What's the value of i here?
What's the value of arrayOfIntegers[i]?
Post by Sandeepan Kashyap
Label3.Caption := IntToStr(arrayOfIntegers[i]);
Please try to learn how to use the Delphi debugging features. When you
put an breakpoint somewhere in this procedure, e.g. on "i := 1;", you
can step through the following code and inspect all variables by moving
the mouse pointer over the name of a variable. It even will show you the
content of arrayOfIntegers[i], depending on the current value of i.

Then you'll find that the array is not initialized, because the condition in
while (i = 9) do ...
is never True.

Okay, this may be a typo, should read (i <= 9) instead?
Please fix this and try again.


Finally I assume that you expect too much from IntToStr(), in case you
want to display *all* array elements at once. That doesn't work, for two
reasons:
- IntToStr only converts an single value
- arrayOfInteger[i] is a single value, not the entire array

You need another loop over the array, adding the values to an result
string or to a ListBox control.

DoDi
Sandeepan Kashyap
2014-09-15 09:34:56 UTC
Permalink
Post by Hans-Peter Diettrich
Post by Sandeepan Kashyap
Thanks DoDi,
I am actually an experienced Mumps/Cache developer and have to learn
Delphi for a new project. I am actually trying to print a series of
number in ascending order, e.g. 1,2,3,4,5,6,7,8,9 but in every number
should print in new followed line. like
1
2
3
..
Let's look into this later, it depends on what you mean by "printing".
When you want to show multiple lines on a form, use a TListBox or TMemo,
not an TLabel.
Post by Sandeepan Kashyap
I tried the below code. But the below code printing '0' always inside Label3, don't know why? what's is wrong with the code. Please enlighten me.
type
TIntegerArray = Array[1..9] of Integer;
var
x,y : integer;
txt: string;
i: integer;
arrayOfIntegers : TIntegerArray;
begin
Form1.Color := clGreen;
x := StrToInt(Edit1.Text);
y := StrToInt(Edit2.Text);
i := 1 ;
begin
while (i = 9) do
arrayOfIntegers[i] := i;
Inc(i);
end;
What's the value of i here?
What's the value of arrayOfIntegers[i]?
Post by Sandeepan Kashyap
Label3.Caption := IntToStr(arrayOfIntegers[i]);
Please try to learn how to use the Delphi debugging features. When you
put an breakpoint somewhere in this procedure, e.g. on "i := 1;", you
can step through the following code and inspect all variables by moving
the mouse pointer over the name of a variable. It even will show you the
content of arrayOfIntegers[i], depending on the current value of i.
Then you'll find that the array is not initialized, because the condition in
while (i = 9) do ...
is never True.
Okay, this may be a typo, should read (i <= 9) instead?
Please fix this and try again.
Finally I assume that you expect too much from IntToStr(), in case you
want to display *all* array elements at once. That doesn't work, for two
- IntToStr only converts an single value
- arrayOfInteger[i] is a single value, not the entire array
You need another loop over the array, adding the values to an result
string or to a ListBox control.
DoDi
Thanks DoDi, I tried few things last Friday and it worked for me. the below code worked for me.

There are other things that I will be learning gradually. Dodi, could you please share some good Delhpi coding links (where I can get the coding content).
Also, I am looking to learn debugging.
Form1.Color := clCream;
k := StrToInt(Edit1.Text);
{$R+} // Set range checking on
For j := 1 to k do
begin
TIntegerArray[j] := ' '+IntToStr(j); // ' ' to get the data in the next line
end;
For i := 1 to k do
begin
If i = 1
then result := TIntegerArray[i]
else result := result + TIntegerArray[i];
end;
Label3.Caption := result+sLineBreak;
Sandeepan Kashyap
2014-09-15 09:36:30 UTC
Permalink
Dodi- I really appreciate your time and help. Thank you very much once again.
Post by Sandeepan Kashyap
Post by Hans-Peter Diettrich
Post by Sandeepan Kashyap
Thanks DoDi,
I am actually an experienced Mumps/Cache developer and have to learn
Delphi for a new project. I am actually trying to print a series of
number in ascending order, e.g. 1,2,3,4,5,6,7,8,9 but in every number
should print in new followed line. like
1
2
3
..
Let's look into this later, it depends on what you mean by "printing".
When you want to show multiple lines on a form, use a TListBox or TMemo,
not an TLabel.
Post by Sandeepan Kashyap
I tried the below code. But the below code printing '0' always inside Label3, don't know why? what's is wrong with the code. Please enlighten me.
type
TIntegerArray = Array[1..9] of Integer;
var
x,y : integer;
txt: string;
i: integer;
arrayOfIntegers : TIntegerArray;
begin
Form1.Color := clGreen;
x := StrToInt(Edit1.Text);
y := StrToInt(Edit2.Text);
i := 1 ;
begin
while (i = 9) do
arrayOfIntegers[i] := i;
Inc(i);
end;
What's the value of i here?
What's the value of arrayOfIntegers[i]?
Post by Sandeepan Kashyap
Label3.Caption := IntToStr(arrayOfIntegers[i]);
Please try to learn how to use the Delphi debugging features. When you
put an breakpoint somewhere in this procedure, e.g. on "i := 1;", you
can step through the following code and inspect all variables by moving
the mouse pointer over the name of a variable. It even will show you the
content of arrayOfIntegers[i], depending on the current value of i.
Then you'll find that the array is not initialized, because the condition in
while (i = 9) do ...
is never True.
Okay, this may be a typo, should read (i <= 9) instead?
Please fix this and try again.
Finally I assume that you expect too much from IntToStr(), in case you
want to display *all* array elements at once. That doesn't work, for two
- IntToStr only converts an single value
- arrayOfInteger[i] is a single value, not the entire array
You need another loop over the array, adding the values to an result
string or to a ListBox control.
DoDi
Thanks DoDi, I tried few things last Friday and it worked for me. the below code worked for me.
There are other things that I will be learning gradually. Dodi, could you please share some good Delhpi coding links (where I can get the coding content).
Also, I am looking to learn debugging.
Form1.Color := clCream;
k := StrToInt(Edit1.Text);
{$R+} // Set range checking on
For j := 1 to k do
begin
TIntegerArray[j] := ' '+IntToStr(j); // ' ' to get the data in the next line
end;
For i := 1 to k do
begin
If i = 1
then result := TIntegerArray[i]
else result := result + TIntegerArray[i];
end;
Label3.Caption := result+sLineBreak;
Hans-Peter Diettrich
2014-09-15 12:14:38 UTC
Permalink
Post by Sandeepan Kashyap
There are other things that I will be learning gradually. Dodi, could you please share some good Delhpi coding links (where I can get the coding content).
Look at the sample programs, coming with Delphi.

DoDi
Alan Lloyd
2014-09-25 10:51:07 UTC
Permalink
You've got an excess begin / end so the last three lines are outside the
procedure, its a wonder it compiled.

You'd be better indenting your code appropriately.

But your basic error is not inserting carriage returns (#13) between each
character.

In any case, why transfer the edit.text to integers & then back to text. A
simpler code is below, and an ever better coding which is more OO is after
that, using an independent procedure called from the On Click.

procedure TForm1.Button2Click(Sender: TObject);
var
i : integer;
Txt: string;
const
CR = #13;
begin
Self.Color := clAqua;
Txt := '';
for i := 1 to Length(Edit1.Text) do begin
Txt := Txt + Edit1.text[i] + CR
end;
SetLength(Txt, Length(Txt) - 1);
Label3.Caption := Txt;
end;

// better coding

procedure TForm1.Button3Click(Sender: TObject);
begin
Self.Color := clAqua;
ShowDigits(Edit1, Label3);
end;

procedure TForm1.ShowDigits(AnEdit : TEdit; ALabel : TLabel);
var
i : integer;
Str: string;
const
CR = #13;
begin
Str := '';
for i := 1 to Length(AnEdit.Text) do begin
Str := Str + AnEdit.Text[i] + CR
end;
// remove final CR - easier & quicker than condition coding above
SetLength(Str, Length(Str) - 1);
ALabel.Caption := Str;
end;


Alan Lloyd
Post by Sandeepan Kashyap
Hello,
I am new to Delphi and trying to learn it through internet.
Could you please refer some good links for basic Delphi to learn it?
Trying - While hitting button1 and it should display all the arrays values
in defined LABEL in a form of 1,2,3... etc. every digit should come
beneath to each other in new line.
1
2
3
Issue - It's only displaying the last value, like if you enter 3, it's
just displaying only 3. Please suggest what wrong I am doingg? Below Is my
code.
procedure TForm1.Button1Click(Sender: TObject);
var
x,y,i,Leng : integer;
txt: string;
MyMatrix: packed array of char;
begin
Form1.Color := clGreen;
x := StrToInt(Edit1.Text);
y := StrToInt(Edit2.Text);
Leng := Length(Edit1.Text);
SetLength(MyMatrix, leng);
for i := 1 to 3 do begin
begin
MyMatrix[i] := Edit1.text[i];
end;
end;
end;
for i:= 1 to leng do begin
txt := txt + MyMatrix[i];
end;
Label3.caption := txt;
Sandeepan Kashyap
2014-09-26 12:58:13 UTC
Permalink
Thanks Alan for making me understand the code in a better way. Evidentially, the above code makes better sense to me. There is one thing, while trying to implement the above code, I am getting the below error.

If I pass the parameters in the below way, I am getting error at 58 line.
procedure ShowDigits(Edit1,Label3,Sender: TObject); //getting [Error] Final.pas(58): then getting too many parameters
procedure ShowDigits(Edit1,Label3); //getting [Error] Final.pas(58): Missing parameter type
procedure ShowDigits(Sender: TObject); //getting [Error] Final.pas(58): Too many actual parameters.
begin
Form1.Color := clCream;
ShowDigits(Edit1,Label3); //58 line where I am getting error
end;
procedure ShowDigits(AnEdit : TEdit; ALabel : TLabel);
begin
.
.
end;

I guess, I am missing either the correct way of declaring the procedure or may be the correct way of Type of parameters.
Please suggest

Thanks again for you help.

Sandeepan.
Post by Alan Lloyd
You've got an excess begin / end so the last three lines are outside the
procedure, its a wonder it compiled.
You'd be better indenting your code appropriately.
But your basic error is not inserting carriage returns (#13) between each
character.
In any case, why transfer the edit.text to integers & then back to text. A
simpler code is below, and an ever better coding which is more OO is after
that, using an independent procedure called from the On Click.
procedure TForm1.Button2Click(Sender: TObject);
var
i : integer;
Txt: string;
const
CR = #13;
begin
Self.Color := clAqua;
Txt := '';
for i := 1 to Length(Edit1.Text) do begin
Txt := Txt + Edit1.text[i] + CR
end;
SetLength(Txt, Length(Txt) - 1);
Label3.Caption := Txt;
end;
// better coding
procedure TForm1.Button3Click(Sender: TObject);
begin
Self.Color := clAqua;
ShowDigits(Edit1, Label3);
end;
procedure TForm1.ShowDigits(AnEdit : TEdit; ALabel : TLabel);
var
i : integer;
Str: string;
const
CR = #13;
begin
Str := '';
for i := 1 to Length(AnEdit.Text) do begin
Str := Str + AnEdit.Text[i] + CR
end;
// remove final CR - easier & quicker than condition coding above
SetLength(Str, Length(Str) - 1);
ALabel.Caption := Str;
end;
Alan Lloyd
Post by Sandeepan Kashyap
Hello,
I am new to Delphi and trying to learn it through internet.
Could you please refer some good links for basic Delphi to learn it?
Trying - While hitting button1 and it should display all the arrays values
in defined LABEL in a form of 1,2,3... etc. every digit should come
beneath to each other in new line.
1
2
3
Issue - It's only displaying the last value, like if you enter 3, it's
just displaying only 3. Please suggest what wrong I am doingg? Below Is my
code.
procedure TForm1.Button1Click(Sender: TObject);
var
x,y,i,Leng : integer;
txt: string;
MyMatrix: packed array of char;
begin
Form1.Color := clGreen;
x := StrToInt(Edit1.Text);
y := StrToInt(Edit2.Text);
Leng := Length(Edit1.Text);
SetLength(MyMatrix, leng);
for i := 1 to 3 do begin
begin
MyMatrix[i] := Edit1.text[i];
end;
end;
end;
for i:= 1 to leng do begin
txt := txt + MyMatrix[i];
end;
Label3.caption := txt;
Alan Lloyd
2014-09-26 18:53:18 UTC
Permalink
Sandeepan

When you define & use a procedure you must :

1) Specify the names and types of the parameters and the type of method
(procedure or function) in the interface section of the unit; either in the
class definition (TSomthing = Class (ancestor) . . . end; if its a class
method, or just in the section (if its not a class method). For this general
procedure it must be
procedure Procname(Para1 : Paratype1; Para2: Para2type; etc etc );

2) Specify in the implementation section a repeat of the interface
definition) with the same number & type of parameters, but with the class of
the procedure prefixing the name of the procedure -
procedure TSomething.Procname(Para1 : Paratype1; Para2: Para2type;
etc etc );
If it is not a class procedure (ie the previous definition is in the
interface section but not in a class definition) then the class name prefix
is ommitted -
procedure Procname(Para1 : Paratype1; Para2: Para2type; etc etc );

3) Then this procedure definition in the implementation section must be
followed by var;, const, begin (your code) end; as
procedure TSomething.Procname(Para1 : Paratype1; Para2: Para2type; etc
etc );
var
MyVar : MyVarType;
etc, etc,
const
MyConst = MyConstValue;
etc, etc
begin
my code for the procedure
etc etc
end;

or if the procedure is not a class method
procedure Procname(Para1 : Paratype1; Para2: Para2type; etc etc );
var
MyVar : MyVarType;
etc, etc,
const
MyConst = MyConstValue;
etc, etc
begin
my code for the procedure
etc etc
end;

When you call the procedure to be run, you omit the parameter types in the
call, just using the name of the item which is the input value to the
procedure. If the types or number of parameters are different between the
definition and the call, then an error will be raised (this is what happened
in your list of errors).

Try & get hold of a good book on Delphi, even if it is for an earlier
version it will be simpler and the examples should run in a later version of
Delphi.

Alan Lloyd
Post by Sandeepan Kashyap
Thanks Alan for making me understand the code in a better way.
Evidentially, the above code makes better sense to me. There is one thing,
while trying to implement the above code, I am getting the below error.
If I pass the parameters in the below way, I am getting error at 58 line.
procedure ShowDigits(Edit1,Label3,Sender: TObject); //getting [Error]
Final.pas(58): then getting too many parameters
Missing parameter type
procedure ShowDigits(Sender: TObject); //getting [Error]
Final.pas(58): Too many actual parameters.
begin
Form1.Color := clCream;
ShowDigits(Edit1,Label3); //58 line where I am getting error
end;
procedure ShowDigits(AnEdit : TEdit; ALabel : TLabel);
begin
.
.
end;
I guess, I am missing either the correct way of declaring the procedure or
may be the correct way of Type of parameters.
Please suggest
Thanks again for you help.
Sandeepan.
Post by Alan Lloyd
You've got an excess begin / end so the last three lines are outside the
procedure, its a wonder it compiled.
You'd be better indenting your code appropriately.
But your basic error is not inserting carriage returns (#13) between each
character.
In any case, why transfer the edit.text to integers & then back to text. A
simpler code is below, and an ever better coding which is more OO is after
that, using an independent procedure called from the On Click.
procedure TForm1.Button2Click(Sender: TObject);
var
i : integer;
Txt: string;
const
CR = #13;
begin
Self.Color := clAqua;
Txt := '';
for i := 1 to Length(Edit1.Text) do begin
Txt := Txt + Edit1.text[i] + CR
end;
SetLength(Txt, Length(Txt) - 1);
Label3.Caption := Txt;
end;
// better coding
procedure TForm1.Button3Click(Sender: TObject);
begin
Self.Color := clAqua;
ShowDigits(Edit1, Label3);
end;
procedure TForm1.ShowDigits(AnEdit : TEdit; ALabel : TLabel);
var
i : integer;
Str: string;
const
CR = #13;
begin
Str := '';
for i := 1 to Length(AnEdit.Text) do begin
Str := Str + AnEdit.Text[i] + CR
end;
// remove final CR - easier & quicker than condition coding above
SetLength(Str, Length(Str) - 1);
ALabel.Caption := Str;
end;
Alan Lloyd
Post by Sandeepan Kashyap
Hello,
I am new to Delphi and trying to learn it through internet.
Could you please refer some good links for basic Delphi to learn it?
Trying - While hitting button1 and it should display all the arrays values
in defined LABEL in a form of 1,2,3... etc. every digit should come
beneath to each other in new line.
1
2
3
Issue - It's only displaying the last value, like if you enter 3, it's
just displaying only 3. Please suggest what wrong I am doingg? Below Is my
code.
procedure TForm1.Button1Click(Sender: TObject);
var
x,y,i,Leng : integer;
txt: string;
MyMatrix: packed array of char;
begin
Form1.Color := clGreen;
x := StrToInt(Edit1.Text);
y := StrToInt(Edit2.Text);
Leng := Length(Edit1.Text);
SetLength(MyMatrix, leng);
for i := 1 to 3 do begin
begin
MyMatrix[i] := Edit1.text[i];
end;
end;
end;
for i:= 1 to leng do begin
txt := txt + MyMatrix[i];
end;
Label3.caption := txt;
Loading...