【问题标题】:Currency Converter using two checkedlistbox c#, vb 2010货币转换器使用两个checkedlistbox c#, vb 2010
【发布时间】:2015-02-09 11:01:58
【问题描述】:

我对 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)
      {

      }

}
}

【问题讨论】:

  • 为什么需要checkedListBox 才能将美元兑换成欧元?

标签: c# checkedlistbox


【解决方案1】:

我不知道checkListBoxes 的用途是什么,但您可以通过这种方式将美元转换为欧元:

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);
}

}

它使用两个 TextBoxes - textBox1textBox2 和两个 ComboBoxes - comboBox1comboBox2private void comboBox_SelectedIndexChanged 是两个 ComboBoxes 的一个事件。当文本更改以及ComboBox 的索引之一发生更改时,它会自动更新转换。

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

【讨论】:

  • 这很有帮助,谢谢,我真正的问题是如何从checklistbox 1和checklistbox 2中获取元素,然后进行转换,如下所示:checkbox1:checkbox2:EUR EUR USD USD GBP GBP 如果从 1 开始检查 EUR,如果从 2 开始检查 USD,则转换。
  • 我建议使用ComboBox,因为在CheckedListBox 中您可以选择多个选项,而在ComboBox 中您可以从项目列表中选择一个。
  • 我添加了一些货币转换器代码,以便您了解如何使用ComboBox
  • 谢谢你,Aradmey,我尝试了这段代码,并用 checklistbox 做了一些修改,效果很好。这很有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多