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