對 C# 來說很新。
我想從組合框中讀取用戶輸入,并以一種將一種貨幣轉換為另一種貨幣的方法使用選定的貨幣。輸出不在控制臺中,而是在 Windows 表單上。
這是我的組合框輸入讀取代碼:
public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
double first = comboBox1.SelectedIndex;
}
public void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
double second = comboBox2.SelectedIndex;
}
這是我的代碼,它使用選定的索引并轉換它們:
internal class Currencies
{
public double[] Convert(double[] currencies, int amount)
{
double[] convertedAmount = new double[currencies.Length];
if (Converter.comboBox1.first == HRK && Converter.comboBox2.second == EURO)
{
int exchangeRate1 = 1;
double exchangeRate2 = 7.4;
for (int i = 0; i < currencies.Length; i )
{
convertedAmount[i] = amount * exchangeRate1 / exchangeRate2;
}
}
else if (Converter.comboBox1.first == EURO && Converter.comboBox2.seocnd == HRK)
{
int exchangeRate1 = 1;
double exchangeRate2 = 7.4;
for (int i = 0; i < currencies.Length; i )
{
convertedAmount[i] = amount * exchangeRate1 / exchangeRate2;
}
}
else
{
throw new NotSupportedException();
}
return convertedAmount;
}
}
為什么該方法不允許我使用存盤所選索引的變數“first”和“second”來轉換貨幣?
uj5u.com熱心網友回復:
擺脫 selectedIndexChanged 程序,因為您在那里宣告了在程序觸發后收集垃圾的區域變數。
只需將Convert() 方法中的first和second替換為comboBox1.SelectedValue和comboBox2.SelectedValue
uj5u.com熱心網友回復:
抱歉,當我遇到問題時
public string SelectedItem { get; set; }
你必須添加新的類utils,你必須{ get ; set }
每一個你都有這個更好的概念
uj5u.com熱心網友回復:
你可以使用這種方法,這就是我在我的應用程式中經常做的事情
- 創建三個檔案夾(ViewModel、View 和 model)
- 從 Nugget 安裝 fody.propertyChange
- 為您的頁面創建一個 viewModel
string SelectedIted {get; set;}在 viewwModel 中定義一個變數- 在頁面的 xaml 代碼隱藏中,將
DataContext = new NameOfYourViewModel
像這樣的東西
<ComboBox
Grid.Row="2"
Grid.Column="1"
Margin="10,0,10,0"
HorizontalAlignment="Stretch"
ui:ControlHelper.PlaceholderText="Select Lenguague"
DisplayMemberPath="Value.Name"
ItemsSource="{Binding LanguagesDictionary}"
SelectedValue="{Binding SelectedItem}"
SelectedValuePath="Value.Code" />
在你的虛擬機中
public string SelectedItem { get; set; }
在你的 XAML 代碼后面
DataContext = new AudioPageViewModel();
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/522141.html
標籤:C#表格
