Click to See Complete Forum and Search --> : how summing the numbers in the listbox


blankon
October 7th, 2010, 05:35 AM
the numbers in the listbox were added from the textbox.
after the numbers added, I want to summing the numbers in the listbox.

I've made this code but It didn't work:

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
int i,jum, x, y;
{
listBox2->Items->Add(txt_add->Text);
}

x=listBox2->Items->Count;


for(i=0; i<=x; i++)

{

y = Convert::ToInt32(listBox2->Items->Item[i]); //ERROR LINE

jum += y;

}

txt_total2->Text=jum.ToString();
}

I got this error:

error C3293: 'Item': use 'default' to access the default property (indexer) for class 'System::Windows::Forms::ListBox::ObjectCollection'

can you tell me what's wrong with that code, and what is the right code to solve my case?

VictorN
October 7th, 2010, 05:49 AM
This Forum is for native VC++, not for Managed C++/CLI code

cilu
October 7th, 2010, 07:29 AM
[ redirected ]

Do you want to say listBox2->Items[i] instead of listBox2->Items->Item[i]?

Alex F
October 7th, 2010, 07:41 AM
Also,

for(i=0; i<=x; i++)

should be

for(i=0; i<x; i++)