Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Utilizing Tag property w/ For loop?

Status
Not open for further replies.

sassygirl

Technical User
Mar 20, 2002
16
0
0
US
Hiya,
This is the setup: I have 80 TBitBtn's and each one has it's own information. By clicking on a button, it sends that information to a couple displays. Then, from there I do calculations from the display's->Text.
This is what I intend to do: I need for Borland to self-click, enable, or to activate each individual button. Sorry, I don't know the terminology. So right now I'd have to write 80 lines of "if else" statements if I don't find a solution, ugh.
Your help could be: Tips on how to use the Tag property. =)

-sassygirl...
 
I hope this helps ...

You could do something like this to "filter" on the tag property:

Code:
     // Loop through the controls on the form
     // enabling all of those whos tag contains 57
     for(i=0;i<this->ControlCount;i++)
     {
          TControl* ctrl = this->Controls[i];
          if (ctrl->Tag == 57)
               ctrl->Enabled = true;
     }

But, if I understood you correctly, you may need more &quot;control&quot; over the BitBtn control. You might want to try creating these controls dynamically at runtime. This is easy if the positions of the buttons are not &quot;random&quot;. Here's an example:

Code:
     //--- Declared as Public or Private ---
     TBitBtn *btn[80];     
.
.
.    //---In the OnCreate event of the form---
     for (y=0;y<80;y++)
     {
          btn[y] = new TBitBtn(this);
          btn[y]->Left = 10;
          btn[y]->Top = 10;
          btn[y]->Parent = this;  // Important!
          btn[y]->Align = alNone;
          .
          .   MORE PROPERTIES
          .
          btn[y]->Top = 10 + (y * 20);
          btn[y]->Visible = true;
          btn[y]->Width = 18;
          // Assign your events like this ...
          btn[y]->OnClick = MyBitBtnClickFn;
     }

This would allow you to more control over your BItBtns. To execute an event, such as the OnClick event, just do this:

Code:
          // This will call MyBitBtnClickFn 
          btn[59]->OnClick();


 
Almost there, jnecciai. My intentions are closer to your first suggestion but I was not able to see what this code does:

for(i=0;i<this->ControlCount;i++)
{
TControl* ctrl = this->Controls;
if (ctrl->Tag == 57)
ctrl->Enabled = true;
}
This code is not exactly what I needed, but I just wanted to see what it does. Is it possible to assign a function or a bit button to a tag so that I don't have to list the function or button several times and just simply do a for loop, incrementing tag? =)

 
In your original question, you wrote &quot;... right now I'd have to write 80 lines of &quot;if else&quot; statements ...&quot;

Could you give me an example of one of these if else statements? Maybe it will help me understand your intentions a little better.

As for the example, it just loops through all of the controls on a form and enables only those where the value of the Tag property = 57.
 
Hello,
I forgot to mention that I abandoned the &quot;if else&quot; statement approach but now going with something similar. So this is part of my current code:

TX_controlClick(Sender);
Read_XIOClick(Sender);
TX_fcw_iClick(Sender);
Read_XIOClick(Sender);
TX_dco_pClick(Sender);
Read_XIOClick(Sender);

In continuation, I'd have call all 80 functions for all the different &quot;TX_*&quot; and then call Read_XIOClick(Sender). So now I'm trying to find a short cut by labeling tag on the actual TBitBtn. Is it possible? =)
 
Ok, I think I understand where you are going ...
How about this:

Code:
     int i;
     HWND hwBtn;            // Handle of TBitBtn
     AnsiString sClsName;
     // Loop through the controls on the form
     // enabling all of those whos tag contains 57
     for(i=0;i<this->ControlCount;i++)
     {
          TControl* ctrl = this->Controls[i];
          sClsName = (AnsiString) ctrl->ClassName();
          if (sClsName == &quot;TBitBtn&quot;)
          {
               // Dynamic Cast
               TBitBtn* btn = dynamic_cast<TBitBtn*>(ctrl);
               // Get the Windows Handle of the BitBtn
               hwBtn = btn->Handle;
               // Send Windows Messages to the control
               // to simulate a Left-Click
               SendMessage( hwBtn, WM_LBUTTONDOWN, 0, 0);
               SendMessage( hwBtn, WM_LBUTTONUP, 0, 0);
          }
     }

The above code automatically 'clicks' every TBitBtn on the form. Other controls remain unaffected.

Getting closer ???
 
No where in the code does it mention &quot;57&quot; so how would it know to click on TBitBtn with tag 57?

This is what i did, I first changed a few TBitBtn tags to 57 and ran the code. Apparently it did not click on any of them. Also, in the section:

SendMessage( hwBtn, WM_LBUTTONDOWN, 0, 0);
SendMessage( hwBtn, WM_LBUTTONUP, 0, 0);

do I have to specify any parameters? =)

Many thanks for helping.
 
I'm sorry, the '57' was left over from a previous post.

The code in the last post simply loops through all of the controls on the form. When it finds a TBitBtn, it gets a handle to the button and sends a windows &quot;Click&quot; message to it.

You will still need to utilize the tag IF you don't want ALL of the TBitBtns to be clicked. within the loop above, You would check for a specific value in the tag (like 57 or something) before sending the windows messages.

If you want to 'click' ALL of the TBitBtns, then the code above should work.

Just to make sure ... I built a small example:
 
Whoops, you're using Borland Builder 5.0 whereas I'm still using 4.0. Your file is not compatible. =( But I'll keep working on how to implement your previous code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top