Unsort a listbox in c# winform - c#

I am very new programmer, the user is able to input vales into a ListBox, there is also a option button where the user can sort the numbers in ascending order. However I am also asked to create a option button to unsort back into its original form, however I am not sure how I would do this. I am trying to do this without the use of any containers/arrays. here is my code to sort:
private void sorted()
{
int a = lstHoldValue.Items.Count;
for (int i = 0; i < a - 1; i++)
{
var k = 0;
for (var j = 1; j < a - i; j++)
{
if (Convert.ToInt32(lstHoldValue.Items[j]) < Convert.ToInt32(lstHoldValue.Items[k]))
{
var temp = lstHoldValue.Items[j];
lstHoldValue.Items[j] = lstHoldValue.Items[k];
lstHoldValue.Items[k] = temp;
k = j;
}
else
{
k++;
}
}
}
}

Related

How to remove an element from an array and resize the array

I want to remove a specific object from an array, put it in a smaller array without getting out of range. This is what I've tried but it won't work.
Skateboard[] newSkateboard = new Skateboard[_skateboards.Length - 1];
for (int i = 0; i < _skateboards.Length; i++)
{
if (skateboard.Code != _skateboards[i].Code)
{
newSkateboard[i] = _skateboards[i];
}
}
Sure.
var j = 0;
for (int i = 0; i < _skateboards.Length; i++)
{
if (skateboard.Code != _skateboards[i].Code)
{
newSkateboard[j] = _skateboards[i];
j = j + 1;
}
}

Bubble sort is not working

Currently the user is able to input numbers into the ListBox, and I want the ListBox to be sorted by the bubble sort below when a sort checkbutton is clicked. However, it only outputs the index number e.g. 0,1,2,3... I am not allowed to use any array or containers just the items property and parsing and converting.
private void sorted()
{
int a = Convert.ToInt32(lstHoldValue.Items.Count);
int temp;
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));
}
}
This is more like a Bubble sort algorithm:
private void BubbleSort()
{
int a = lstHoldValue.Items.Count;
for (int i = 0; i < a - 1; i++)
{
var k = 0;
for(var j = 1; j < a - i; j++)
{
if (Convert.ToInt32(lstHoldValue.Items[j]) < Convert.ToInt32(lstHoldValue.Items[k]))
{
var temp = lstHoldValue.Items[j];
lstHoldValue.Items[j] = lstHoldValue.Items[k];
lstHoldValue.Items[k] = temp;
k = j;
}
else
{
k++;
}
}
}
}
It will sort the numbers in the Items collection of your lstHoldValue listBox control
If the item in the ListBox is not an integer, then it is sorted as zero (0).
This sets bubbleUp to true indicating that a swap has been made in the list box. This variable is used to indicate that a swap has/has not been made in the last comparison. Entering the while(bubbleUp) sets bubbleUp to false to indicate that no swaps have been made. Then a loop through each item in the list box to compare adjacent items and swap if needed. If a swap is made, bubbleUp gets set to true indicating that the sort is not finished. bubbleUp only needs to be set once in the for loop to indicate another iteration is necessary. CheckItem is the conversion from string to integer. Hope this helps.
private void sorted()
{
bool bubbleUp = true;
string temp = "";
while (bubbleUp)
{
// bubble up adjacent values
bubbleUp = false;
for (int i = 0; i < _ListBox.Items.Count - 1; i++)
{
if (CheckItem(_ListBox.Items[i].ToString()) > CheckItem(_ListBox.Items[i + 1].ToString()))
{
temp = _ListBox.Items[i].ToString();
_ListBox.Items[i] = _ListBox.Items[i + 1];
_ListBox.Items[i + 1] = temp;
bubbleUp = true;
}
}
}
}
private int CheckItem(string inItem)
{
int value;
if (int.TryParse(inItem, out value))
return value;
return 0;
}
private void button1_Click(object sender, EventArgs e)
{
sorted();
}

Listbox not removing values in c# GUI

The user basically enters a number of hex values into a textbox separated by commas eg. AA,1B,FF. These are then displayed in a listbox box. if the number of hex values in the textbox exceeds the size to transfer defined by the user, the listbox only displays the this number of values or if the size to transfer is bigger that adds zero values to the listbox.
this works fine until you enter a value with a zero in front of it such as AA,BB,CC,DD,EE,0F, if sizeToTransfer = 2, the listbox should display 0xAA and 0xBB. but instead it only removes the 0F value?
I'm pretty new to programming so it may be something obvious I'm missing any help would be appreciated.
private void WriteSPI1_Click(object sender, EventArgs e)
{
string hexstring = textbox1.Text;
HexValues.Items.Clear();
string[] hexarray = hexstring.Split((",\r\n".ToCharArray()), StringSplitOptions.RemoveEmptyEntries);
byte[] hexbytes = new byte[hexarray.Length];
uint num = Convert.ToUInt32(hexarray.Length);
for (int j = 0; j < hexarray.Length; j++)
{
hexbytes[j] = Convert.ToByte(hexarray[j], 16);
Hexlist.Add(hexbytes[j]);
writebuff = Hexlist.ToArray();
x = writebuff[j].ToString("X2");
HexValues.Items.Add("0x" + x);
}
if (hexarray.Length > sizeToTransfer)
{
diff = num - sizeToTransfer;
for (i = 0; i < diff+1; i++)
{
HexValues.Items.Remove("0x" + x);
}
}
else
{
diff = sizeToTransfer - num;
for (i = 0; i < diff; i++)
{
HexValues.Items.Add("0x00");
}
}
}
CHANGE
for (int j = 0; j < sizeToTransfer; j++)
{
hexbytes[j] = Convert.ToByte(hexarray[j], 16);
Hexlist.Add(hexbytes[j]);
writebuff = Hexlist.ToArray();
x = writebuff[j].ToString("X2");
HexValues.Items.Add("0x" + x);
}
WITH
for (int j = 0; j < hexarray.Length; j++)
{
hexbytes[j] = Convert.ToByte(hexarray[j], 16);
Hexlist.Add(hexbytes[j]);
writebuff = Hexlist.ToArray();
x = writebuff[j].ToString("X2");
HexValues.Items.Add("0x" + x);
}
and remove the if stantment that follow

Set dropdown list to initial value

I have manually added values in dropdown list using for loop.
for (int i = 1; i <= 31; i++)
{
date0.Items.Add(i.ToString());
date1.Items.Add(i.ToString());
date2.Items.Add(i.ToString());
date3.Items.Add(i.ToString());
}
for (int j = 1; j <= 12; j++)
{
month0.Items.Add(j.ToString());
month1.Items.Add(j.ToString());
month2.Items.Add(j.ToString());
month3.Items.Add(j.ToString());
}
for (int k = DateTime.Now.Year; k <= 2020; k++)
{
yyyy0.Items.Add(k.ToString());
yyyy1.Items.Add(k.ToString());
yyyy2.Items.Add(k.ToString());
yyyy3.Items.Add(k.ToString());
}
Now on clear button I want to clear out these values and set them to initial. I tried calling the function in which I have initialized them. But it's not happening. Can someone tell me why?
You will need to fill the dropdownlist only if the page is Not postback
if (! IsPostBack) {
//Fill out the dropdown list
}
and make sure to set SelectedIndex = 0 for those you want to set back to initial values
month0.SelectedIndex = 0;
You can reset it to the first record by setting the SelectedIndex to 0 so that the first element in the list will be selected:
date0.SelectedIndex = 0;
date1.SelectedIndex = 0;
date2.SelectedIndex = 0;
date3.SelectedIndex = 0;
month0.SelectedIndex = 0;
month1.SelectedIndex = 0;
month2.SelectedIndex = 0;
month3.SelectedIndex = 0;
yyyy0.SelectedIndex = 0;
yyyy1.SelectedIndex = 0;
yyyy2.SelectedIndex = 0;
yyyy3.SelectedIndex = 0;
You can put this on the Clear button.

Selection sort just working once in C# gui

I am using selection sort in GUI and the thing is that when I select selection sort and do sorting on generate numbers it sorts generated numbers for one time but if next time I will use other number it will do just the first step of sorting by just replacing two numbers and won't work then... So why it's not working again and why showing such different behavior?
The code is:-
void SelectionSort() {
int i = 0;
int j, min, temp;
min = i;
for (j = i + 1; j < 10; j++) {
if (generate[min] > generate[j]) {
min = j;
}
}
if (min != i) {
temp = generate[i];
generate[i] = generate[min];
generate[min] = temp;
//show1(generate);
}
show1(generate);
i++;
}
My guess, you need to add i=0; at the beginning.
I guess from your function that i is a global variable.
You need to reset i to 0 every time you enter the function (Inside the function)
using System;
namespace SelectionSortExample
{
class Program
{
static void Main(string[] args)
{
int[] num = { 105, 120, 10, 200, 20 };
for (int i = 0; i < num.Length; i++)
{
int minIndex = i;
for (int j = i + 1; j < num.Length; j++)
{
if (num[minIndex] > num[j])
{
minIndex = j;
}
}
int tmp = num[i];
num[i] = num[minIndex];
num[minIndex] = tmp;
}
}
}
}

Categories