//Add New Item button clicked. Checks all the fields for valid values, //and if no errors, creates the record and adds it to the db. private void btn_Add_AddItem_Click(object sender, EventArgs e) { if (GuiPrefs.OpenDBPath == "") { MessageBox.Show("You must load a database before you can add items.", "No Database Loaded", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Add the item if all the fields have valid values if (Add_Controls_Valid()) { SpecsRecord nr = new SpecsRecord(); int skunew = 1; int skumin, skumax; //Step 1 - Find the first unused SKU in the database try { tb_Add_MinSKU.Text = (skumin = Math.Abs(Convert.ToInt16(tb_Add_MinSKU.Text))).ToString(); } catch { //If not a valid integer, set min to 1 skumin = 1; tb_Add_MinSKU.Text = ""; } try { tb_Add_MaxSKU.Text = (skumax = Math.Abs(Convert.ToInt16(tb_Add_MaxSKU.Text))).ToString(); } catch { //If not a valid integer, set max to -1 (function will read as no limit) skumax = -1; } if (skumax < skumin) { //Blanks max if it's less than min tb_Add_MaxSKU.Text = ""; } try { skunew = Mydb.GetNextFreeSKU(skumin, skumax); } catch (IndexOutOfRangeException) { MessageBox.Show("There is no available SKU number in the specified range.", "No available SKU", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // } // else { //No valid range specified, search entire inventory // skunew = Mydb.GetNextFreeSKU(); // } //Step 2 - Set all the values of the SpecsRecord nr.SKU = (uint)skunew; nr.SphereOD = Convert.ToSingle(tb_Add_SphereOD.Text); nr.SphereOS = Convert.ToSingle(tb_Add_SphereOS.Text); nr.CylOD = Convert.ToSingle(tb_Add_CylOD.Text); nr.CylOS = Convert.ToSingle(tb_Add_CylOS.Text); nr.AxisOD = Convert.ToInt16(tb_Add_AxisOD.Text); nr.AxisOS = Convert.ToInt16(tb_Add_AxisOS.Text); nr.AddOD = Convert.ToSingle(tb_Add_AddOD.Text); nr.AddOS = Convert.ToSingle(tb_Add_AddOS.Text); if (rb_Add_Male.Checked) { nr.Gender = SpecGender.Male; } else if (rb_Add_Female.Checked) { nr.Gender = SpecGender.Female; } else { nr.Gender = SpecGender.Uni; } if (rb_Add_Single.Checked) { nr.Type = SpecType.Single; } else { nr.Type = SpecType.Multi; } if (rb_Add_NoTint.Checked) { nr.Tint = SpecTint.None; } else if (rb_Add_LightTint.Checked) { nr.Tint = SpecTint.Light; } else { nr.Tint = SpecTint.Dark; } if (rb_Add_Small.Checked) { nr.Size = SpecSize.Small; } else if (rb_Add_Medium.Checked) { nr.Size = SpecSize.Medium; } else if (rb_Add_Large.Checked) { nr.Size = SpecSize.Large; } else { nr.Size = SpecSize.Child; } nr.Comment = tb_Add_Comment.Text; nr.DateAdded = String.Format("{0:yyyy-MM-dd}", DateTime.Now); // Step 2.5 - Convert to negative CYL representation if (nr.CylOD > 0) { nr.SphereOD += nr.CylOD; nr.CylOD *= -1; nr.AxisOD = (nr.AxisOD + 90) % 180; } if (nr.CylOS > 0) { nr.SphereOS += nr.CylOS; nr.CylOS *= -1; nr.AxisOS = (nr.AxisOS + 90) % 180; } //Step 3 - Add the new SpecsRecord to the database Mydb.Insert(nr, SSTable.Current); Mydb.GetCurrentInventory(); //Refresh the displayed inventory IncrementOps(); //Increment the number of ops by 1 if the appropriate prefs are set //If the database is not normal (that is, if it is a merge database), also add the record to the //merge table and refresh it for display if (!GuiPrefs.NormalDatabase) { Mydb.Insert(nr, SSTable.MergeItems); dt_Add_MergeTable.Clear(); Mydb.GetTable(dt_Add_MergeTable, SSTable.MergeItems); } //Step 4 - Clear the entry fields and refresh the inventory Add_Clear_Controls(); //Step 5 - Highlight the new item in the DataGridView for (int j = 0; j < dgv_Add_InventoryView.Rows.Count; j++) { try { if (Convert.ToInt16(dgv_Add_InventoryView[0, j].Value) == skunew) { dgv_Add_InventoryView.Rows[j].Selected = true; if (j < 3) { dgv_Add_InventoryView.FirstDisplayedScrollingRowIndex = 0; } else { dgv_Add_InventoryView.FirstDisplayedScrollingRowIndex = j - 2; } } } catch { } } } }