hello every one ;
Q. how we can perform addition of integers in list box ?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tanmay SarkarRank #1,407 · 1.2K reputationPosted Aug 5, 2010, 9:27 AM EDT
please clear it.
What you want to do?
You have 2 value in a list & want to add & show it on that list box?
or you have 2 list box chose integer & want to add.
or you want anything else.
Please clear it first. :)
Sam HobbsRank #107 · 20.5K reputationPosted Aug 5, 2010, 1:44 PM EDT
theLizardRank #1,079 · 1.8K reputationPosted Aug 5, 2010, 7:27 PM EDT
int total = 0;
for(int i = 0; i < ListBox.Items.Count; i ++)
{
total += TryInt.Parse(ListBox->Items[i].value);
}
you may have to do error checking to make sure that the list box value can be converted to an integer.
I have not tried this but it is basically one correct way of doing it.
sumaira manzoor hussain khanRank #8,900 · 214 reputationPosted Aug 5, 2010, 8:45 PM EDT
my requirement is :
i added many integers in checkedlist box through this code :
code of add integers in list box :
this.checkedListBox1.Items.Add("3");
this.checkedListBox1.Items.Add("85");
this.checkedListBox1.Items.Add("3500");
after this code i used listbox in my form .after this i selected integers like 3 , 3500 etc randomly and move in second list box. my desire integers will be moved from checkedlistbox to listbox. so, the desire integers has been come in list box. i want to perform addition between these integers that are exit in my list box.
theLizardRank #1,079 · 1.8K reputationPosted Aug 5, 2010, 8:53 PM EDT
if you want to total the integers moved to the second list box 2 then this total += TryInt.Parse(ListBox->Items[i].value); becomes this total += TryInt.Parse(ListBox2->Items[i].value);
sumaira manzoor hussain khanRank #8,900 · 214 reputationPosted Aug 5, 2010, 9:14 PM EDT
i wrote your code . but your code is given error.
for (a=0; a < listBox1.Items.Count;a++)
{
b += int.TryParse(listBox1.Items.Add(a));
}
i tell you one thing. " .value " does not exist in listbox library .
theLizardRank #1,079 · 1.8K reputationPosted Aug 5, 2010, 9:37 PM EDT
b += int.TryParse(listBox1.Items.Add(a));
you are attempting to add an int to the listbox and then get a total, not possible.
.value My mistake should be
b += int.TryParse(lb.Items[i].ToString());
theLizardRank #1,079 · 1.8K reputationPosted Aug 5, 2010, 9:37 PM EDT
b += int.TryParse(listBox1.Items.Add(a));
you are attempting to add an int to the listbox and then get a total, not possible.
.value My mistake should be
b += int.TryParse(lb.Items[i].ToString());
theLizardRank #1,079 · 1.8K reputationPosted Aug 5, 2010, 9:38 PM EDT
b += int.TryParse(listBox1.Items.Add(a));
you are attempting to add an int to the listbox and then get a total, not possible.
.value My mistake should be
b += int.TryParse(lb.Items[i].ToString());
sumaira manzoor hussain khanRank #8,900 · 214 reputationPosted Aug 5, 2010, 9:54 PM EDT
now this is a final code for additing integers in list box .
private void button1_Click(object sender, System.EventArgs e)
{
int a=0, b = 0,c=0;
for (a=0; a < listBox1.Items.Count;a++)
{
b += Convert.ToInt32(listBox1.Items[a]);
c = c+b;
b = 0;
}
label1.Text = Convert.ToString(c);
}