Home > Blockchain >  How to check if strings in textboxes are in ascending order
How to check if strings in textboxes are in ascending order

Time:09-30

i have an application that generates 10 random strings then i require the user to enter these 10 strings into 10 textboxes in ascending order, once this is done the user clicks a button that checks if the order is correct then gives them a message if it was correct or not.

THE ISSUE: when a user enters every value generated in my list box to the 10 ten textboxes REGARDLESS OF ORDER and clicks the check button, success message displays. This shouldn't happen since the user only entered the values present in the listbox and did not enter them in ascending order. However if the user enters in a wrong value that's not in the listbox then the failure message is shown.The failure message should be shown aswell if the order was not in ascending.

enter image description here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WinForms_ExchangeTextsInTextBoxes
{
    public partial class Form2 : Form
    {
        /// <summary>
        /// list of call numbers created
        /// </summary>
        public static List<string> values = new List<string>
        {
            "001.45 EBC",
            "004.56 VWH",
            "002.44 MFH",
            "003.88 CSK",
            "006.96 FEB",
            "008.77 NJC",
            "005.23 MKF",
            "007.62 IJN",
            "010.13 FNH",
            "009.88 ENC"
        };


        public static List<string> sortedListBoxValues = new List<string> { };

        public Form2()
        {
            InitializeComponent();
        }

        private void Btn_Generate_Click(object sender, EventArgs e)
        {
            //clears items in the list box
            listBox1.Items.Clear();

            //random class created
            Random random = new Random();

            //for loop created that adds 10 random numbers to listbox
            for (var i = 0; i < 10; i  )
            {
                int index = random.Next(values.Count);

                //adds items to listbox
                listBox1.Items.Add(values[index]);

                // Add values to the sortedList
                sortedListBoxValues.Add(values[index]);
            }

            // Sort sorted list of listBox values
            sortedListBoxValues.Sort();
        }

        private void Btn_Check_Click(object sender, EventArgs e)
        {
            //list created
            List<string> textBoxValues = new List<string>();

            //adds each textbox to the list
            textBoxValues.Add(textBox1.Text);
            textBoxValues.Add(textBox2.Text);
            textBoxValues.Add(textBox3.Text);
            textBoxValues.Add(textBox4.Text);
            textBoxValues.Add(textBox5.Text);
            textBoxValues.Add(textBox6.Text);
            textBoxValues.Add(textBox7.Text);
            textBoxValues.Add(textBox8.Text);
            textBoxValues.Add(textBox9.Text);
            textBoxValues.Add(textBox10.Text);

            if (sortedListBoxValues.SequenceEqual(textBoxValues))
            {
                //disaplays a message to the user if the ordering was correct
                MessageBox.Show("sucess");
            }
            else
            {
                //displays a message to the user if the ordering was incorrect
                MessageBox.Show("fail");
            }
        }
    }
}
  • Related