PKHeX.SAV_Inventory.saveBag C# (CSharp) Method

saveBag() private method

private saveBag ( object sender ) : void
sender object
return void
        private void saveBag(object sender)
        {
            int offset = 0;
            if (B_DisplayItems.ForeColor == Color.Red)
            {
                offset = bagoffsets[0] + shiftval;
            }
            else if (B_DisplayKeyItems.ForeColor == Color.Red)
            {
                offset = bagoffsets[1] + shiftval;
            }
            else if (B_DisplayTMHM.ForeColor == Color.Red)
            {
                offset = bagoffsets[2] + shiftval;
            }
            else if (B_DisplayMedicine.ForeColor == Color.Red)
            {
                offset = bagoffsets[3] + shiftval;
            }
            else if (B_DisplayBerries.ForeColor == Color.Red)
            {
                offset = bagoffsets[4] + shiftval;
            }

            // Fetch Data
            int itemcount = dataGridView1.Rows.Count;
            int emptyslots = 0;
            for (int i = 0; i < itemcount; i++)
            {
                string item = dataGridView1.Rows[i].Cells[0].Value.ToString();
                int itemindex = Array.IndexOf(Form1.itemlist, item);
                int itemcnt;
                try 
                { itemcnt = Convert.ToUInt16(dataGridView1.Rows[i].Cells[1].Value.ToString()); }
                catch { itemcnt = 0; }

                if (itemindex == 0) // Compression of Empty Slots
                {
                    emptyslots++;
                    continue;
                }
                if (itemcnt == 0)
                    itemcnt++; // No 0 count of items
                else if (itemcnt > 995)
                    itemcnt = 995;

                // Write Data into Save File
                Array.Copy(BitConverter.GetBytes(itemindex), 0, sav, offset + 4 * (i - emptyslots), 2); // item #
                Array.Copy(BitConverter.GetBytes(itemcnt), 0, sav, offset + 4 * (i - emptyslots) + 2, 2); // count
            }

            // Delete Empty Trash
            for (int i = itemcount - emptyslots; i < itemcount; i++)
            {
                Array.Copy(BitConverter.GetBytes(0), 0, sav, offset + 4 * i + 0, 2); // item #
                Array.Copy(BitConverter.GetBytes(0), 0, sav, offset + 4 * i + 2, 2); // count
            }

            // Load New Button Color, after finished we'll load the new data.
            Button btn = sender as Button;
            B_DisplayItems.ForeColor =
            B_DisplayKeyItems.ForeColor =
            B_DisplayTMHM.ForeColor =
            B_DisplayMedicine.ForeColor =
            B_DisplayBerries.ForeColor = Form1.defaultControlText;

            btn.ForeColor = Color.Red;
        }
        private void giveAll(string[] inarray, int count)