Button image is not updating to new image when Serial Port received new data
-
I am trying to set the image of a button when my Serial Port receives data. After I receive the data, I use an Invoke to call MyForm::ProcessReceived, then from the MyForm::ProcessReceived to setAppearance. My setAppearance function is called, however the image on button is still showing the default image. How do I show the new image in button?
System::Void MyForm::SerialPort\_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) { uint16\_t test = 0; test = mySerialPort->BytesToRead; this->Invoke(gcnew EventHandler(this, &MyForm::ProcessReceived)); } System::Void MyForm::ProcessReceived(System::Object^ sender, System::EventArgs^ e) { // my\_ReceivedData is just a custom class to parse & store received data my\_ReceivedData^ status = gcnew my\_ReceivedData; mySerialPort->Read(status->bytes, 0, length); mySerialPort->DiscardInBuffer(); setAppearance(status->bytes); } public: System::Void setAppearance(array^ bytes) { btn\_Start->ImageIndex = 3; // function is excecuted, but image is still not updated from it's default }
Update (Problem solved): It turned out that the buttons were disabled so the background images couldnt be updated. After enabling the buttons, everything worked accordingly!
-
I am trying to set the image of a button when my Serial Port receives data. After I receive the data, I use an Invoke to call MyForm::ProcessReceived, then from the MyForm::ProcessReceived to setAppearance. My setAppearance function is called, however the image on button is still showing the default image. How do I show the new image in button?
System::Void MyForm::SerialPort\_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) { uint16\_t test = 0; test = mySerialPort->BytesToRead; this->Invoke(gcnew EventHandler(this, &MyForm::ProcessReceived)); } System::Void MyForm::ProcessReceived(System::Object^ sender, System::EventArgs^ e) { // my\_ReceivedData is just a custom class to parse & store received data my\_ReceivedData^ status = gcnew my\_ReceivedData; mySerialPort->Read(status->bytes, 0, length); mySerialPort->DiscardInBuffer(); setAppearance(status->bytes); } public: System::Void setAppearance(array^ bytes) { btn\_Start->ImageIndex = 3; // function is excecuted, but image is still not updated from it's default }
Update (Problem solved): It turned out that the buttons were disabled so the background images couldnt be updated. After enabling the buttons, everything worked accordingly!
Caveat: I'm not a C++ programmer so any syntax below may be incorrect. The objects, properties and method signatures look like Windows Forms in .Net. If so, try ...
public: System::Void setAppearance(array^ bytes) {
btn_Start->ImageIndex = 3; // function is excecuted, but image is still not updated from it's default
btn_Start->Refresh();
} -
Caveat: I'm not a C++ programmer so any syntax below may be incorrect. The objects, properties and method signatures look like Windows Forms in .Net. If so, try ...
public: System::Void setAppearance(array^ bytes) {
btn_Start->ImageIndex = 3; // function is excecuted, but image is still not updated from it's default
btn_Start->Refresh();
}Hello, I seen that this Refresh() should force the button to redraw itself, and I tested but it still didn't work. I then guessed that the button was disabled and I was right, because the button is disabled it couldn't show the new background image. Thanks for your help!
-
Hello, I seen that this Refresh() should force the button to redraw itself, and I tested but it still didn't work. I then guessed that the button was disabled and I was right, because the button is disabled it couldn't show the new background image. Thanks for your help!
Did you try enabling, refreshing, then disabling so that the refresh happens whilst it is enabled but the chances of someone clicking it in the brief time it is enabled is slim? If you wanted to eliminate its clickablity whilst it is briefly disabled, you could change the click action to do nothing before enabling it and then restoring the normal click action after disabling it. Not tried any of this myself, so I am just guessing but it is worth a go. It may be that the disabled visual element overrides the selected background image.
-
Did you try enabling, refreshing, then disabling so that the refresh happens whilst it is enabled but the chances of someone clicking it in the brief time it is enabled is slim? If you wanted to eliminate its clickablity whilst it is briefly disabled, you could change the click action to do nothing before enabling it and then restoring the normal click action after disabling it. Not tried any of this myself, so I am just guessing but it is worth a go. It may be that the disabled visual element overrides the selected background image.
Hello jsc42, yes I have done this part to prevent the user from ever mistakenly clicking the button :) . Thanks for the suggestion!