Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Button image is not updating to new image when Serial Port received new data

Button image is not updating to new image when Serial Port received new data

Scheduled Pinned Locked Moved Managed C++/CLI
questiondata-structureshelpannouncement
5 Posts 2 Posters 16 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yehiga1467
    wrote on last edited by
    #1

    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!

    J 1 Reply Last reply
    0
    • Y yehiga1467

      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!

      J Offline
      J Offline
      jsc42
      wrote on last edited by
      #2

      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();
      }

      Y 1 Reply Last reply
      0
      • J jsc42

        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();
        }

        Y Offline
        Y Offline
        yehiga1467
        wrote on last edited by
        #3

        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!

        J 1 Reply Last reply
        0
        • Y yehiga1467

          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!

          J Offline
          J Offline
          jsc42
          wrote on last edited by
          #4

          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.

          Y 1 Reply Last reply
          0
          • J jsc42

            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.

            Y Offline
            Y Offline
            yehiga1467
            wrote on last edited by
            #5

            Hello jsc42, yes I have done this part to prevent the user from ever mistakenly clicking the button :) . Thanks for the suggestion!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups