private void btnSummary_Click(object sender, EventArgs e) { Application.OpenForms.OfType <Form>().Where(x => x.Name != "Main").ToList().ForEach(x => x.Close()); frmSummary summary = new frmSummary(); summary.TopLevel = false; pnlContent.Controls.Add(summary); summary.Dock = DockStyle.Fill; summary.Show(); btnHome.Enabled = true; btnForm.Enabled = true; btnSummary.Enabled = false; btnSettings.Enabled = true; }
// Adds this transaction private void btnAdd_Click(object sender, EventArgs e) { // init int catID = 0; int srcID = 0; int amount = 0; // this represents the tally arr before this transaction List <object[]> objList = Global_Variable.tally_arr; // does this transaction require a new entry in the global tally list? bool savedToGlobal = false; // the array with the details of the current transaction // category | source | amount object[] entry = new object[3]; // if there is nothing in the tally arr, this current array will need to be added. //if (Global_Variable.tally_arr.Count==0) //{ // needNew = true; //} // create Transaction object for data of this transaction. Transaction tran = new Transaction(); // Validation bool isValid = true; // is the money spent or earned? if (rbEarned.Checked) { tran.IsCredit = true; } else if (rbSpent.Checked) { tran.IsCredit = false; } else // invalid if nothing selected { MessageBox.Show("Please select whether you have earned or spent this money."); isValid = false; } // if something has been selected in the category combobox if (cbCategory.SelectedIndex >= 0) { // +1 cb index starts at 0, databsae starts at 1 tran.CategoryID = int.Parse(cbCategory.SelectedIndex.ToString()) + 1; } else { MessageBox.Show("Please assign the budget category that this money represents."); isValid = false; } if (cbAccount.SelectedIndex >= 0) { tran.SourceID = int.Parse(cbAccount.SelectedIndex.ToString()) + 1; } else { MessageBox.Show("Please assign the source that this money has come from/arrived at."); isValid = false; } if (double.TryParse(txtAmount.Text, out double result)) { if (tran.IsCredit) { tran.Amount = result; } else { tran.Amount = result * -1; } } else { MessageBox.Show("Please enter the $amount of this transaction."); isValid = false; } if (isValid) { tran.TransDate = DateTime.Now; if (Global_Variable.currentPeriod == 0) { // first transaction, create first period upon first valid entry. UpdatePeriod(); Global_Variable.currentPeriod = 1; tran.Period = 1; } else { tran.Period = Global_Variable.currentPeriod; } // are there any entries in the global array already? if (objList.Count == 0) // if empty, add this as a new list item. { entry[0] = tran.CategoryID; entry[1] = tran.SourceID; entry[2] = tran.Amount; Global_Variable.tally_arr.Add(entry); savedToGlobal = true; SaveTransaction(tran); } else // otherwise check through list items to see if this category has been added before. { // check through all the existing arrays in the global variable for (int i = 0; objList.Count > i; i++) { // if there is another entry in the same category, I need to update // with the new figure, but otherwise I add a new list item array. if ((int)(objList[i][0]) == tran.CategoryID) { // i = objList.Count + 1; if ((int)(objList[i][1]) == tran.SourceID) { double tot; double curr = double.Parse(objList[i][2].ToString()); tot = curr + tran.Amount; Global_Variable.tally_arr[i][2] = tot; savedToGlobal = true; SaveTransaction(tran); } } } // if no match was found in both category & source, create new. if (!savedToGlobal) { entry[0] = tran.CategoryID; entry[1] = tran.SourceID; entry[2] = tran.Amount; Global_Variable.tally_arr.Add(entry); savedToGlobal = true; SaveTransaction(tran); } } Global_Variable.transact = tran; frmSummary sum = new frmSummary(); sum.Show(); } else { MessageBox.Show("Fix the errors in this transaction and try again."); } // and will later do business. }