So step 1, you create an array of strings:
Dim results() As String = {"test", "test2", "test3", "test", "a", "b", "a", "\n", "c", "test", " ", "z"}
For a As Integer = 0 To results.Length - 1
ListBox1.Items.Add(results(a))
Next
C# is essentially the same, only you define the type of variable first.
Dim results() becomes string[] results and you set it:
string[] results = new string{"test", "test2", "test3", "test", "a", "b", "a", "\n", "c", "test", " ", "z"}
Step 2 was to declare some local variables used to control the loop. You will notice that you used 3 variables (a, b, and done) - a is first set to 0, and used as an index in the listbox. b is set to the total number of listbox items (-1 because a listbox index starts at 0) and decrements by 1 every time.
So what you're actually doing is looping through the entire list, 1 item at a time (a) and checking if it is equal to the item indexed by (b). If it is the same then you remove the item indexed by (b).
In step 3, you decide if the index of a is greater than the total number of items in the listbox.
I hope you're not lost so far.
I wrote the code using 3 examples.
1. Converting what you are using to C#
2. Cleaning up the example you provided using C#
3. The arraylist method which actually creates the correct list of items.
Create a project called "Stuff" and copy this code into it.
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Stuff
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Items.AddRange(new object[] {
"a",
"b",
"c",
"a",
"c",
"d",
"e",
"f",
"d",
"f",
"g",
"h",
"i",
"j",
"k"});
this.listBox1.Location = new System.Drawing.Point(8, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(152, 212);
this.listBox1.TabIndex = 0;
//
// listBox2
//
this.listBox2.Location = new System.Drawing.Point(296, 8);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(144, 212);
this.listBox2.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(184, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "Proposed";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(184, 56);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "Cleaned";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(184, 104);
this.button3.Name = "button3";
this.button3.TabIndex = 4;
this.button3.Text = "ArrayList";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(744, 590);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
listBox2.Items.Clear(); //Clear the differences box
//The proposed method
//I have preset the items in the listbox
int a = 0;
bool done = false;
while (done == false) //or while (!done)
{
for (int b = listBox1.Items.Count - 1; b > a; b--)
{
if (listBox1.Items[a].ToString() == listBox1.Items[b].ToString())
{
listBox2.Items.Add(listBox1.Items[b]); //I added this to show which items have been removed
listBox1.Items.RemoveAt(b);
}
}
a+=1;
done = (a > listBox1.Items.Count - 1);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
listBox2.Items.Clear(); //Clear the differences box
//This is a slightly cleaned up version of the above code.
//I have preset the items in the listbox
for (int a = 0; a <= listBox1.Items.Count; a++)
{
for (int b = listBox1.Items.Count - 1; b > a; b--)
{
if (listBox1.Items[a].ToString() == listBox1.Items[b].ToString())
{
listBox2.Items.Add(listBox1.Items[b]); //I added this to show which items have been removed
listBox1.Items.RemoveAt(b);
}
}
}
}
private void button3_Click(object sender, System.EventArgs e)
{
listBox2.Items.Clear(); //Clear the differences box
//Using an arraylist, I create a new list of correct items - no dupilcates
//I have preset the items in the listbox
ArrayList templist = new ArrayList();
for (int i = 0; i < listBox1.Items.Count; i++)
{
if (!templist.Contains(listBox1.Items[i]))
{
templist.Add(listBox1.Items[i]);
}
}
//results is a list of the items with the duplicate values already removed.
string[] results = (string[])templist.ToArray(typeof(string));
for (int i = 0; i < results.Length; i++)
{
if (results[i].Trim().Length > 0)
{
listBox2.Items.Add(results[i]);
}
}
}
}
}