我不知道这个项目下一步该去哪里。 计算按钮应该使用转换计算,但我不太清楚如何去做,而且我无法找到任何相关的答案,因为它从两个组合中提取数据框。
此外,组合框应根据用户选择的内容进行更改。因此,例如,如果用户选择" in"从第一个组合框,他们不应该选择" in"从第二个组合框。
如果有人能指出我正确的方向,我会非常感激。
public partial class Form1 : Form
{
const double mi_To_km = 1.6093;
const double Km_To_mi = 0.6214;
const double ft_To_m = 0.3048;
const double m_To_ft = 3.2808;
const double in_To_cm = 2.54;
const double cm_To_in = 0.3937;
const double ft_To_mi = 1 / mi_To_ft;
const double mi_To_ft = 5280;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cboFrom.Items.Clear(); //clear the combobox
string[,] measurements =
{ {"in"}, {"ft"}, {"mi"}, {"cm"}, {"m"}, {"km"} };
foreach (string str in measurements)
{
cboFrom.Items.Add(str);
}
foreach (string str in measurements)
{
cboTo.Items.Add(str);
}
}
public bool IsDecimal(TextBox txtEntry, string entry)
{
try
{
Convert.ToDecimal(txtEntry.Text);
return true;
}
catch (FormatException)
{
MessageBox.Show(entry + " must be a decimal value.", "Entry Error");
txtEntry.Focus();
return false;
}
}
private void btnCalculate_Click(object sender, EventArgs e)
{
int from = cboFrom.SelectedIndex;
int to = cboTo.SelectedIndex;
}
private void btnReset_Click(object sender, EventArgs e)
{
foreach (var c in this.Controls)
{
if (c is TextBox)
{
((TextBox)c).Text = String.Empty;
}
}
cboFrom.SelectedIndex = 0;
cboTo.SelectedIndex = 0;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
答案 0 :(得分:0)
您可以像这样使用SelectedIndexChanged事件:
private void cboFrom_SelectedIndexChanged(object sender, EventArgs e)
{
btnCalculate.Enabled = true;
if (cboTo.SelectedItem != null)
{
if (cboFrom.SelectedItem.ToString() == cboTo.SelectedItem.ToString())
{
btnCalculate.Enabled = false;
}
}
}
private void cboTo_SelectedIndexChanged(object sender, EventArgs e)
{
btnCalculate.Enabled = true;
if (cboFrom.SelectedItem != null)
{
if (cboFrom.SelectedItem.ToString() == cboTo.SelectedItem.ToString())
{
btnCalculate.Enabled = false;
}
}
}
答案 1 :(得分:0)
要实现此目的,您需要修改代码:
string[] measurements = { "in", "ft", "mi", "cm", "m", "km" };
private void Form1_Load(object sender, EventArgs e)
{
foreach (string str in measurements)
{
cboFrom.Items.Add(str);
}
foreach (string str in measurements)
{
cboTo.Items.Add(str);
}
}
private void cboFrom_SelectedIndexChanged(object sender, EventArgs e)
{
if (cboTo.SelectedItem == null)
{
cboTo.Items.Clear();
foreach (string str in measurements)
{
cboTo.Items.Add(str);
}
cboTo.Items.RemoveAt(cboFrom.SelectedIndex);
}
else
{
if (cboFrom.SelectedItem.ToString() == cboTo.SelectedItem.ToString())
{
cboTo.Items.Clear();
foreach (string str in measurements)
{
cboTo.Items.Add(str);
}
cboTo.Items.RemoveAt(cboFrom.SelectedIndex);
}
}
}
private void cboTo_SelectedIndexChanged(object sender, EventArgs e)
{
if (cboFrom.SelectedItem == null)
{
cboFrom.Items.Clear();
foreach (string str in measurements)
{
cboFrom.Items.Add(str);
}
cboFrom.Items.RemoveAt(cboTo.SelectedIndex);
}
else
{
if (cboTo.SelectedItem.ToString() == cboFrom.SelectedItem.ToString())
{
cboFrom.Items.Clear();
foreach (string str in measurements)
{
cboFrom.Items.Add(str);
}
cboFrom.Items.RemoveAt(cboTo.SelectedIndex);
}
}
}
答案 2 :(得分:0)
我会添加此代码:
private void btnCalculate_Click(object sender, EventArgs e)
{
int from = GetIndex(cboFrom.SelectedItem.ToString());
int to = GetIndex(cboTo.SelectedItem.ToString());
}
private int GetIndex(string _measure)
{
int _index = -1;
switch (_measure)
{
case "in":
_index = 0;
break;
case "ft":
_index = 1;
break;
case "mi":
_index = 2;
break;
case "cm":
_index = 3;
break;
case "m":
_index = 4;
break;
case "km":
_index = 5;
break;
}
return _index;
}