I was given a small task to simply make a button’s text toggle “On” and “Off” when you clicked on it – it already starts with the text “Off” as it hasn’t been pressed yet, but when you click it changes to “On”. On each alternating click afterwards, the button’s text would ideally keep changing from “On” to “Off”. In my case I thought a simple boolean variable would be the solution as On and Off can be treated as True or False, but not to be…
Anyway here’s the code for the button’s handler I’ve got so far:
private: System::Void toggleButtonText_Click(System::Object^ sender, System::EventArgs^ e)
{
static bool isOn = true;
if(isOn == false)
{
toggleButtonText->Text = "Off";
}
else
{
toggleButtonText->Text = "On";
}
}
As you can see, the name of the button is “toggleButtonText”. In the InitializeComponent(void) method, this line enables the default text to “Off”:
this->toggleButtonText->Text = L"On";
Looking at the rest of my tasks, getting this right will give me enough clues to attempt them on my own instead of spending ages on endless Google searches.
You have to update your variable state after toggling the text