货币转换器使用两个checkedlistbox c#,vb 2010

时间:2015-02-09 11:01:59

标签: c# checkedlistbox

我在c#中有点新,我有一个任务要做:

有人可以帮我处理按钮代码,如何将美元兑换成欧元?

这就是迄今为止所做的事情:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
        {
            e.Handled = true;
        }

        // only allow one decimal point
        if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
        {
            e.Handled = true;
        }
    }

      private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
       {
              for (int ix = 0; ix < checkedListBox1.Items.Count; ++ix)
               if (ix != e.Index) checkedListBox1.SetItemChecked(ix, false);

         }

      private void checkedListBox2_ItemCheck(object sender,                           ItemCheckEventArgs e)
      {
          for (int ix = 0; ix < checkedListBox2.Items.Count; ++ix)
            if (ix != e.Index) checkedListBox2.SetItemChecked(ix, false);

      }

      private void button1_Click(object sender, EventArgs e)
      {

      }

}
}

1 个答案:

答案 0 :(得分:1)

我不知道checkListBox es用于什么,但您可以通过这种方式将USD转换为EUR:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        textBox1.Text =  (Double.Parse(textBox1.Text) * 0.88).ToString(); // 0.88 EUR = 1 USD
    }
    catch { }
}

使用try语句,以便在插入无法解析为Double的内容时,程序不会崩溃

如果您希望结果在点后仅显示2位数,则可以使用textBox1.Text = (Math.Round(Double.Parse(textBox1.Text) * 0.88, 2)).ToString();

修改

所以我很无聊并且想要做些事情,并决定为我们编写一些货币转换器,以便从中学习ComboBox。那就是:

double[] dcurrency = { 1, 1.13, 1.52 }; // Currency conversion to USD (By order: USD, EUR, GBP)
double[] currency = { 1, 0.88, 0.66 }; // Currency conversion from USD (By order: USD, EUR, GBP)
string[] currencies = { "USD", "EUR", "GBP" };
public Form1()
{
    InitializeComponent();
    comboBox1.Items.AddRange(currencies);
    comboBox2.Items.AddRange(currencies);
}

private void button1_Click(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex == -1 || comboBox2.SelectedIndex == -1)
    {
        textBox2.Text = "ERROR";
        return;
    }
    try
    {
        double c1 = Double.Parse(textBox1.Text);
        textBox2.Text = (c1 * dcurrency[comboBox1.SelectedIndex] * currency[comboBox2.SelectedIndex]).ToString();
    }
    catch { textBox2.Text = "ERROR"; }
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex == comboBox2.SelectedIndex)
    {
        ((ComboBox)sender).SelectedIndex = -1;
        textBox2.Text = "ERROR";
        return;
    }
    if(comboBox1.SelectedIndex > -1 && comboBox2.SelectedIndex > -1)
        button1_Click(button1, e);
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex > -1 && comboBox2.SelectedIndex > -1)
        button1_Click(button1, e);
}

}

它使用两个TextBox es - textBox1textBox2以及两个ComboBox es - comboBox1comboBox2private void comboBox_SelectedIndexChanged是两个ComboBox es的一个事件。它会在文本更改时以及ComboBox的某个索引发生更改时自动更新转化。

我使用了两个Double数组,因为使用ComboBox索引将值转换为USD然后转换为正确的货币更简单,而不是写出大量的if语句并检查每种可能性。重要的是Double数组和ComboBox es的索引将针对相同的货币进行调整(0 = USD,1 = EUR,2 = GBP等)。另外,我为String项使用了ComboBox数组,只是为了向您展示它是如何编写的。您也可以在每个ComboBox的属性菜单中插入这些项目。享受学习!