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;

Recommended Answers

All 6 Replies

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:

procedure tAnaForm.CheckBoxClick( sender: tObject );
  begin
  if tCheckBox( sender ).checked
    then begin
         tLabel( AnaForm.findComponent( 'DIKEYAC' +intToStr( sender.tag ) ) ).visible := true;
         ...
         end
    else begin
         ...
         end
  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 [[I][/I]code[I][/I]] tags...

OK think as "dikeyac" and "yataykapa" image components that is included in additional components

sorry duoas I dont know what tag property of component does?

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.

Member Avatar for Micheus

For each tCheckBox, set the tag property to 1, 2, 3, etc. as appropriate. Then give them all an onClick event method like this:

procedure tAnaForm.CheckBoxClick( sender: tObject );
  begin
  if tCheckBox( sender ).checked
    then begin
         tLabel( AnaForm.findComponent( 'DIKEYAC' +intToStr( sender.tag ) ) ).visible := true;
         ...
         end
    else begin
         ...
         end
  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:

procedure tAnaForm.CheckBoxClick( sender: tObject );
var
  FoundComp :TComponent;
  begin
  if tCheckBox( sender ).checked
    then begin
         FoundComp := AnaForm.findComponent( 'DIKEYAC' +intToStr( sender.tag ) );
         if FoundComp <> nil then
              tLabel( FoundComp ).visible := true;
         ...
         end
...
end;

bye

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.