First, double-check your Load
handler is actually connected to the form’s event.
Next, try using breakpoints. Step through the code and watch the values change. If the breakpoint in Load
isn’t hit, well, something’s wrong with the event.
Then, a weird thing: try changing your Load
handler to a Shown
handler.
For historic reasons, in certain situations if the Load
handler throws an exception it is quietly handled and you are none the wiser. So it’s smarter to use the Shown
event so you can tell if this happened.
Beyond that, someone else might find something I’m missing.
Thanks for the help so far. I am kind of new to this c# coding still, but how do I check if the load is actually connected to the forms event?
Your issue is the type of combo box. I don’t recall the names but one allows typing in it and one doesn’t. The one that allows you to type will default to blank. Check the properties for that item and change it to dropdown list, I think.
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Conversions
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int k;
string[,] conversionTable = {
{“Miles to kilometers”, “Miles”, “Kilometers”, “1.6093”},
{“Kilometers to miles”, “Kilometers”, “Miles”, “0.6214”},
{“Feet to meters”, “Feet”, “Meters”, “0.3048”},
{“Meters to feet”, “Meters”, “Feet”, “3.2808”},
{“Inches to centimeters”, “Inches”, “Centimeters”, “2.54”},
{“Centimeters to inches”, “Centimeters”, “Inches”, “0.3937”}
};
private void Form1_Load(object sender, EventArgs e)
{
// to add all conversion in combobox on form load
cboConversions.Items.Add(conversionTable[0, 0]);
cboConversions.Items.Add(conversionTable[1, 0]);
cboConversions.Items.Add(conversionTable[2, 0]);
cboConversions.Items.Add(conversionTable[3, 0]);
cboConversions.Items.Add(conversionTable[4, 0]);
cboConversions.Items.Add(conversionTable[5, 0]);
cboConversions.SelectedIndex = 0; // to show 1st item by default in combo box
lblCalculatedLength.Enabled = false; // to disable editing of textbox2
{
}
}
private void cboConversions_SelectedIndexChanged(object sender, EventArgs e)
{
txtLength.Clear();
k = cboConversions.SelectedIndex;
lblConversions.Text = conversionTable[k, 1]; // to change the label depending upon user selection
lblFromLength.Text = conversionTable[k, 2];// to change the label depending upon user selection
txtLength.Focus(); //focus on textbox1
}
public bool IsDecimal(TextBox textBox, string name)
{
try
{
Convert.ToDecimal(textBox.Text);
return true;
}
catch (FormatException)
{
MessageBox.Show(name + ” must be a decimal number.”, “Entry Error”);
textBox.Focus();
return false;
}
}
private void button1_Click(object sender, EventArgs e)
{
bool p = IsDecimal(txtLength, “length”); //decimal check
if (p == true)
{
double a = Convert.ToDouble(txtLength.Text);
double unit = Convert.ToDouble(conversionTable[k, 3]);
double ans = a * unit;
lblCalculatedLength.Text = ans.ToString(); // final conversion display
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
I haven’t worked with winforms in a good while, but I would not add items directly to the combo box. Look into binding source or binding a collection to a combobox.
C# devs
null reference exceptions