|
How to randomly reorder an ArrayList and refresh the ComboBox contents at runtime
|
|
Rating: 9 user(s) have rated this article
Posted by: Suprotim Agarwal,
on 6/3/2008,
in category "Windows Forms 2.0"
Views: this article has been read 9150 times
Abstract: In this short snippet, we will take a look at how to randomly reorder an array using an ArrayList.
How to randomly reorder an ArrayList and Refresh the ComboBox contents at Runtime
In this short snippet, we will take a look at how to randomly reorder an arraylist and update the contents of the ComboBox each time the contents of the ArrayList changes. This article has been republished.
Note : If anyone is interested in the VB.NET version of this code, it is given at the end of this article.
Step 1: Drag and drop a combobox and a button on your form.
Step 2: Declare an ArrayList at the form level
C#
ArrayList l = new ArrayList();
Step 3: In the Form_Load(), add items to the ArrayList
C#
private void Form1_Load(object sender, EventArgs e)
{
l.Add("Jack");
l.Add("Jim");
l.Add("Harry");
l.Add("Zane");
l.Add("Sally");
RandomOrder(l);
}
Step 4: Create a function called RandomOrder
C#
public void RandomOrder(ArrayList arrList)
{
Random r = new Random();
for (int cnt = 0; cnt < arrList.Count; cnt++)
{
object tmp = arrList[cnt];
int idx = r.Next(arrList.Count - cnt) + cnt;
arrList[cnt] = arrList[idx];
arrList[idx] = tmp;
}
}
The logic is as follows. This function accepts an arraylist. Then a random element is picked up and added at position 0. Another random element is picked where postion <> 0 and swapped into position 1 and then another random element is picked where position <> 0 or 1 and swapped it into position 2 and so on…
Step 5: Now we will add some code to update the combobox each time the arraylist is updated. ArrayList belongs to the type Ilist. If your control is bound to a data source that does not implement the IBindingList interface, such as an ArrayList then changes to the list will NOT reflect as changes in the combobox. So for that, we will use this code to update the ComboBox each time the arraylist changes :
C#
BindingManagerBase bindMB = comboBox1.BindingContext[arrList];
bindMB.SuspendBinding();
bindMB.ResumeBinding();
comboBox1.DataSource = arrList;
As you see, suspending and resuming the binding process forces the combobox to be updated manually.
Step 6: Call the RandomOrder() method on the click event of the button
C#
privatevoid button1_Click(object sender, EventArgs e)
{
RandomOrder(l);
}
And that’s it.
The entire code looks like this :
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsApplication1
{
publicpartialclassForm1 : Form
{
ArrayList l = newArrayList();
public Form1()
{
InitializeComponent();
}
privatevoid Form1_Load(object sender, EventArgs e)
{
l.Add("Jack");
l.Add("Jim");
l.Add("Harry");
l.Add("Zane");
l.Add("Sally");
RandomOrder(l);
}
publicvoid RandomOrder(ArrayList arrList)
{
Random r = newRandom();
for (int cnt = 0; cnt < arrList.Count; cnt++)
{
object tmp = arrList[cnt];
int idx = r.Next(arrList.Count - cnt) + cnt;
arrList[cnt] = arrList[idx];
arrList[idx] = tmp;
}
BindingManagerBase bindMB = comboBox1.BindingContext[arrList];
bindMB.SuspendBinding();
bindMB.ResumeBinding();
comboBox1.DataSource = arrList;
}
privatevoid comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
privatevoid button1_Click(object sender, EventArgs e)
{
RandomOrder(l);
}
}
}
VB.NET
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Collections
Namespace WindowsApplication1
PublicPartialClass Form1
Inherits Form
Private l As ArrayList = New ArrayList()
PublicSubNew()
InitializeComponent()
EndSub
PrivateSub Form1_Load(ByVal sender AsObject, ByVal e As EventArgs)
l.Add("Jack")
l.Add("Jim")
l.Add("Harry")
l.Add("Zane")
l.Add("Sally")
RandomOrder(l)
EndSub
PublicSub RandomOrder(ByVal arrList As ArrayList)
Dim r As Random = New Random()
For cnt AsInteger = 0 To arrList.Count - 1
Dim tmp AsObject = arrList(cnt)
Dim idx AsInteger = r.Next(arrList.Count - cnt) + cnt
arrList(cnt) = arrList(idx)
arrList(idx) = tmp
Next cnt
Dim bindMB As BindingManagerBase = comboBox1.BindingContext(arrList)
bindMB.SuspendBinding()
bindMB.ResumeBinding()
comboBox1.DataSource = arrList
EndSub
PrivateSub comboBox1_SelectedIndexChanged(ByVal sender AsObject, ByVal e As EventArgs)
EndSub
PrivateSub button1_Click(ByVal sender AsObject, ByVal e As EventArgs)
RandomOrder(l)
EndSub
EndClass
EndNamespace
References :
Conclusion: There are numerous ways of reordering an arraylist. I just demoed one of them. You could also use the Generic collection to do so. Try it out!!. I hope this article was useful and I thank you for viewing it.