using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
Im trying to create a program that sorts 3 numbers in ascending and descending order by the press of either button. I added the 2 buttons but as of right now they have no value to them. The 3 numbers need to be inserted into a text box. Ex) text box 1, text box 2, text box 3. I am stuck any guidance would be great. Please and thank you!
Loading
Jennifer BellPosted Nov 26, 2012, 11:56 AM
VulpesPosted Nov 26, 2012, 11:32 AM
If I were you, I'd leave the 'MessageBox' code in to check for input errors. If you don't and someone presses button1 when one of the textboxes is still empty, your application will end with an exception.
Jennifer BellPosted Nov 26, 2012, 11:24 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void
button1_Click(object sender, EventArgs e)
{
SortNumbers(false);
}
private void
button2_Click(object sender, EventArgs e)
{
SortNumbers(true);
}
private void
SortNumbers(bool reverse)
{
double[] numbers = { number1, number2, number3 };
Array.Sort(numbers);
if (reverse) Array.Reverse(numbers);
textBox1.Text = numbers[0].ToString();
textBox2.Text = numbers[1].ToString();
textBox3.Text = numbers[2].ToString();
}
}
}
I changed the numbers to be double in this program. I then took out the message boxes so I can use decimal numbers. After doing this I now get an error for numbers 1-3 saying does not exist in the current context.
VulpesPosted Nov 26, 2012, 4:48 AM