Sorted number inside in the textbox

YUI-012 160 Reputation points
2025-02-08T22:43:26.04+00:00

1

I want to sort the value of textbox1 automatically and display into textbox2.

I have a code but it not working properly. I insert this code into textbox2.

 private void textBox2_TextChanged(object sender, EventArgs e)
        {
			var count = default(int);
            if (int.TryParse(textBox1.Text, count))
            {
                string strNumbers = " ";
                for (int x = 0, loopTo = count - 1; x <= loopTo; x++)
                    strNumbers += x.ToString();
                textBox2.Text = strNumbers;
            }
        }

Developer technologies | Visual Studio | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Marcin Policht 50,730 Reputation points MVP Volunteer Moderator
    2025-02-09T01:26:32.04+00:00

    Appreciate the clarification. It looks like you want to input numbers separated by spaces and have them sorted while maintaining leading zeros. If so, try the following:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        // Read the input from textBox1 and split by spaces
        string input = textBox1.Text.Trim();
        string[] numbers = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    
        // Check if all elements are valid numbers
        if (numbers.All(n => int.TryParse(n, out _)))
        {
            // Sort numbers while preserving leading zeros
            var sortedNumbers = numbers.OrderBy(n => n).ToArray();
    
            // Join them back into a space-separated string
            textBox2.Text = string.Join(" ", sortedNumbers);
        }
        else
        {
            textBox2.Text = "Invalid input"; // Display error if input is not numeric
        }
    }
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Marcin Policht 50,730 Reputation points MVP Volunteer Moderator
    2025-02-08T23:48:23.4666667+00:00

    There are a few issues with your code:

    1. Wrong Event: You are using the TextChanged event of textBox2, but the logic should trigger when textBox1 changes.
    2. Incorrect TryParse Usage: The second argument of int.TryParse should be an out parameter.
    3. Sorting Issue: Your current code is not sorting anything; it generates numbers from 0 to count-1 instead.
    4. Whitespace in Output: You are concatenating numbers as a string but leaving an unnecessary leading space.

    Attach this event to textBox1.TextChanged instead:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (int.TryParse(textBox1.Text, out int count))
        {
            // Generate a list of numbers from 1 to count
            List<int> numbers = new List<int>();
            for (int i = 1; i <= count; i++)
            {
                numbers.Add(i);
            }
    
            // Sort numbers (although it's naturally sorted)
            numbers.Sort();
    
            // Convert to a string and display in textBox2
            textBox2.Text = string.Join(", ", numbers);
        }
        else
        {
            textBox2.Text = "Invalid input";
        }
    }
    
    • This code listens for changes in textBox1.
    • It tries to parse the integer value from textBox1.
    • It generates numbers from 1 to count, sorts them (though unnecessary here), and joins them into a string.
    • The result is displayed in textBox2.

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.