how to write a function Click() for dynamic created button? - c++

Trying to write a simple VCL program for educating purposes (dynamicly created forms, controls etc). Have such a sample code:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TForm* formQuiz = new TForm(this);
formQuiz->BorderIcons = TBorderIcons() << biSystemMenu >> biMinimize >> biMaximize;
formQuiz->Position = TPosition::poDesktopCenter;
formQuiz->Width = 250;
formQuiz->Height = 250;
formQuiz->Visible = true;
TButton* btnDecToBin = new TButton(formQuiz);
btnDecToBin->Parent = formQuiz;
btnDecToBin->Left = 88;
btnDecToBin->Top = 28;
btnDecToBin->Caption = "Dec to Bin";
btnDecToBin->Visible = true;
}
I wonder how can i write a function for dynamic created button, so it would be called when the button is clicked. In this example i need a 'btnDecToBin->Click();' func but i don't know where should i place it.
Inside 'void __fastcall TForm1::Button1Click(TObject *Sender){}' ?
I will appreciate any input, some keywords for google too.

You could do two things, you could either create an action and associate it with the button, or you could make a function like so:
void __fastcall TForm1::DynButtonClick(TObject *Sender)
{
// Find out which button was pressed:
TButton *btn = dynamic_cast<TButton *>(Sender);
if (btn)
{
// Do action here with button (btn).
}
}
You bind it to the button instance by setting the OnClick property btnDecToBin->OnClick = DynButtonClick please note that the function is inside the form Form1. This will work due to the nature of closures (compiler specific addition). The problem comes if you delete Form1 before formQuiz without removing the reference to the click event. In many ways it might be a more clean solution to use an Action in this case.
Edit: On other way to do this, if you have a standard layout for your quizforms, you could make a custom TQuizForm class inheriting from TForm. In this way you wouldn't have to bind the event each time you create the form.

all buttons have the normal "events" you just need to reference them to the method you will deal with the event.
example:
...
btnDecToBin->OnClick = &Test;
-- and add a additional method to .cpp
void __fastcall TForm1::Test(TObject *Sender)
{
TButton *btn = dynamic_cast<TButton *>(Sender);
if (btn->name == "your_button_name"){
// Do action here with button (btn).
}
}
and on .h
void __fastcall TForm1::Test(TObject *Sender);
reference the button either by the tag or name. I usually use a array of buttons that I create dynamically. ALWAYS sanity check your "sender" by casting it. There are other ways to hack info from the object but they are a path to heartache... LOL.

Related

Closing Popup and setting button label

I'm writing a C++ wxWidgets calculator application. I want to compress trigonometric function buttons into a single one to save on space, using what's basically a split button. If you left click on it, the current option is used. If you right click, a popup menu is opened, which contains all the buttons; when you click on one of them, it is used and the big button changes.
I've been suggested to use wxComboBox and other stuff for this job, but I preferred using wxPopupTransientWindow because this way I can display the buttons in a grid, making everything - in my opinion - neater.
Problem is, when I choose an option from the menu, the main button's ID changes (because when I reopen the menu the previously clicked button is light up as its ID and the big button's ID match), but the label does not. Furthermore, the popup is supposed to close itself when you left click on one of the buttons, but it does not.
This is the code for the custom button in the popup which is supposed to do all that stuff:
void expandButton::mouseReleased(wxMouseEvent& evt)
{
if (pressed) {
pressed = false;
paintNow();
wxWindow* mBtn = this->GetParent()->GetParent();
mBtn->SetLabel(this->GetLabel());
mBtn->SetId(this->GetId());
this->GetParent()->Close(true);
}
}
This is the code for the custom button in the main frame which opens up the popup (temporary setup just to test if the whole thing is working):
void ikeButton::rightClick(wxMouseEvent& evt) // CREA PANNELLO ESTENSIONE
{
if (flags & EXPANDABLE)
{
std::vector<expandMenuInfo> buttons;
buttons.push_back(expandMenuInfo(L"sin", 3001));
buttons.push_back(expandMenuInfo(L"cos", 3002));
buttons.push_back(expandMenuInfo(L"tan", 3003));
buttons.push_back(expandMenuInfo(L"arcsin", 3004));
buttons.push_back(expandMenuInfo(L"arccos", 3005));
buttons.push_back(expandMenuInfo(L"arctan", 3006));
wxPoint p = this->GetScreenPosition();
size_t sz = this->GetSize().GetHeight() / 1.15;
expandMenu* menu = new expandMenu(this, buttons, sz, wxPoint(
p.x, p.y + this->GetSize().GetHeight() + 2));
menu->SetPosition(wxPoint(
menu->GetPosition().x - ((menu->GetSize().GetWidth() - this->GetSize().GetWidth()) / 2),
menu->GetPosition().y));
menu->Popup();
}
}
Let me know if I need to show more code.
This is probably a terrible way of doing this, but this is basically the first "serious" application I'm creating using the wxWidgets framework. Any help is appreciated.
when I choose an option from the menu, the main button's ID changes
(because when I reopen the menu the previously clicked button is light
up as its ID and the big button's ID match), but the label does not.
If you're creating the popup menu like in your previous post, you had a popup window with a panel as its child and the buttons were then children of the panel layed out with a sizer.
If that's still the case, this->GetParent() and should be the panel, this->GetParent()->GetParent() should be the popup. So this->GetParent()->GetParent()->GetParent() should be the trig function button (assuming you created the popup with the trig function button as the parent).
So I think the line wxWindow* mBtn = this->GetParent()->GetParent(); should be changed to wxWindow* mBtn = this->GetParent()->GetParent()->GetParent();.
Or slightly shorter wxWindow* mBtn = this->GetGrandParent()->GetParent();;
the popup is supposed to close itself when you left click on one of
the buttons, but it does not.
It looks like wxPopupTransientWindow has a special Dismiss method that is supposed to be used to close it.
So I think the line this->GetParent()->Close(true); should be changed to this->GetGrandParent()->Dismiss(); (Assuming as above that the buttons in the popup are children pf a panel).
Alternately, if you want a solution that will work regardless of the parentage of the controls in the popup window, you could have a utility function to find the popup ancestor which would look something like this:
wxPopupTransientWindow* FindPopup(wxWindow* w)
{
wxPopupTransientWindow* popup = NULL;
while ( w != NULL )
{
popup = wxDynamicCast(w, wxPopupTransientWindow);
if ( popup )
{
break;
}
w = w->GetParent();
}
return popup;
}
This uses the wxDynamicCast function which is slightly different from the c++ dynamic_cast expression. wxDynamicCast uses wxWidgets' RTTI system to check if the given pointer can be converted to the given type.
Then the mouseReleased method could use this utility function something like this:
void expandButton::mouseReleased(wxMouseEvent& evt)
{
if (pressed) {
pressed = false;
paintNow();
wxPopupTransientWindow* popup = FindPopup(this);
if (popup ) {
wxWindow* mBtn = popup->GetParent();
mBtn->SetLabel(this->GetLabel());
mBtn->SetId(this->GetId());
mBtn->Refresh();
popup->Dismiss();
}
}
}
I'm not sure why you're setting trig function button to have a new id, but I assume you have a reason.
To make the SetLabel method work in your custom button class, I think the easist thing is to call the SetLabel() method in the button's constructor. This will store the string passed to the constructor in the button's internal label member.
Based on other questions, I think the ikeButton constructor looks something like this:
ikeButton::ikeButton(wxFrame* parent, wxWindowID id, wxString text,...
{
...
this->text = text;
To store the label, you would need to change the line this->text = text; to
SetLabel(text);
And when you draw the button, I think the method you use looks like this:
void ikeButton::render(wxDC& dc)
{
...
dc.DrawText(text, ...);
}
You would need to change, the line dc.DrawText(text, ...); to
dc.DrawText(GetLabel(), ...);
Likewise, any other references to the button's text member should be changed to GetLabel() instead.
Finally, when you set the label in the expandButton::mouseReleased method, it might be a good idea to call button's Refresh() method to force the button to redraw itself. I added that my suggestion for the mouseReleased method above.

Button that output on TEdit

I'm trying to make a simple calculator with Embarcadero C++Builder. I'm still a novice. How can I extract text from a button? When I press the button, I want to see "3" on a TEdit field (for example). Surely the event is OnClick. But after that, what must I do to redirect this button to TEdit?
As you said, TButton has an OnClick event; if you want to append a certain character to the text of a TEdit:
// Somewhere declared and istantiated:
//TButton *Button1;
//TEdit *Edit1;
//----------------------------------
void __fastcall TForm1::Button1Click(TObject* Sender)
{
Edit1->Text = Edit1->Text + "3";
}

Error when dynamically creating more than one Frame at runtime (C++, FMX, IDE: C++ Builder)

I would like to dynamically create a series of Frame components, then save the pointers into a vector.
I am able to dynamically create TEdit components with no problem, but if I replace TEdit with the name of the frame component (that contains an edit box) the program will error. The first frame will be created, but errors when creating the second one, stating "External exception EEFFACE"
Here is the relevant code. Note that if I replace TFrame2 with TEdit, it works.
class TForm1 : public TForm
{
...
public: // User declarations
std::vector<TFrame2*> fields;
...
};
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TFrame2* temp = new TFrame2 (Layout1);
temp->Parent = Layout1;
temp->Align = TAlignLayout::Top;
fields.push_back(temp);
count++;
}
This is what it looks like after one click.
Error message after 2 clicks.
This is how I want it to look after two clicks.
This is what it looks like when I replace TFrame2 with TEdit, after 3 clicks.
-
EDIT
If I try to hard-code the creation of two Frames, I get the same error on the first click.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TFrame2* temp = new TFrame2 (Layout1);
temp->Parent = Layout1;
temp->Align = TAlignLayout::Top;
fields.push_back(temp);
count++;
TFrame2* temp1 = new TFrame2 (Layout1);
temp1->Parent = Layout1;
temp1->Align = TAlignLayout::Top;
fields.push_back(temp1);
count++;
}
-
EDIT 2
In this post
Can FireMonkey frames be created dynamically?
I see a comment that states
I should note here that it seems the frame objects need to be assigned a unique Name property manually right after creation, at least when using C++, or the next frame object created of the same type will try to take the same name as the first one.
To fix this issue, I needed to set the name of the Frame at runtime. Adding the following code in the Button1 click method fixes the issue.
temp->Name = std::strcat("TFrame2", std::to_string(count).c_str());
This names every new Frame "TFrame2#" where # is the number frame that has been created.

Firemonkey custom control click events firing in designer

I have a custom Firemonkey control that has several sub components. These sub components have OnClick events associated with them that are setup in the control's constructor. I have noticed that when I click on the custom control in my design view, the OnClick events of these sub components are getting fired.
Is there a particular setting or best practice I need to employ to prevent this from happening?
Is there something I can check in my C++ code to see if this event is being run in the designer vs at run time? Something like:
void __fastcall MyControlOnClick( TObject * Sender )
{
if( InDesigner == false )
{
//do stuff here
}
}
Use the ComponentState property. It has a csDesigning flag enabled when your control is being used in the Form Designer.
void __fastcall MyControl::SubControlClick(TObject *Sender)
{
if( !ComponentState.Contains(csDesigning) )
{
//do stuff here
}
}
Alternatively, simply don't assign the OnClick handlers at design-time to begin with:
__fastcall MyControl::MyControl(TComponent *Owner)
: TBaseControl(Owner)
{
...
FSubControl = new TWhatever(this);
if( !ComponentState.Contains(csDesigning) )
FSubControl->OnClick = &SubControlClick;
...
}

How Can Change color of 10 Edit control by single function in c++ builder

i want to change color of edit control when enter edit and when exit from edit, i want do this by single function i dont want to add code for each edit in enter event or on exit event
yes like David and kobik said you just need to create event handlers for OnEnter and OnExit and assign the controls you wish to use them
for example
Add two TEdit's to your form and in the constructor of the form do the following
__fastcall TTestForm::TTestForm(TComponent* Owner)
: TForm(Owner)
{
Edit1->OnEnter = EditEnter;
Edit2->OnEnter = EditEnter;
Edit1->OnExit = EditExit;
Edit2->OnExit = EditExit;
}
Now Create the Enter and Exit event handlers like so
void __fastcall TTestForm::EditEnter(TObject *Sender)
{
TEdit *Temp = (TEdit*)Sender;
Temp->Color = clRed;
}
void __fastcall TTestForm::EditExit(TObject *Sender)
{
TEdit *Temp = (TEdit*)Sender;
Temp->Color = clGreen;
}
Thats it.
Write OnEnter and OnExit event handlers and assign them to each control. Use the Sender parameter to the event to identify which control the event applies to.