0

I am currently working on a windows mobile 6.5 application. The application has a bluetooth function which reads data from serial port. Once the data has been read, event handler will pass data from lower level class to the UI view. The UI would update the value and change the backcolor of the button depending on the value read from serial port. However, sometimes, bluetooth device can be out of range. In other words, the handheld device can not receive any data from the remote bluetooth device.

When the device is in range and the handheld device can receive data from serial port, received value and the backcolor of the button will change in UI accordingly based on the received value. However, after the device comes in range again after out of range, the event handler doesn't work for some reason, therefore, only the value changes, the backcolor of button cannot change via the event handler.

If I click on that button, the color will change again. It looks like the color will only change if focus is on the button. I can confirm that the issue relating to the event handler because i printed the deviceName on the label, the event handler doesn't fire. Can anyone know why it occurs? or any better idea to approach.

Here is the code I use for keeping listening to the backcolor change of the button and its event handler:

 Button[] gauges = new Button[MonitoringGauges.Count()]; // declare the button variable

    // Create a button for each gauge
    .....       
    .....
    .....

    // Constructor

            for(int k = MonitoringGauges.count -1 ; k >=0 ; k--){  // keep listening to the backcolor change for the button corresponding to the gauge
              if(MonitoringGauges[k] !=null){
                   MonitoringGauges[k].TrainingZoneChanged += new Gauge.TrainingZoneChangedEventHandler(x_TrainingZoneChanged);
               }
            }

        // event handler

        void x_TrainingZoneChanged(string deviceName, string macAddress, Color color){
           if(!string.IsNullOrEmpty(deviceName) && !string.IsNullOrEmpty(macAddress) && color !=null){
             Button btn = gauges.Where(x =>x.Name.equals(deviceName)).First(); // find the correct button for updating the backcolor of the button

        if(btn !=null){
            btn.Invoke((Action) delegate
             {
                 if(color == Color.Black){
                        btn.BackColor = Color.LightBlue;
                 } else{
                        btn.BackColor = color;
                  }

             });
        }
        }
        }

Thanks for any helps.

Regards,

SW Lau

Charles LAU
  • 281
  • 1
  • 8
  • 19
  • Are you saying the eventhandler isn't firing, or are you saying that the call to btn.BackColor doesn't have any noticeable effect? – ctacke Jun 28 '12 at 16:50
  • The backcolor doesn't have noticeable effect after the device comes in range again after out of range – Charles LAU Jun 28 '12 at 21:20
  • So you put a break point on that line and you verified that the line of code is executing, correct? – ctacke Jun 28 '12 at 21:24
  • Hi cracke, If I click on one of the buttons in UI, the backcolor of that button will change again. It looks like the color only works when the button is on focus after device comes in range again. – Charles LAU Jun 28 '12 at 21:53
  • So the question really has nothing to do with backcolor, but the fact that the evenhandler isn't firing? Please update the question to clarify this. – ctacke Jun 28 '12 at 21:55

1 Answers1

0

Did you already try a simple btn.Refresh() to enforce a redraw? Possibly followed by an Application.DoEvents();

josef
  • 5,951
  • 1
  • 13
  • 24
  • Hi, josef, I have tried btn.Refresh() itself, no any success. Do you mean add Application.Doevents() after btn.Refresh(); – Charles LAU Jun 29 '12 at 17:01
  • Hi, would the reason be after out of range, the UI freezes a bit of time and then come back again, so that the event handler doesn't work? – Charles LAU Jun 29 '12 at 17:28
  • Hi yes, I mean adding a Application.DoEvents() after the btn.Refresh(). Is your event handler color code ever been called? Did you put a breakpoint in the first line of the event handler ("if(!string.IsNullOrEmpty(deviceName) &&...)? I the event handler fired by a background thread? – josef Jul 01 '12 at 05:01
  • The event handler is called before the out of range occurs. After out of range, the particular button corresponding to specific gauge who has been out of range and came in range again, the color won't change. It only starts to change again after I click on the button. Sound like it is the focus. – Charles LAU Jul 01 '12 at 13:51
  • Sounds very strange. Can you put a breakpoint in the eventhandler and check when it is called and if it is routed to your button function? – josef Jul 02 '12 at 17:57
  • The event handler of the button corresponding to the particular gauge who has been out of range doesn't fire. Only it fires again when I click on the button. But when it comes out of range, the UI freeze a bit of time, would this cause the event handler won't work? Because i found that if the gauge has been out of range at anytime, its color change will have a issue. – Charles LAU Jul 02 '12 at 23:24
  • Possibly you can post the code of MonitoringGauges class, especially the event/delegate stuff. Further, if the UI freezes for some time, the code for checking the in/out of range function would be of interest. It should run on a separate thread and not in the GUI thread. – josef Jul 04 '12 at 03:01