Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C#, I need to create a Windows Forms application that requires the three numbers entered in the text box to be sorted from large to small.
C#, I need to create a Windows Forms application that requires the three numbers entered in the text box to be sorted from large to small.

private void button1_Click(object sender, EventArgs e)

{

//Get the value of the text box

string txt1 = textBox1 .Text;

string txt2 = textBox2.Text;

string txt3 = textBox3.Text;

//Define the integer variable stored during conversion (if Floating point type can be replaced by yourself)

int num1 = 0;

int num2 = 0;

int num3 = 0;

int result = 0;//The smallest number

//int.TryParse will return whether txt1 can be converted to int type. If it can be converted, the value will be assigned to num1

if ( !int.TryParse(txt1, out num1)) //If txt1 cannot be converted to int type

{

MessageBox.Show("The first text box is not a valid integer") ;

return;

}

result = num1;//Because it is the first value, it is assigned directly

if ( !int.TryParse(txt2, out num2)) //If txt2 cannot be converted to int type

{

MessageBox.Show("The second text box is not a valid integer") ;

return;

}

if (num2 < result) //If num2 is less than the current smallest number

{

result = num2;//The current smallest number is changed to num2

}

if (!int.TryParse(txt3, out num3)) //If txt3 cannot be converted to int type

{

MessageBox.Show("The third text box is not a valid integer");

return;

}

if (num3 < result) //If num3 is less than the current smallest number

{

result = num3;//The current smallest number number, change to num3

}

//Display the final smallest number

label4.Text = "Among the three text boxes, the smallest number is" + result;

}