locked
c# winform 3 textboxs the lowest value of 2 textboxs shown in the 3rd RRS feed

  • Question

  • I have 3 textboxs, 2 textboxs have a number in them I need the third textbox to show the lowest value of the 2.

    I have looked all over for an example.


    textbox1 = 5

    textbox 2 = 10

    textbox 3 = 5  result



    Booney440

    Tuesday, August 11, 2020 12:14 PM

Answers

  • Try this

    private void button1_Click(object sender, EventArgs e)
    {
        if (int.TryParse(textBox1.Text, out var value1) && int.TryParse(textBox2.Text, out var value2))
        {
            var values = new[] {value1, value2};
            textBox3.Text = values.Min().ToString();
        }
    }

    Or old school

    private void button1_Click(object sender, EventArgs e)
    {
        int value1 = 0;
        int value2 = 0;
        if (int.TryParse(textBox1.Text, out value1) && int.TryParse(textBox2.Text, out value2))
        {
            var values = new[] {value1, value2};
            textBox3.Text = values.Min().ToString();
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Booney440 Tuesday, August 11, 2020 1:09 PM
    Tuesday, August 11, 2020 12:33 PM

All replies

  • Try this

    private void button1_Click(object sender, EventArgs e)
    {
        if (int.TryParse(textBox1.Text, out var value1) && int.TryParse(textBox2.Text, out var value2))
        {
            var values = new[] {value1, value2};
            textBox3.Text = values.Min().ToString();
        }
    }

    Or old school

    private void button1_Click(object sender, EventArgs e)
    {
        int value1 = 0;
        int value2 = 0;
        if (int.TryParse(textBox1.Text, out value1) && int.TryParse(textBox2.Text, out value2))
        {
            var values = new[] {value1, value2};
            textBox3.Text = values.Min().ToString();
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Booney440 Tuesday, August 11, 2020 1:09 PM
    Tuesday, August 11, 2020 12:33 PM
  • Excellent

    Thank You


    Booney440

    Tuesday, August 11, 2020 1:09 PM