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);
        }

       
    }
}

Edited 5 Years Ago by steven8579: n/a

Hi!

You populate value of lstResult2 here: (Read comment within code)

void ShowValues()
        {
            lstResult2.Items.Clear();   ' this clears the previous value
            for (int i = 0; i < values.Count; i++)
                lstResult2.Items.Add(values[i]);
            lblSum.Text = values.Count.ToString();
        }

Edited 5 Years Ago by ShahanDev: n/a

hi thanks for the reply the items.clear method clears the whole listbox. i just want to remove one line thats why i used

lstResult2.Items.Remove(lstResult2.SelectedItem);

I just dont know why the value that i deleted keeps coming back when i enter a new value. When i enter the new value, that value and the deleted value appear.

Yes the above expert is right the value don't remove from here:

List<double> values;

In this part:

try
                {
                    value = double.Parse(txtItemValue.Text);
[B]                    values.Add(value);[/B]
                    ShowValues();
                    txtItemValue.Text = "";
                    txtItemName.Focus();
                }

you always "add", you do not delete from "values" anywhere in the rest of code.

im sorry but i dont really understand what you are saying can you show me an example or try explaining it differently

Ok. No problem. Keeping your all code as it is:

Execute one time with this code: (lstResult2 will retain previous value(s))

void ShowValues()
        {
            lstResult2.Items.Clear(); here previous values clears from "ListBox"
            [B]//[/B]values.Clear();  ' here previous values [B]Do not[/B] clears from "Collection"
            for (int i = 0; i < values.Count; i++)
                lstResult2.Items.Add(values[i]);
            lblSum.Text = values.Count.ToString();
        }

and one time with this: (lstResult2 will remove previous value(s) and keep the last added "txtItemValue.Text" value)

void ShowValues()
        {
            lstResult2.Items.Clear();    'here previous values clears from "ListBox"
            values.Clear();    ' here previous values clears from "Collection"
            for (int i = 0; i < values.Count; i++)
                lstResult2.Items.Add(values[i]);
            lblSum.Text = values.Count.ToString();
        }

Hope this helps!!!

ok im sorry but i still dont understand where im supposed to put this code. when i put it under the button event handler it says that it already contains a definition for ShowValues()

i understand that but where am i supposed to put it because if i put in that code it says that ShowValues() already contains a definition. Also is this for clearing the entire listbox or just one item from the listbox.

Okay first tell me, you want to remove the last item of "lstResult2" or first item of "lstResult2" or just the selected one ???

Note: In case of selected one I don;t think so it is possible because your ShowValue() fires only when item adds.

Hi!

I need clarificatino on this: (please answer in the same order)

1) When two button to remove "item" and "value" from "lstResult" and "lstresult2" respectively. Is it correct ???

2) What should happen when "add" btn presses ???

If I bothered you, my apologize.

>im trying to delete the selected item.

if (lstresult2.SelectedIndex != -1)
      lstresult2.Items.RemoveAt(lstresult2.SelectedIndex);

@steven8579

Where you need to remove selected item or selected value you can write:

To remove from "lstResult selected item":

btnRemoveItem_Click(new string("ShowValues".ToCharArray()), new EventArgs());
            values.Remove((lstResult2.SelectedItem != null) ? (double)lstResult2.SelectedItem : 0.0);

Edited 5 Years Ago by ShahanDev: n/a

@ShahanDev

1)Yes there are two buttons to remove "item" and "value" from "lstResult" and "lstresult2" respectively

2) When you press the add button its supposed to take the name entered in the textbox "Item Name" and Item Value and put them in lstResult and lstResult2 Respectively. When the Item Value gets added to lstResult2 the sum is automatically calculated.

Hi!

Is this solves your Problem ???

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)
        {
            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)
        {
[B]            values.Remove(Convert.ToDouble(lstResult2.SelectedItem)); [/B]
            lstResult2.Items.Remove(lstResult2.SelectedItem);
        }

        private void btnRemoveItem_Click(object sender, EventArgs e)
        {
            lstResult.Items.Remove(lstResult.SelectedItem);
        }
//get the selected item:
string value  = listBox1.SelectedItem.ToString();
//delete it:
listBox1.Items.Remove(value);

there is another way to get the selecte index (thats an integer) and then remove with using method "RemoveAt", like:

int index = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(index);
Comments
You got a point here.

ok i used this code and it Subtracts the values when i remove them but when i add a new value i get the value i typed in and (negative) value that i deleted). How do i get rid of this.

private void btnRemoveValue_Click(object sender, EventArgs e)
         {
             double value = 0.00;
             double sum = 0.00;
             double x = Convert.ToDouble(lstResult2.SelectedItem);

             values.Add(value - x);
              

             lstResult2.Items.Remove(lstResult2.SelectedItem);
            
             
             
             txtItemName.Focus();


             for (int i = 0; i < values.Count; i++)
                 sum += values[i];
             lblSum.Text = sum.ToString("F");


         }

Hi!

To delete the selected value from "lstResult2" and subtract it from the "sum" as well, here is the code:

Here I have provided only btnRemoveValue code.

private void btnRemoveValue_Click(object sender, EventArgs e)
        {
            values.Remove(Convert.ToDouble(lstResult2.SelectedItem));
            lblSum.Text = (Convert.ToDouble(lblSum.Text) - Convert.ToDouble(lstResult2.SelectedItem)).ToString(); 
            lstResult2.Items.Remove(lstResult2.SelectedItem);
        }

Is this helpful ???

Yes thank you so much! I really appreciate. I have one last question though. How do i get a single scroll bar to scroll both listboxes at the same time?

that doesnt really give me exactly what i need. i want to code this without using custom content.

Hi!

You need customization to scroll listbox from some external control. Since there is no appropriate event that you could use to compel the ListBox to scroll. So you should follow the suggestion of other expert.

Edited 5 Years Ago by ShahanDev: n/a

This article has been dead for over six months. Start a new discussion instead.