using only one procedure for more than one components

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 29
Reputation: ferhatkuskaya is an unknown quantity at this point 
Solved Threads: 0
ferhatkuskaya ferhatkuskaya is offline Offline
Light Poster

using only one procedure for more than one components

 
0
  #1
Nov 26th, 2007
Now I want to know how I will short my code lines within one procudures just as sharing of events in only one procedures. for now I have written my program code like that but I want to learn how I will use events for 5 components within IF conditions. pls tell me how I will short these code line below. If you write to me or any sample like solution of below code scope I will be very glad from you..
procedure TAnaform.CheckBox2Click(Sender: TObject);
begin
if Checkbox2.Checked then
begin
Anaform.DIKEYAC2.Visible:=True;
Anaform.YATAYKAPA2.Visible:=False;
end;
if not (Checkbox2.Checked) then
begin
Anaform.DIKEYAC2.Visible:=False;
Anaform.YATAYKAPA2.Visible:=True;
end;
end;

procedure TAnaform.CheckBox3Click(Sender: TObject);
begin
if Checkbox3.Checked then
begin
Anaform.DIKEYAC3.Visible:=True;
Anaform.YATAYKAPA3.Visible:=False;
end;
if not (Checkbox3.Checked) then
begin
Anaform.DIKEYAC3.Visible:=False;
Anaform.YATAYKAPA3.Visible:=True;
end;
end;

procedure TAnaform.CheckBox4Click(Sender: TObject);
begin
if Checkbox4.Checked then
begin
Anaform.DIKEYAC4.Visible:=True;
Anaform.YATAYKAPA4.Visible:=False;
end;
if not (Checkbox4.Checked) then
begin
Anaform.DIKEYAC4.Visible:=False;
Anaform.YATAYKAPA4.Visible:=True;
end;
end;

procedure TAnaform.CheckBox5Click(Sender: TObject);
begin
if Checkbox5.Checked then
begin
Anaform.DIKEYAC5.Visible:=True;
Anaform.YATAYKAPA5.Visible:=False;
end;
if not (Checkbox5.Checked) then
begin
Anaform.DIKEYAC5.Visible:=False;
Anaform.YATAYKAPA5.Visible:=True;
end;
end;
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: using only one procedure for more than one components

 
0
  #2
Nov 26th, 2007
Same kind of answer as in your other thread.

For each tCheckBox, set the tag property to 1, 2, 3, etc. as appropriate. Then give them all an onClick event method like this:
  1. procedure tAnaForm.CheckBoxClick( sender: tObject );
  2. begin
  3. if tCheckBox( sender ).checked
  4. then begin
  5. tLabel( AnaForm.findComponent( 'DIKEYAC' +intToStr( sender.tag ) ) ).visible := true;
  6. ...
  7. end
  8. else begin
  9. ...
  10. end
  11. end;
Again, untested! Of course, I don't know what type of things DIKEYAC and YATAYKAPA are, so I just used "tLabel". You'll need to cast to the correct type of thing...

Also, please use [code] tags...
Last edited by Duoas; Nov 26th, 2007 at 12:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 29
Reputation: ferhatkuskaya is an unknown quantity at this point 
Solved Threads: 0
ferhatkuskaya ferhatkuskaya is offline Offline
Light Poster

Re: using only one procedure for more than one components

 
0
  #3
Nov 27th, 2007
OK think as "dikeyac" and "yataykapa" image components that is included in additional components
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 29
Reputation: ferhatkuskaya is an unknown quantity at this point 
Solved Threads: 0
ferhatkuskaya ferhatkuskaya is offline Offline
Light Poster

Re: using only one procedure for more than one components

 
0
  #4
Nov 27th, 2007
sorry duoas I dont know what tag property of component does?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: using only one procedure for more than one components

 
0
  #5
Nov 27th, 2007
The "tag" doesn't do anything. It's just a longint value you can use for whatever you like.

I've used it above to uniquely identify the components you wish to change when you click a checkbox item.
So yataykapa1 gets modified by the checkbox whose tag is 1, and yataykapa2 gets modified by the checkbox whose tag is 2, etc.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 71
Reputation: Micheus is an unknown quantity at this point 
Solved Threads: 4
Micheus's Avatar
Micheus Micheus is offline Offline
Junior Poster in Training

Re: using only one procedure for more than one components

 
0
  #6
Nov 29th, 2007
Originally Posted by Duoas View Post
For each tCheckBox, set the tag property to 1, 2, 3, etc. as appropriate. Then give them all an onClick event method like this:
  1. procedure tAnaForm.CheckBoxClick( sender: tObject );
  2. begin
  3. if tCheckBox( sender ).checked
  4. then begin
  5. tLabel( AnaForm.findComponent( 'DIKEYAC' +intToStr( sender.tag ) ) ).visible := true;
  6. ...
  7. end
  8. else begin
  9. ...
  10. end
  11. end;
Good tip Duoas. I have used this approach some times too.
But just let me to add that some problem that could be gotten by using type-cast without test returned value. If the component could not be found (bad name or disposed), an exception will occur. Just a sugestion to prevent this:
  1. procedure tAnaForm.CheckBoxClick( sender: tObject );
  2. var
  3. FoundComp :TComponent;
  4. begin
  5. if tCheckBox( sender ).checked
  6. then begin
  7. FoundComp := AnaForm.findComponent( 'DIKEYAC' +intToStr( sender.tag ) );
  8. if FoundComp <> nil then
  9. tLabel( FoundComp ).visible := true;
  10. ...
  11. end
  12. ...
  13. end;

bye
"It always has, at least, two ways to make one same thing. Exactly that they are certain and wrong"(Micheus)

Brazil - Blumenau
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: using only one procedure for more than one components

 
0
  #7
Nov 29th, 2007
It depends. In this case I want an exception to occur, because that would mean he mis-programmed his form, and it will let him know if he does.

That is, it is an inviolate part of his design that the checkbox reference a specific component. This is easily done in the forms designer. Failure to do this causes a runtime exception when testing each button.

If you are aiming to find a component based on some other criteria, then by all means you must validate the result, since it is no longer a design assertion but a very possible runtime error.

Hope this helps.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1061 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Pascal and Delphi
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC