public void BadSectorToolTip()
        {
            int x, y, bsbyte, indexS1 = 0, indexS2;
            int offset = 4;
            int sectorlength;
            int threadid = 0;

            switch ((int)processing.diskformat)
            {
            case 0:
                return;

            case 1:
                offset = 0;
                break;

            case 2:
                offset = 0;
                break;

            case 3:
                offset = 4;
                break;

            case 4:
                offset = 4;
                break;

            case 5:
                offset = 4;
                break;
            }

            if (ECMFMcheckBox.Checked)
            {
                //if( processing.diskformat == DiskFormat.amigados || processing.diskformat == DiskFormat.diskspare || processing.diskformat == )
                if (BadSectorListBox.SelectedIndices.Count == 1)
                {
                    indexS1  = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).id;
                    indexS2  = -1;
                    threadid = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).threadid;
                }
                else if (BadSectorListBox.SelectedIndices.Count >= 2)
                {
                    indexS1  = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).id;
                    indexS2  = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[1]]).id;
                    threadid = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).threadid;
                }
                else
                {
                    return;
                }
                if (processing.sectordata2 == null)
                {
                    return;
                }
                if (processing.sectordata2.Count == 0)
                {
                    return;
                }
                sectorlength        = processing.sectordata2[indexS1].sectorlength;
                BadSectorTooltipPos = BadSectorPanel.PointToClient(Cursor.Position);
                //int f = sectorlength / 512;
                int w = 13;
                int h = 8;
                int lengthmfm;
                switch ((int)processing.diskformat)
                {
                case 0:
                    return;

                case 1:     //AmigaDos
                    offset    = 0;
                    lengthmfm = 8704;
                    break;

                case 2:    //diskspare
                    offset    = 0;
                    lengthmfm = 8320;
                    break;

                case 3:    //pc2m
                    offset    = -704;
                    lengthmfm = 10464;
                    break;

                case 4:    //pcdd
                    offset    = -704;
                    lengthmfm = 10464;
                    break;

                case 5:    //pchd
                    offset    = -704;
                    lengthmfm = 10464;
                    break;
                }
                //if (f == 0.0f) f = 1;
                x      = ((BadSectorTooltipPos.X) / w);
                y      = (int)(BadSectorTooltipPos.Y / h);
                bsbyte = (y * 40 + x);

                //if (bsbyte > sectorlength - 1) return;

                if (BadSectorTooltipPos.X < 350)
                {
                    BadSectorTooltipPos.X += 30;
                }
                else
                {
                    BadSectorTooltipPos.X -= 150;
                }
                int mfmoffset = bsbyte * 8 + offset;
                if (mfmoffset < offset)
                {
                    return;
                }
                int mfmmarkerposition = processing.sectordata2[indexS1].MarkerPositions;
                threadid = processing.sectordata2[indexS1].threadid;
                byte[] mfm = processing.MFM2ByteArray(processing.mfms[threadid], mfmmarkerposition + mfmoffset, 256);
                BadSectorTooltip.Text = " Offset: " + (mfmoffset) + " = " + mfm[0].ToString("X2");;
                BadSectorTooltip.Show();
            }
            else
            {
                //if( processing.diskformat == DiskFormat.amigados || processing.diskformat == DiskFormat.diskspare || processing.diskformat == )
                if (BadSectorListBox.SelectedIndices.Count == 1)
                {
                    indexS1  = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).id;
                    indexS2  = -1;
                    threadid = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).threadid;
                }
                else if (BadSectorListBox.SelectedIndices.Count >= 2)
                {
                    indexS1  = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).id;
                    indexS2  = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[1]]).id;
                    threadid = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).threadid;
                }
                else
                {
                    return;
                }
                if (processing.sectordata2 == null)
                {
                    return;
                }
                if (processing.sectordata2.Count == 0)
                {
                    return;
                }
                sectorlength        = processing.sectordata2[indexS1].sectorlength;
                BadSectorTooltipPos = BadSectorPanel.PointToClient(Cursor.Position);
                int f = sectorlength / 512;
                if (f == 0.0f)
                {
                    f = 1;
                }
                x      = ((BadSectorTooltipPos.X) / 16);
                y      = (int)((BadSectorTooltipPos.Y) / (16 / f));
                bsbyte = y * 32 + x;

                if (bsbyte > sectorlength - 1)
                {
                    return;
                }

                if (BadSectorTooltipPos.X < 350)
                {
                    BadSectorTooltipPos.X += 30;
                }
                else
                {
                    BadSectorTooltipPos.X -= 150;
                }

                //BadSectors[indexS1][i + offset];
                //BadSectorTooltip.Text = "X: " + x + " Y:" + y + " byte: " + bsbyte;
                if (bsbyte >= 0 && bsbyte <= (sectorlength + 6) - 4)
                {
                    BadSectorTooltip.Text = " byte: " + bsbyte + " = " + processing.sectordata2[indexS1].sectorbytes[bsbyte + offset].ToString("X2");
                    BadSectorTooltip.Show();
                }
            }
        }
        public void BadSectorPanelClick()
        {
            int indexS1;//, indexS2;
            int offset = 4;
            int diskoffset;
            int x, y;
            int bsbyte;
            int track, sectornr;
            int datacrc;

            int i;
            int threadid;

            BadSectorTooltipPos = BadSectorPanel.PointToClient(Cursor.Position);

            if (BadSectorListBox.SelectedIndices.Count >= 1)
            {
                indexS1  = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).id;
                threadid = ((badsectorkeyval)BadSectorListBox.Items[BadSectorListBox.SelectedIndices[0]]).threadid;

                int sectorlength = processing.sectordata2[indexS1].sectorlength;
                if (sectorlength < 512)
                {
                    tbreceived.Append("sector length is less than 512 bytes!!");
                    return;
                }

                if (ECMFMcheckBox.Checked)
                {
                    int w = 13;
                    int h = 8;
                    int lengthmfm;
                    int mfmoffset2 = 0;

                    switch ((int)processing.diskformat)
                    {
                    case 0:
                        return;

                    case 1:     //AmigaDos
                        offset    = -48;
                        lengthmfm = 8704;
                        break;

                    case 2:    //diskspare
                        offset    = -16;
                        lengthmfm = 8320;
                        break;

                    case 3:    //pcdd
                        offset     = -712;
                        lengthmfm  = 10464;
                        mfmoffset2 = -712;
                        break;

                    case 4:    //pchd
                        offset     = -712;
                        lengthmfm  = 10464;
                        mfmoffset2 = -712;
                        break;

                    case 5:    //pc2m
                        offset     = -712;
                        lengthmfm  = 10464;
                        mfmoffset2 = -712;
                        break;
                    }
                    //if (f == 0.0f) f = 1;
                    x = ((BadSectorTooltipPos.X) / w);
                    y = (int)(BadSectorTooltipPos.Y / h);
                    //int offset;


                    int mfmoffset = processing.sectordata2[indexS1].MarkerPositions;
                    bsbyte = ((y * 40 + x) * 8) + offset;
                    MFMByteStartUpDown.Value = ((y * 40 + x) * 8) + mfmoffset2;
                    int indexcnt = 0;
                    if (bsbyte > 0)
                    {
                        for (i = 0; i < bsbyte; i++)
                        {
                            if (processing.mfms[processing.sectordata2[indexS1].threadid][i + mfmoffset] == 1)
                            {
                                indexcnt++;
                            }
                        }
                    }
                    else
                    {
                        for (i = bsbyte; i < 0; i++)
                        {
                            if (processing.mfms[processing.sectordata2[indexS1].threadid][i + mfmoffset] == 1)
                            {
                                indexcnt--;
                            }
                        }
                    }
                    tbreceived.Append("index:" + indexcnt + "\r\n");
                    ScatterMinTrackBar.Value = indexcnt;
                    ScatterMaxTrackBar.Value = indexcnt + 14;
                    updateECInterface();
                }
                else
                {
                    int f = sectorlength / 512;

                    x = ((BadSectorTooltipPos.X) / 16);
                    y = (int)((BadSectorTooltipPos.Y) / (16 / f));

                    bsbyte = y * 32 + x;
                    // Temporary decouple event handler
                    byteinsector         = bsbyte;
                    BSEditByteLabel.Text = "Byte: " + bsbyte;

                    // Zoom in scatterplot
                    int indexcnt  = 0;
                    int mfmoffset = processing.sectordata2[indexS1].MarkerPositions;
                    // First find the period index
                    for (i = 0; i < (bsbyte + 4) * 16; i++)
                    {
                        if (processing.mfms[processing.sectordata2[indexS1].threadid][i + mfmoffset] == 1)
                        {
                            indexcnt++;
                        }
                    }
                    tbreceived.Append("index:" + indexcnt + "\r\n");
                    ScatterMinTrackBar.Value = indexcnt;
                    ScatterMaxTrackBar.Value = indexcnt + 14;
                    updateECInterface();
                    if ((int)BluetoRedByteCopyToolBtn.Tag == 1)
                    {
                        // Copy single byte from BadSectors to disk array
                        if (BSBlueSectormapRadio.Checked)
                        {
                            textBoxReceived.AppendText("Copy byte to disk array.");
                            track    = processing.sectordata2[indexS1].track;
                            sectornr = processing.sectordata2[indexS1].sector;
                            datacrc  = processing.sectordata2[indexS1].crc;

                            processing.sectorspertrack = 9;

                            //(tracknr * processing.sectorspertrack * 512 * 2) + (headnr * processing.sectorspertrack * 512) + (sectornr * 512);
                            diskoffset = track * processing.sectorspertrack * 512 + sectornr * 512;
                            processing.disk[diskoffset] = processing.sectordata2[indexS1].sectorbytes[bsbyte + offset];
                        }

                        //Copy byte from BadSectors to TempSector
                        if (BlueTempRadio.Checked)
                        {
                            textBoxReceived.AppendText("Copy byte to Temp.");
                            track    = processing.sectordata2[indexS1].track;
                            sectornr = processing.sectordata2[indexS1].sector;
                            datacrc  = processing.sectordata2[indexS1].crc;

                            processing.sectorspertrack = 9;

                            //(tracknr * processing.sectorspertrack * 512 * 2) + (headnr * processing.sectorspertrack * 512) + (sectornr * 512);
                            diskoffset = track * processing.sectorspertrack * 512 + sectornr * 512;
                            TempSector[bsbyte + offset] = processing.sectordata2[indexS1].sectorbytes[bsbyte + offset];
                        }

                        //Check crc
                        ushort     datacrcchk;
                        Crc16Ccitt crc = new Crc16Ccitt(InitialCrcValue.NonZero1);
                        datacrcchk             = crc.ComputeChecksum(TempSector);
                        BlueCrcCheckLabel.Text = "Crc: " + datacrcchk.ToString("X2");

                        processing.sectordata2[indexS1].crc = datacrcchk;
                    }
                }
            }
        }