Reputation: 27
I would like to test if a TextBox
created earlier in the code exists. This TextBox
is created in a "if test", this is why I want to test if it exists. But I don't know how I can test if it exists because I can't call the TextBox
name because it doesn't exists..
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (((ComboBoxItem)typeproduit.SelectedItem).Content.ToString() == "Isolant")
{
TextBox EpIsolant = new TextBox
{
Width = 100,
Height = 29,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(209, 294, 0, 0)
};
MyPage.Children.Add(EpIsolant);
Label EpIsolantLabel = new Label
{
Content = "Ep isolant (mm)",
Width = 100,
Height = 29,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(209, 260, 0, 0)
};
MyPage.Children.Add(EpIsolantLabel);
}
else
{
// I want to test it here
// And if it exists, I want to remove it from MyPage.Children
}
}
Thanks for helping ! I couldn't find any help with Google
PS : When I try to change visibility, it's still not working:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TextBox EpIsolant = new TextBox
{
Width = 100,
Height = 29,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(209, 294, 0, 0)
};
MyPage.Children.Add(EpIsolant);
Label EpIsolantLabel = new Label
{
Content = "Ep isolant (mm)",
Width = 100,
Height = 29,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(209, 260, 0, 0)
};
MyPage.Children.Add(EpIsolantLabel);
EpIsolant.Visibility = Visibility.Hidden;
EpIsolantLabel.Visibility = Visibility.Hidden;
if (((ComboBoxItem)typeproduit.SelectedItem).Content.ToString() == "Isolant")
{
EpIsolant.Visibility = Visibility.Visible;
EpIsolantLabel.Visibility = Visibility.Visible;
}
else
{
EpIsolant.Visibility = Visibility.Hidden;
EpIsolantLabel.Visibility = Visibility.Hidden;
}
}
Upvotes: 1
Views: 1252
Reputation: 37337
As already said, you could consider things such changing Visibility
or IsEnabled
property of Button
to hide/disable it.
But if you want to stick with your solution, you could take out the variable outside the method:
private TextBox _epIsolant;
Then you can assign it a object:
_epIsolant = new TextBox
{
Width = 100,
Height = 29,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(209, 294, 0, 0)
};
and you can also check if it was created inside your method:
if(_epIsolant == null)
{
...
}
Upvotes: 1