tags:

views:

1201

answers:

4

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?

Update

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).

+2  A: 

Depends on where the code is being run. If you need interactivity, i.e. 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.

Robert Harvey
The .Visible property of the label is never going to true tho.
James
No I mean when the above evaluation is processed the Visible property of the label does not change. The label is invisible by default, then when the user attempts to process the form a validation check is performed and based on String.IsNullOrEmpty the Visible property should be assigned accordingly. It doesn't seem to work tho...
James
Have you tried placing it in a KeyUp event handler?
Charlie Salts
@Charlie do you mean as a test? As this is not what I want to happen with the label. I want the label to appear when the user attempts to process the form and the field is missing.
James
@Robert, that is not what I want. I want the label to appear when the textbox is empty!
James
And only want it to appear when the user attempts to process the form.
James
What event processes the form?
Robert Harvey
The form is launched as a dialog, when the user clicks an Ok button on the form I have a controller class which calls methods of the form to validate and display the labels based on the text in the textboxes i.e. if they are empty or not. All the code to do with changing the Form UI is done in the code-behind page.
James
You need to repaint the entire form after the code has run, as in this.Refresh or this.Repaint (memory fails at the moment). Just so I'm clear, you are running in Winforms, correct?
Robert Harvey
Your code needs to go in the OK_Click event.
Robert Harvey
@Robert yes using WinForms. Why does the code need to go in the Ok_Click event? Surely as long as the code is triggered the assignment should work?
James
You told me you wanted the behavior to occur when the user attempts to process the form. I assumed that would occur when the user clicked OK.
Robert Harvey
@Robert, As you can see in the above code, the form submission is handled when the dialog ok button is pressed. There should be no need to directly put the validation code behind the button click.
James
Put the visibility code in the same event where you want the action to occur.
Robert Harvey
A: 

I suspect you want to use data binding to set the visibility of the label.
This discussion might help you: http://stackoverflow.com/questions/1049311/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>
Number8
+1  A: 

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.

JaredPar
Ah yeah it worked after that. However, this does not fix my issue. I want the visibility of the label to change after the form is submitted not on the text change. However, my real question is why doesn't my assignment of MyLabel.Visible = String.IsNullOrEmpty(MyTextBox.Text) not work?
James
A: 

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.

Henk Holterman
The timing of the code is not the issue...it is triggered at the correct time. However, as I keep repeating the above assignment does not change the value of Visible. I want to know why.
James
James, we cannot tell what's wrong w/o a more complete sample of your code. My guess was a space in the string but it could be lots of other things. I ran a quick test and it worked as expected.
Henk Holterman
@Henk, I believe Groo figured it out just before you got back. The issue is I am trying to change properties of form components once the form is no longer visible.
James
Groo never posted a reply so I awarded you with the answer :)
James