Below is my first attempt at programming a Celsius to Fahrenheit converter in C# winforms. I'm looking for tips and advice on improving my style and what I can do to make the code more efficent/proper, or is it good as is? I apologize ahead of time if i've forgotten any details and would be happy to provide more upon request.
namespace TemperatureConverter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Initalize the handler to allow use of only control/number characters.
this.txtCelsius.KeyPress += new KeyPressEventHandler(txtCelsius_KeyPress);
// Limits user's inpute to 5 characters max.
txtCelsius.MaxLength = 5;
// Prevents the user from resizing window.
this.FormBorderStyle = FormBorderStyle.FixedDialog;
}
// Main function. Converts the input into Fahrenheit and outputs to the targeted label.
private void btnConvert_Click(object sender, EventArgs e)
{
double C;
double F;
C = double.Parse(txtCelsius.Text);
F = C * 9 / 5 + 32;
lblFahrenheit.Text = F.ToString();
}
// Prevents user from entering anything other than Number and Control characters.
private void txtCelsius_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsControl(e.KeyChar) && !Char.IsNumber(e.KeyChar))
{
e.Handled = true;
}
}
}
}
celciusValue
,fahrenheitValue
andeventArgs
. \$\endgroup\$e
is a pretty typical name for theeventArgs
argument. I think .NET may even default to it under some circumstances. Doesn't mean it isn't worth changing, but it is a convention. \$\endgroup\$