I have a program where the user can input number into a listbox the user also gets and option to sort the listbox. I am not allowed to use any arrays or containers or list, just modify the items listbox property and use converting and parsing. I want to do this through a bubble sort, although the numbers that only displays on the listbox once the sort button is clicked is 0,1,2,3,4...
private void sorted()
{
int a = Convert.ToInt32(lstHoldValue.Items.Count);
int temp = Convert.ToInt32(lstHoldValue.Items[0]);
for (int i = 0; i < a; i++)
{
for (int j = i + 1; j < a; j++)
{
if (Convert.ToInt32(lstHoldValue.Items[i]) > Convert.ToInt32(lstHoldValue.Items[j]))
{
temp = Convert.ToInt32(lstHoldValue.Items[i]);
(lstHoldValue.Items[i]) = Convert.ToInt32(lstHoldValue.Items[j]);
(lstHoldValue.Items[j]) = temp;
}
}
}
lstHoldValue.Items.Clear();
for (int i = 0; i < a; i++)
{
Convert.ToInt32(lstHoldValue.Items.Add("\t" + i));
}
}
How users enter values to the listbox
private void btnAdd_Click(object sender, EventArgs e)
{
string text = "\t" + txtInitialise.Text;
if (this.index < MAX_ITEMS) // MAX_ITEMS or 10
{
Convert.ToInt32(lstHoldValue.Items.Count);
int dnum;
if (int.TryParse(txtInitialise.Text, out dnum))
{
Convert.ToInt32(lstHoldValue.Items.Add( "\t" + dnum));
index++;
txtInitialise.Text = "";