Upload
mohammad-shaker
View
573
Download
3
Tags:
Embed Size (px)
Citation preview
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
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
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”.
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.
Memo functions
– Memo1.Lines.add(//String);– Memo1.Lines[i]:= //String;– Memo1.Lines.Clear;
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);
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
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;
ExerciseMemo Notepad
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);
StringGrid
StringGrid
• “StringGrid” Contains “String”s
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]
StringGrid Prop.
• Option:– all “Boolean”s
• GoRowSizing, GoColSizing, default “false”.• GoRowMoving, GoColMoving, default “false”.• Most Imprtant: GoEditing “true”, default “false”. (INPUT)
StringGrid
• Output– Cells[i,j]
• Input:– GoEditing “True”.
MainMenu
MainMenu
• U gonna like it, believe me:D• Designing Time:
MainMenu
• Executing Time:
MainMenu Prop.
• Items: nice & easy • That’s it, Test it live!
Image
Image
• Properties:– Picture – Stretch– Autosize– Width, Height– Visible, Enabled– Top, Left
Shape
Shape Prop.
• Most used for illustrating “Graphics”.• Properties:
– Shape: rectangle, circle .– Brush: Color– Pen– Width, Height– Visible, Enabled – Top, Left
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.
Panel
• BevelIn, BevelOut• Cutting & pasting problem• Test it
Parent Property
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”
Debugging
• Form our Menu > Run > Step Over• Form our Menu > Run > Trace into
– Step Over : F8– Trace into : F7
What’s for today?
• Timer• Sleep• TrackBar• ProgressBar• StatusBar
Timer
Timer
• Properties:– Enabled : default “true”.– Interval : 1 “second” = 1000 “milli-seconds”
• Events:– OnTimer
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;
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
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
Time Machine :PWe can control time with: “Timer” or “Sleep”
Time Machine :PTest it live!
TrackBar
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
TrackBar
procedure TForm11.TrackBar1Change(Sender: TObject);
Begin
Label1.caption:=inttostr(TrackBar1.Position);
end;
ProgressBar
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
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;
StatusBar
StatusBar
• The one that at the bottom to indicate the form’s “status”.
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.
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”
StatusBar Example
procedure TForm1.Button1Click(Sender: TObject);
begin
form2.show();
StatusBar1.SimpleText:='Form2 is running';
end;
Uses unit2;
StatusBar Example
Before clicking “Button1” After clicking “Button1”
Timer – Shape Example
See you!