i am creating a simple currency converter app in WPF C#. i need to convert to the currency usd to indian rupees or srilankan rupees but couldn't process the output. i have done the design and code when i ran the application output not displayed and error also not displayed what was a problem i don't know.what i tried so far i attached the code below please have a look . WPFdesign
<Label Content="Amount :"
Grid.Row="0"
Grid.Column="0"
/>
<TextBox
x:Name="TextBoxAmount"
Grid.Row="0"
Grid.Column="1"
/>
<Label Content="From :"
Grid.Row="2"
Grid.Column="0"
/>
<ComboBox Name="CboFrom" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="25" Grid.Row="2"
Grid.Column="1">
<ComboBoxItem Content="USD"></ComboBoxItem>
</ComboBox>
<Label Content="To :"
Grid.Row="4"
Grid.Column="0"
/>
<ComboBox Name="CboTo" HorizontalAlignment="Left" VerticalAlignment="Top"
Width="100" Height="25" Grid.Row="4"
Grid.Column="1">
<ComboBoxItem Content="Srilankan Rupees"></ComboBoxItem>
<ComboBoxItem Content="Indian Rupees"></ComboBoxItem>
</ComboBox>
<Label Content="Total :"
Grid.Row="6"
Grid.Column="0"
/>
<TextBox
x:Name="TextBoxTotal"
Grid.Row="6"
Grid.Column="1"
/>
</Grid>
<StackPanel
Grid.Row="8"
Grid.ColumnSpan="2"
Orientation="Horizontal">
<Button
Width="60"
Height="30"
Content="Convert"
x:Name="ButtonConvert"
Margin="10 0 10 0"
Click="ButtonConvert_Click" Background="#FF04097E" Foreground="White"/>
</StackPanel>
C# Code
private void ButtonConvert_Click(object sender, RoutedEventArgs e)
{
double tot;
double amount = double.Parse(TextBoxAmount.Text);
if (CboFrom.SelectedItem == "USD" && CboTo.SelectedItem == "Srilankan Rupees")
{
tot = amount * 179.50;
TextBoxTotal.Text = tot.ToString();
}
else if (CboFrom.SelectedItem == "USD" && CboTo.SelectedItem == "Indian Rupees")
{
tot = amount * 70;
TextBoxTotal.Text = tot.ToString();
}
}
CodePudding user response:
CboFrom.SelectedItem is not a string, it's a ComboBoxItem.
What you want to compare is ((ComboBoxItem)CboFrom.SelectedItem).Content.ToString()
.
Which gives you:
if (((ComboBoxItem)CboFrom.SelectedItem).Content.ToString() == "USD" && ((ComboBoxItem)CboTo.SelectedItem).Content.ToString() == "Srilankan Rupees")
Whilst that will work, it is definitely hacky and bad design. I advise you look into EldHasp's suggestions to improve your design.
CodePudding user response:
You set a collection of UI elements in the ComboBox - ComboBoxItem. Therefore, you select a UI element. And it will not work to compare it with the value of the data.
According to the application logic, you need to set the data collection in the ComboBox source so that you can select this data.
In its simplest form, it would be something like this:
public static class SomeHelper
{
public static ReadOnlyCollection CurrencyСodes {get;} = Array.AsReadOnly(new string[] {"USD"});
public static ReadOnlyCollection CurrencyNames {get;} = Array.AsReadOnly(new string[] {"Srilankan Rupees", "Indian Rupees"});
}
<ComboBox x:Name="CboFrom" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="25"
Grid.Row="2" Grid.Column="1"
ItemsSource="{x:Static local:SomeHelper.CurrencyСodes}"/>
<ComboBox x:Name="CboTo" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="25"
Grid.Row="4" Grid.Column="1"
ItemsSource="{x:Static local:SomeHelper.CurrencyNames}"/>
private void ButtonConvert_Click(object sender, RoutedEventArgs e)
{
double tot;
double amount = double.Parse(TextBoxAmount.Text);
if (CboFrom.SelectedItem == SomeHelper.CurrencyСodes[0])
{
if (CboTo.SelectedItem == SomeHelper.CurrencyNames[0])
{
tot = amount * 179.50;
TextBoxTotal.Text = tot.ToString();
}
else if (CboTo.SelectedItem == SomeHelper.CurrencyNames[1])
{
tot = amount * 70;
TextBoxTotal.Text = tot.ToString();
}
}
}
P.S. But I would recommend that you implement a normal WPF-typical ViewModel. This will greatly simplify the code, make it easier to read and understand. Reduce the likelihood of various errors.