How to get the listbox to be ordered increasingly?

Asked

Viewed 284 times

0

Hello!

My C# project has a listbox that lists numbers typed by the user. What I want to do is that these numbers are constantly being updated increasingly in the listbox.

I tried to use the Sorted tool, but what happens is: https://puu.sh/zOdxj/9ecded73c4.png

In print: I entered with the values 1,2,3,4,5,6 and when I entered with the value 11, it appeared just below the 1.

Code of the Confirm Value button:

if (tbNumero.Text == "")
        {
            MessageBox.Show("Não foi digitado nenhum número.", "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else
        {
            try
            {
                lstRoll.Items.Add(Convert.ToDouble(tbNumero.Text));                                                          
                btnRemoveRoll.Enabled = true;
                btnResetAll.Enabled = true;

                tbNumero.Text = "";
                tbNumero.Focus();
                lstRoll.Sorted = true;
            }
            catch (Exception)
            {
                MessageBox.Show("Por favor, digite apenas numeros", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbNumero.Text = "";
                tbNumero.Focus();
            }

        }

Solved with this video: https://www.youtube.com/watch?v=rKu3m1NwrUU

  • Elements are sorted as text which is what was stored in ListBox

1 answer

0


Even if you have converted the text to the type Double, before adding the value to the ListBox:

lstRoll.Items.Add(Convert.ToDouble(tbNumero.Text));

the classification in control ListBox is always done alphabetically, treating items as if they were String:

Listbox.Sorted Property
https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.sorted.aspx

You can then play the list of values for an array, sort them there and return the sorted list numerically to the ListBox:

// Cria um array de doubles com a mesma quantidade de itens que o ListBox.
double[] arrayNumerico = new double[lstRoll.Items.Count];
// Copia a lista de itens para o array de doubles.
((ICollection)lstRoll.Items).CopyTo(arrayNumerico, 0);
// Classifica o array de doubles, de forma numérica.
Array.Sort(arrayNumerico);
// Limpa o conteúdo do ListBox e adiciona os itens do array, classificados numericamente.
lstRoll.SetItemsCore(arrayNumerico);

EDIT:
I hadn’t noticed that the scope of the method ListBox.SetItemsCore() is Protected, then you can’t use it directly. Instead of it (the last line of the code) you can do so, then:

lstRoll.Items.Clear();
lstRoll.Items.AddRange((object[])arrayNumerico); 
  • Got it. Thanks! I have one more question: How do I select the first value of listbox? I need to do (last value) - (first value) to store in a variable, but I don’t know the syntax.

  • You can do so to pick up the first item lstRoll.Items[0], and so to catch the last lstRoll.Items[lstRoll.Items.Count-1].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.