Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Dynamically identify checkboxes Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
27-Aug-02
Category
VCL-General
Language
Delphi All Versions
Views
75
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

My code looks something like this: ... if CheckBox(var).checked = True then ... 
where (var) is a counter in a for loop. Is the number of checkboxes not known when 
coding , ie created only at run time?

Answer:

When in design mode, you really should know how many checkboxes are on a given 
form. When the App is running, use Delphi's Run Time Type Information (RTTI). For a 
given form, try the following code snippet:

1   var
2     i: Integer
3   begin
4     for i := 0 to ComponentCount - 1 do
5       if Components[i] is TCheckBox then
6         (Components[i] as TCheckBox).Checked then
7       begin
8         {... insert your code here ...}
9       end;
10  end;
11  
12  //In addition, the following code is a valid statement in Delphi:
13  
14  if Components[i] = CheckBox5 then  DoSomething;


Also, each component in Delphi has a Published Property called 'Tag', you can use 
this to your advantage by setting the Tag to some non-zero number at design time, 
then using it at runtime, ie:

15  var
16    i: Integer
17  begin
18    for i := 0 to ComponentCount - 1 do
19      if Components[i] is TCheckBox then
20        with (Components[i] as TCheckBox) do
21          case Tag of
22            1: if Checked then
23                DoSomethingOnBox1;
24            2: if Checked then
25                DoSomethingOnBox2;
26            {... etc ...}
27          end;
28  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC