简体   繁体   中英

C# Set the visibility of a label using the result of String.IsNullOrEmpty

Hi I am trying to set the visibility of a label based on a textbox string being empty. I have the following code:

MyLabel.Visible = String.IsNullOrEmpty(MyTextBox.Text);

Why does MyLabel not appear when the textbox is left empty?

I have tried placing this code in the Text_Changed event of the textbox and it still doesn't work.

This was an update issue, it does work on the Text_Changed event. However the issue is it does not work when triggered on the proccessing of the form.

Here is the code triggered from my controller class to give everyone a better insight as to what is going on:

using (var frm = new frmAdd(PersonType.Carer))
{
    var res = frm.ShowDialog();
    if (res == System.Windows.Forms.DialogResult.OK)
    {
         if (frm.ValidateInformation())  // the above code is called in here
         {
               // process the information here...
         }
    }
}

Also I forgot to mention that this form is in a Class Library project (dll).

Depends on where the code is being run. If you need interactivity, ie the label disappears when a character is typed into the textbox, you need to run it on the Keyup event of the textbox. You may also need to repaint the label.

If you are updating the Visible property in the text changed event you are probably running into the following problem. When the form first starts up the text is set to an empty string. But because this is the initial value it hasn't changed so to speak and hence no event is raised.

You may need to perform this update directly in your Form constructor. See if that fixes the problem.

I suspect you want to use data binding to set the visibility of the label.
This discussion might help you: WPF Data Binding : enable/disable a control based on content of var?

Update, some code:

public string MyText
{
  get { return _myText; }
  set { _myText = value; OnPropertyChanged("MyText"); }
}

public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
  if (PropertyChanged != null)
  {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  }
}

private void theTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
  _myText = theTextBox.Text;
  OnPropertyChanged("MyText");
}

}

[ValueConversion(typeof(string), typeof(System.Windows.Visibility))]
public class StringToVisibilityConverter : System.Windows.Data.IValueConverter
{
  public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    System.Windows.Visibility vis;
    string stringVal = (string)value;
    vis = (stringVal.Length < 1) ? Visibility.Visible : Visibility.Hidden;
    return vis;
  }

  public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    return null;
  }  
}
In the XAML:
<TextBlock Background="AliceBlue" >
  <TextBlock.Visibility>
    <Binding ElementName="window1" Path="MyText" Converter="{StaticResource stringToVisibilityConverter}"/>
  </TextBlock.Visibility>
</TextBlock>

I would add a Trim, to be more consistent to the User in case of spaces left:

MyLabel.Visible = String.IsNullOrEmpty(MyTextBox.Text.Trim());

For the rest it is a matter of triggering the code at the right time. TextChanged should cover everything but the inital state, as addressed by JaredPar. Although I would use Form_Load, not the constructor.

Edit, after the clarification:
If your Label an TextBox are on frmAdd then the question is moot, the form itself is no longer shown after ShowDialog returns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM