Trying to find out the elements of ArrayList by using non-generic collection in C# | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Trying to find out the elements of ArrayList by using non-generic collection in C#

Hello There, I was trying to find out the all elements of an ArrayList in C# by clicking on a button in the windows form application but it prints only the first element. used +1 too but the result is still the same. I want all the elements to be printed in textbox 2 (not at the same time but clicking on the button5 like first time click it prints a, 2nd time click it prints b, and so on... and we don't know how many elements will user enter in the ArrayList) which are entered in the ArrayList. I tried the below code: ArrayList ar = new ArrayList(); private void button1_Click(object sender, EventArgs e) { ar.Add(textBox1.Text); textBox1.Clear(); textBox1.Focus(); } private void button2_Click(object sender, EventArgs e) { listBox1.Items.Clear(); foreach (object ob in ar) { listBox1.Items.Add(ob); } } private void button3_Click(object sender, EventArgs e) { ar.Remove(textBox1.Text); } private void button4_Click(object sender, EventArgs e) { textBox2.Text = ar[0].ToString(); label1.Text = "That's first array"; } private void button5_Click(object sender, EventArgs e) { textBox2.Text = ar[ar.IndexOf(0) + 1].ToString(); } Please check the button 5 code and let me know where I am doing mistakes while making the code on button 5.

12th Dec 2020, 11:05 AM
Shubhankar Mishra
Shubhankar Mishra - avatar
1 Réponse
0
Just an idea, Add a private member (int) to the class, name it <arIndex> (for example). Use that inside button5_click() to refer a certain element in array <ar>. Increment <arIndex> after using it as element index of an element to be put into textBox2, so that successive calls will refer to next index available. But you better reset <arIndex> to zero when you're done with last element of <ar>, so you can start over on next click.
12th Dec 2020, 12:36 PM
Ipang