samedi 20 avril 2019

C# If statement Not Display Correct Information WPF

I have so that the user enters a height, and inches in the text box. If the user doesn't enter anything in the box, then the box turns red. Currently, this part works.

The issue that I'm running into is I also want a certain image to show up on the screen. For some reason, it bypasses my line that I entered saying to display the unknown image. It goes to the equation where it changes the image of the user's response.

I need help figuring out how I can get the right image to appear when the user doesn't enter anything. The image that I would like to appear is already there. I want the default image that is there from the start to stay the same and not change. When you first start the program, the unknown image is displayed. I would like the unknown image to be displayed when the user doesn't enter the value.

WPF:

 <Grid>
    <TextBox x:Name="txtFeetInput" HorizontalAlignment="Left" Height="28" Margin="201,126,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="148" RenderTransformOrigin="0.34,0.589" TextChanged="TxtFeetInput_TextChanged"/>
    <Image x:Name="imgHumanBMI" HorizontalAlignment="Left" Height="331" Margin="632,49,0,0" VerticalAlignment="Top" Width="131" Source="unknown.png"/>

</Grid>

C#:

_heightVal = 0;
string s = txtFeetInput.Text;
bool result = double.TryParse(s, out _heightVal);

if (result && _heightVal < 0)
{
    txtFeetInput.Background = Brushes.Red;
    imgHumanBMI.Source = new BitmapImage(new Uri("/Images/unknown.png", UriKind.Relative));
}
else if (String.IsNullOrEmpty(txtFeetInput.Text) || !Double.TryParse(txtFeetInput.Text, out _heightVal))
{
     txtFeetInput.Background = Brushes.Red;
     imgHumanBMI.Source = new BitmapImage(new Uri("/Images/unknown.png", UriKind.Relative));
}
else
{
     txtFeetInput.Background = Brushes.White;
}

Aucun commentaire:

Enregistrer un commentaire