53
Mohammad Shaker mohammadshakergtr.wordpress.com Intro to Event-driven Programming and Forms with Delphi @ZGTRShaker 2010, 2011, 2012 Intro to Event-driven Programming and Forms with Delphi L04 – Controls P2

Delphi L04 Controls P2

Embed Size (px)

Citation preview

Page 1: Delphi L04 Controls P2

Mohammad Shakermohammadshakergtr.wordpress.com

Intro to Event-driven Programming and Forms with Delphi@ZGTRShaker

2010, 2011, 2012

Intro to Event-driven Programming and Forms with Delphi

L04 – Controls P2

Page 2: Delphi L04 Controls P2
Page 3: Delphi L04 Controls P2

Memo

• It’s a standard Windows multiline “edit” control.• That’s it :D• Difference to “Edit”:

– Memo allow the user to enter more than one line of text.

• necessary for “input” or “output” lengthy information.• The most used “component” to deal with “file”s

Page 4: Delphi L04 Controls P2

Memo Prop.

• Lines (String):– Manipulate text in “Memo” control on a “line-by-line” basis.

(Manipulate individual lines of text)– Counting lines in the text.– Adding new lines.– Deleting lines.– Replacing lines with new text “String”.

Page 5: Delphi L04 Controls P2

Memo Prop.

• ScrollBars:– None, Horizontal, Vertical, Both

• Text:– Dealing with all text at once

• MaxLength:– “Get” or “Set” maximum numbers of characters that a “Memo” can

contain.

• Count:– Numbers of current used lines.

Page 6: Delphi L04 Controls P2

Memo functions

– Memo1.Lines.add(//String);– Memo1.Lines[i]:= //String;– Memo1.Lines.Clear;

Page 7: Delphi L04 Controls P2

Memo functions

• Let’s test the following:– Memo1.Lines.IndexOf(Const s:string): integer;– Memo1.Lines.Insert(Index:integer, Const s:string);– Memo1.Lines.Delete(Index:integer);– Memo1.Lines.AddStrings(Strings:TStrings);

Page 8: Delphi L04 Controls P2

Memo functions

// add to last position in memo

Memo1.Lines.add(‘heeeeeheeeeeeeI am adding to Memo ’);

// add OR modify to specific position (line) in the memo

Memo1.Lines[0]:=‘heeeeeheeeeeeeI am adding to Memo ’;

//Initializing

for I:= 0 to 4 do

Memo1.lines.add('');

//filing memo with its values

Memo1.Lines[0]:=‘heeeeeheeeeeeeI am adding to Memo ’;

Memo1.Lines[1]:=‘heeeeeheeeeeeeI am adding to Memo ’;

Memo1.Lines[2]:=‘heeeeeheeeeeeeI am adding to Memo ’;

Memo1.Lines[3]:=‘heeeeeheeeeeeeI am adding to Memo ’;

Memo1.Lines[4]:=‘heeeeeheeeeeeeI am adding to Memo ’;

When Multi lines,

InitializeFIRST

Page 9: Delphi L04 Controls P2

Memo functions//counting number of character in memox:=Memo1.Lines.Count;

//printing integer variables into memoMemo1.lines.add(inttostr(x));

//clearing all memoMemo1.Lines.Clear;

Page 10: Delphi L04 Controls P2
Page 11: Delphi L04 Controls P2

ExerciseMemo Notepad

Page 12: Delphi L04 Controls P2

Notepad MeMo

• Additional functions for “Notepad”.– Load all info form a “Specific file” to “Memo”:

– Save all info from “Memo” to a “Specific file”:Memo1.lines.LoadFromFile(Const FileName:String);

Memo1.lines.SaveToFile(Const FileName:String);

Page 13: Delphi L04 Controls P2

StringGrid

Page 14: Delphi L04 Controls P2

StringGrid

• “StringGrid” Contains “String”s

Page 15: Delphi L04 Controls P2

StringGrid Prop.

• ScrollBars:– None, Horizontal, Vertical, Both

• Fixed Row\Col:– FixesCol: number of “fixed Column”s. – FixesRows: number of “fixed Row”s.

• Indexes:– ColCount: number of “column”s.– RowCount: number of “Row”s.

• Cells[i,j]: // Run Time– returns the “String” in index [i,j]

Page 16: Delphi L04 Controls P2

StringGrid Prop.

• Option:– all “Boolean”s

• GoRowSizing, GoColSizing, default “false”.• GoRowMoving, GoColMoving, default “false”.• Most Imprtant: GoEditing “true”, default “false”. (INPUT)

Page 17: Delphi L04 Controls P2

StringGrid

• Output– Cells[i,j]

• Input:– GoEditing “True”.

Page 18: Delphi L04 Controls P2

MainMenu

Page 19: Delphi L04 Controls P2

MainMenu

• U gonna like it, believe me:D• Designing Time:

Page 20: Delphi L04 Controls P2

MainMenu

• Executing Time:

Page 21: Delphi L04 Controls P2

MainMenu Prop.

• Items: nice & easy • That’s it, Test it live!

Page 22: Delphi L04 Controls P2

Image

Page 23: Delphi L04 Controls P2

Image

• Properties:– Picture – Stretch– Autosize– Width, Height– Visible, Enabled– Top, Left

Page 24: Delphi L04 Controls P2

Shape

Page 25: Delphi L04 Controls P2

Shape Prop.

• Most used for illustrating “Graphics”.• Properties:

– Shape: rectangle, circle .– Brush: Color– Pen– Width, Height– Visible, Enabled – Top, Left

Page 26: Delphi L04 Controls P2

Shape most used Events

• Most Important – Create: can be used to “Auto-Create” shapes in “Runtime” using

“Pointer”s– Destroy: can be used to “Auto-Destroy” shapes at “Runtime” using

“Pointer”s– Hide – Refresh– Repaint

• “Pen, Brush, Enabledetc” properties can all be changed at “Runtime” AS USUAL.

Page 27: Delphi L04 Controls P2

Panel

• BevelIn, BevelOut• Cutting & pasting problem• Test it

Page 28: Delphi L04 Controls P2

Parent Property

Page 29: Delphi L04 Controls P2

procedure TForm1.Button2Click(Sender: TObject);

begin

Button2.Parent:= Panel2;

end;

Parent Property

• Every component has sth called “Parent” – like a “Button” in a “Panel”

• So, the “Panel” is the “Parent” of the “Button”

– like a “Button” in a “Form”• So, the “Form” is the “Parent” of the “Button”

• Some Controls have no “Parent”– Form’s parent It’s “NIL”

Page 30: Delphi L04 Controls P2

Debugging

• Form our Menu > Run > Step Over• Form our Menu > Run > Trace into

– Step Over : F8– Trace into : F7

Page 31: Delphi L04 Controls P2
Page 32: Delphi L04 Controls P2

What’s for today?

• Timer• Sleep• TrackBar• ProgressBar• StatusBar

Page 33: Delphi L04 Controls P2

Timer

Page 34: Delphi L04 Controls P2

Timer

• Properties:– Enabled : default “true”.– Interval : 1 “second” = 1000 “milli-seconds”

• Events:– OnTimer

Page 35: Delphi L04 Controls P2

Timer

• In design time “Enabled” Prop. is: “false”.• At Runtime:

procedure TForm11.Button1Click(Sender: TObject);

Begin

Timer1.Enabled:=True;

end;

procedure TForm11.Timer1Timer(Sender: TObject);

Begin

if (strtoint(Label1.Caption)> 0) then

Label1.Caption:=inttostr(strtoint(label1.Caption)-1);

end;

Page 36: Delphi L04 Controls P2

Sleep - Refresh

• Sleep: “Stall” s the program.

• Refresh:– To solve the “Sleep” problem, we use the “Refresh” method.– Most used for:

Form1.Refresh;

// Here the form will be refreshed

Shape1.Refresh;

// Here the shape will be refreshed

Sleep(100);

// here the program executing will be stopped for

// 0.1 sec

Page 37: Delphi L04 Controls P2

Sleep - Refresh

• Test it live as this example:– We’ll do a simulation on one shape like Hanoi towers– Let’s have a shape “Rectangle”– Now, we want to move it upward

• WE NEED TO SEE THE MOVEMENT

Page 38: Delphi L04 Controls P2

Time Machine :PWe can control time with: “Timer” or “Sleep”

Page 39: Delphi L04 Controls P2

Time Machine :PTest it live!

Page 40: Delphi L04 Controls P2

TrackBar

Page 41: Delphi L04 Controls P2

TrackBar

• Properties:– Max : Maximum value (100).– Min : Minimum value (0).– Position: Where the “Slider” stands (1).– Orientation:

• “Vertical” or “Horizontal”

– SliderVisible:• “Boolean” for showing\hiding the “Slider”.

• Events:– Most important: OnChange

Page 42: Delphi L04 Controls P2

TrackBar

procedure TForm11.TrackBar1Change(Sender: TObject);

Begin

Label1.caption:=inttostr(TrackBar1.Position);

end;

Page 43: Delphi L04 Controls P2

ProgressBar

Page 44: Delphi L04 Controls P2

ProgressBar

• Properties:– Max : Maximum value (100).– Min : Minimum value (0).– Position :

• How much “filled” the ProgressBar is (0).• This is showed just in “Run Time”.

– Orientation:• “Vertical” or “Horizontal”• if changed:

– you have to resize your “ProgressBar”, this’s a crazy thing:D

– Step (integer):• Determines how much the “one” step will be

Page 45: Delphi L04 Controls P2

ProgressBar Example

procedure TForm1.Timer1Timer(Sender: TObject);

begin

ProgressBar1.Position:= ProgressBar1.Position + 1;

end;

procedure TForm1.Timer1Timer(Sender: TObject);

begin

ProgressBar1.StepIt;

end;

procedure TForm1.Timer1Timer(Sender: TObject);

begin

ProgressBar1.StepBy(50);

end;

Page 46: Delphi L04 Controls P2

StatusBar

Page 47: Delphi L04 Controls P2

StatusBar

• The one that at the bottom to indicate the form’s “status”.

Page 48: Delphi L04 Controls P2

StatusBar Prop.

• SimplePanel:– Boolean: enables the “StatusBar” when “true”, default false.– Show the “text” when “true”.

• SimpleText: – String: The “StatusBar” text.

• Visible, Enabled, Fontetc. as usual.

Page 49: Delphi L04 Controls P2

StatusBar notes

• Note:– We can change the “SimpleText” in runtime by writing the proper code

that match the form’s STATUS.– A “Written SimpleText” without “SimplePanel” is true will not show

anything. This’s like “Hint” & “ShowHint”

Page 50: Delphi L04 Controls P2

StatusBar Example

procedure TForm1.Button1Click(Sender: TObject);

begin

form2.show();

StatusBar1.SimpleText:='Form2 is running';

end;

Uses unit2;

Page 51: Delphi L04 Controls P2

StatusBar Example

Before clicking “Button1” After clicking “Button1”

Page 52: Delphi L04 Controls P2

Timer – Shape Example

Page 53: Delphi L04 Controls P2

See you!