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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find clicked Control in GroupBox 1

Status
Not open for further replies.

hstijnen

Programmer
Nov 13, 2002
172
NL
Hi,

I have a groupbox with some controls, edits, buttons etc.
Now I want to find out, when a user clicks in the groupbox, which control he has clicked on. This seems to be possible with a MouseDown trigger and ControlAtPos().
I tried:

void __fastcall Tform1::GroupBoxMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
TPoint pos = TPoint(X, Y);
TControl *ctrl = ControlAtPos(pos, false, false);
}

But this trigger appears to fire only when I don't click on a control!!

What can be on hand or what can I do?

Thanks

Henk
 
Assign an OnClick to each control in the textbox. You can assign one function or create a new one for each control. To create a new function for each control, just select the control, click on the events tab, then double click in the "On-Click" event. To create one single function, do the same thing above for ONE of the controls on the form, then copy that function name, and go to all the other controls and paste that function name into the "On-click" event field in the Object Inspector.

Now in the single function, you'll have to detect the component you are working with because "Sender" is of type TObject pointer. You will want to check the name or type of component (depending on how much detail you want to know about the component that was clicked). You can check the class name with the TObject pointer sent to you, so just say Sender->ClassNameIs() and that will return the class name, i.e. "TEdit". Since a TComponent is the base class for all VCL components, cast the Sender to a TComponent. Then you can check Sender->Name. Example below.

The reason the code you wrote above does not do what you want is because each component inside the groupbox has the focus. You would have to make a mousedown event for each component. The mousedown event for a groupbox is only if the mouse goes down inside the groupbox, but outside of any of the controls in it.

void __fastcall TForm1::AnythingInGroupClick(TObject *Sender)
{
if (Sender->ClassNameIs() == "TEdit")
{
DisplayString = "You clicked an Edit Box named: ";
DisplayString += ((TComponent*)Sender->)Name;
OR
DisplayString += ((TEdit*)Sender->)Name;
}
if (Sender->ClassNameIs() == "TCheckBox")
{
}
}
OR
void __fastcall TForm1::AnythingInGroupClick(TObject *Sender)
{
TComponent *CompClicked = (TComponent*) Sender;

if (Sender->Name == "Checkbox1")
{
DisplayString = "You clicked on Checkbox1";
}
if (Sender->Name == "Edit1")
{
DisplayString = "You clicked on Edit1";
}
}

Good luck,
Chris
 
Just one thing. It is faster using ClassType() than ClassNameIs(). Like:

if (Sender->ClassType() == __classid(TEdit))

Done.

Unbornchikken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top