gpt4 book ai didi

c# - 更新我的货币转换器 c# 中的汇率

转载 作者:行者123 更新时间:2023-12-02 08:52:18 26 4
gpt4 key购买 nike

我只是想知道如何在整个程序中使用更新后的汇率。这是迄今为止我的代码供引用...

//Form 1
private void update_Click(object sender, EventArgs e)
{
if (fromcountry.Text == tocountry.Text)
{
MessageBox.Show(" Please Choose Two Different Currencies To Use This Function", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
btnconvert.Enabled = true;
Exchange_Rate frm = new Exchange_Rate();
frm.Show(this);
}
}
//Form 1 one of the comboboxes for selecting 2nd country
private void tocountry_SelectedIndexChanged(object sender, EventArgs e)
{
btnupdate.Enabled = true;
btnconvert.Enabled = true;
txtvalue.Enabled = true;
exchange();
}
private void exchange()
{
if (fromcountry.Text == tocountry.Text)
{
lblexchange.Text = "1";
}
else if (fromcountry.Text == "SGD - Singapore Dollar" && tocountry.Text == "USD - US Dollar")
{
lblexchange.Text = "1.26";
}
else if (fromcountry.Text == "SGD - Singapore Dollar" && tocountry.Text == "MYR - Malaysian Ringgit")
{
lblexchange.Text = "2.35";
}
else if (fromcountry.Text == "SGD - Singapore Dollar" && tocountry.Text == "EUR - Euro")
{
lblexchange.Text = "0.60";
}
//Form 2
private void btnok_Click(object sender, EventArgs e)
{
try
{
double exchange;
exchange = Double.Parse(txtcurrent.Text);
var frm = (currencyconverter)this.Owner;
frm.PassValue(txtcurrent.Text);
this.Close();
}
catch
{
MessageBox.Show("Please Enter Numbers", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
txtcurrent.Text = "";
}
}

我知道使用 if-else 方法在程序开始时获得费率太模糊,而且我只是一个学习简单编程的学生。但我仍然需要知道当我再次按下相同的转换时如何使用更新的汇率。如果没有足够的信息,我可以帮助您获得更多编码

最佳答案

您可以使用共享货币对象来保存有关货币汇率的信息

public class Currency
{
private Currency(string name)
{
Name = name;
}

public string Name {get; private set;}
public decimal Rate {get; private set;}

private void SetRate(decimal rate)
{
Rate = rate;
OnRateChanged(this);
}

public static event EventHandler RateCanged;
private static OnRateChanged(Currency currency)
{
var handler = RateChanged;
if(handler != null)
{
handler(currency, EventArgs.Empty);
}
}

private Dictionary<string, Currency> currencies = new Dictionary<string, Currency>();

public static Currency GetCurrency(string name)
{
Currency currency;
if(!currencies.TryGetValue(name, out currency))
{
currency = new Currency(name);
currencies[name] = currency;
}
}
}

所以您有一个简单的共享费率存储空间,您可以在任何地方使用它

class Form1
{
public Form1()
{
...
Currency.RateChanged += RateChanged;
}

private void RateChanged(object source, EventArgs e)
{
labelRate.Text = Currency.GetCurrency("USD").Rate;
}
}

class Form2
{
public Form2()
{
...
rateTextBox.Text = Currency.GetCurrency("USD").Rate.ToString();
}

void updateButtin_Click()
{
Currency.GetCurrency("USD").SetRate(decimal.Parse(rateTextBox.Rate));
}
}

关于c# - 更新我的货币转换器 c# 中的汇率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17878093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com