I have two listboxes titled lstResult and lstResult2. lstResult clears with no problem using lstResult.Items.Clear() but when i use that for lstResult2 it dissapears but when i put in another value, the value(s) that i deleted come back. Also how do i fix the total to reflect the changes. Thank you in advance!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
namespace Perfume_Formula_Typing_Software
{
public partial class Form1 : Form
{
List<double> values;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
values = new List<double>();
}
void ShowValues()
{
lstResult2.Items.Clear();
for (int i = 0; i < values.Count; i++)
lstResult2.Items.Add(values[i]);
lblSum.Text = values.Count.ToString();
}
private void btnAdd_Click(object sender, EventArgs e)
{
double value = 0.00;
double sum = 0.00;
if (txtItemName.Text == "" || txtItemValue.Text == "")
{
MessageBox.Show("Please Enter A Value In All Fields.", "Missing Information");
return;
}
else
{
lstResult.Items.Add(txtItemName.Text);
txtItemName.Text = "";
try
{
value = double.Parse(txtItemValue.Text);
values.Add(value);
ShowValues();
txtItemValue.Text = "";
txtItemName.Focus();
}
catch (FormatException)
{
}
for (int i = 0; i < values.Count; i++)
sum += values[i];
lblSum.Text = sum.ToString("F");
}
}
private void mnuFileNew_Click(object sender, EventArgs e)[/COLOR="red"]
{
txtFormulaName.Text = "";
txtFormulaNumber.Text = "";
txtItemName.Text = "";
comboBox1.Text = "";
lstResult.Items.Clear();
lstResult2.Items.Clear();
}
private void mnuFileOpen_Click(object sender, EventArgs e)
{
dlgOpen.InitialDirectory = @"C:\";
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
FileStream fileAndorHunsFormulaWorksheet = new FileStream(this.dlgOpen.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader bnrAndorHunsFormulaWorksheet = new BinaryReader(fileAndorHunsFormulaWorksheet);
lblSum.Text = bnrAndorHunsFormulaWorksheet.ReadString();
txtFormulaName.Text = bnrAndorHunsFormulaWorksheet.ReadString();
string item = bnrAndorHunsFormulaWorksheet.ReadString();
int index = comboBox1.FindString(item);
if ((index != -1 & !string.IsNullOrEmpty(item)))
{
comboBox1.SelectedIndex = index;
}
txtFormulaNumber.Text = bnrAndorHunsFormulaWorksheet.ReadString();
int number = lstResult2.Items.Count;
lstResult2.Items.Clear();
lstResult.Items.Clear();
bool switchAction = false;
while (bnrAndorHunsFormulaWorksheet.PeekChar() != -1)
{
string str = bnrAndorHunsFormulaWorksheet.ReadString();
if (str == "@")
{
switchAction = true;
continue;
}
if (switchAction)
lstResult.Items.Add(str);
else
lstResult2.Items.Add(str);
}
bnrAndorHunsFormulaWorksheet.Close();
fileAndorHunsFormulaWorksheet.Close();
}
}
private void mnuFileSave_Click(object sender, EventArgs e)
{
if (dlgSave.ShowDialog() == DialogResult.OK)
{
FileStream fileAndorHunsFormulaWorksheet = new FileStream(dlgSave.FileName, FileMode.Create, FileAccess.Write, FileShare.Write);
BinaryWriter bnrAndorHunsFormulaWorksheet = new BinaryWriter(fileAndorHunsFormulaWorksheet);
bnrAndorHunsFormulaWorksheet.Write(lblSum.Text);
bnrAndorHunsFormulaWorksheet.Write(txtFormulaName.Text);
bnrAndorHunsFormulaWorksheet.Write(comboBox1.Text);
bnrAndorHunsFormulaWorksheet.Write(txtFormulaNumber.Text);
foreach (object ls in lstResult2.Items)
{
bnrAndorHunsFormulaWorksheet.Write(ls.ToString());
}
bnrAndorHunsFormulaWorksheet.Write("@"); // Marker
foreach (object ls in lstResult.Items)
{
bnrAndorHunsFormulaWorksheet.Write(ls.ToString());
}
bnrAndorHunsFormulaWorksheet.Close();
fileAndorHunsFormulaWorksheet.Close();
}
}
private void btnRemoveValue_Click(object sender, EventArgs e)
{
lstResult2.Items.Remove(lstResult2.SelectedItem);
}
private void btnRemoveItem_Click(object sender, EventArgs e)
{
lstResult.Items.Remove(lstResult.SelectedItem);
}
}
}