Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

display array values in a label

479 views
Skip to first unread message

Sandeepan Kashyap

unread,
Sep 11, 2014, 7:02:49 AM9/11/14
to
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

unread,
Sep 11, 2014, 9:46:05 AM9/11/14
to
Sandeepan Kashyap schrieb:

This code

> 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

unread,
Sep 12, 2014, 2:24:14 AM9/12/14
to
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.

Hans-Peter Diettrich

unread,
Sep 12, 2014, 12:17:04 PM9/12/14
to
Sandeepan Kashyap schrieb:
> 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.

> 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]?

> 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

unread,
Sep 15, 2014, 5:35:00 AM9/15/14
to
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

unread,
Sep 15, 2014, 5:36:31 AM9/15/14
to
Dodi- I really appreciate your time and help. Thank you very much once again.

Hans-Peter Diettrich

unread,
Sep 15, 2014, 8:20:43 AM9/15/14
to
Sandeepan Kashyap schrieb:

> 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

unread,
Sep 25, 2014, 6:51:24 AM9/25/14
to
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


"Sandeepan Kashyap" <sandee...@gmail.com> wrote in message
news:917dbf9e-f5aa-4a84...@googlegroups.com...

Sandeepan Kashyap

unread,
Sep 26, 2014, 8:58:15 AM9/26/14
to
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.

Alan Lloyd

unread,
Sep 26, 2014, 2:53:28 PM9/26/14
to
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

"Sandeepan Kashyap" <sandee...@gmail.com> wrote in message
news:61ad2631-66ee-4223...@googlegroups.com...
0 new messages