0

I am trying to change the image of a label on a status strip very rapidly and many many times. I am receiving a ArgumentOutOfRangeException. I have put catches in the paint events of both the label and the status strip as well as the OnPaint override, but I have failed to catch it. The exception is always accompanied by a red cross in place of the status strip.

This is the code that changes the image. It is called every time data is received from a serial connection. The goal is to blink a light as data is received. The image is 16px by 16px.

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    try
    {
        if (ss_RD.Image != SerialScripter.Properties.Resources.GreenOrb)
            ss_RD.Image = SerialScripter.Properties.Resources.GreenOrb;

         SerialPort sp = (SerialPort)sender;
         string indata;

         int bytes = serialConnection.BytesToRead;
         switch (tBtn_receiveAsHex.Checked)
         {
             case false:
                 char[] charBuffer = new char[bytes];
                 serialConnection.Read(charBuffer, 0, bytes);
                 indata = CharToAscii(charBuffer);
                 break;
             case true:
                 byte[] byteBuffer = new byte[bytes];
                 serialConnection.Read(byteBuffer, 0, bytes);
                 indata = ByteToHex(byteBuffer);
                 break;
             default:
                 indata = sp.ReadExisting();
                 break;
        }

        this.BeginInvoke(new SetTextDeleg(DisplayToUI), new object[] { indata });

        if (ss_RD.Image != SerialScripter.Properties.Resources.RedOrb)
            ss_RD.Image = SerialScripter.Properties.Resources.RedOrb;
    }
    catch (Exception ex)
    {
        ss_status.Text = ex.Message;
    }
}

********* Exception Text **************
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
   at System.Collections.ArrayList.get_Item(Int32 index)
   at System.Windows.Forms.ToolStripItemCollection.get_Item(Int32 index)
   at System.Windows.Forms.ToolStrip.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.StatusStrip.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

0 Answers0