5

I have a CheckBox and a TextBox. During the runtime,
if the CheckBox is Checked then the TextBox is enabled.

I did it using following code

private void checkTime_Checked(object sender, RoutedEventArgs e)
{
    if (checkTime.IsChecked == true)
    {
        txtTime_SR.IsEnabled = true;
    }
}

What I need to do is, to disable the TextBox when the CheckBox is Unchecked during runtime.

Any idea of doing this ?

codeteq
  • 1,502
  • 7
  • 13
Zarco
  • 177
  • 2
  • 3
  • 13

5 Answers5

11

Reading your post and comments, I'll guess you are doing WPF or silverlight. Then, in that case, you may do it all in XAML :

<CheckBox x:Name="checkTime" />
<TextBox x:Name="txtTime_SR" IsEnabled="{Binding IsChecked, ElementName=checkTime, Converter={StaticResource NotConverter}, Mode=OneWay}"/>

Then, you need to create the converter. This can be done by reading post here : How to bind inverse boolean properties in WPF?

Hope it helps

Community
  • 1
  • 1
Kek
  • 3,145
  • 2
  • 20
  • 26
5

If I understand, you want the textbox to be enabled when the checkbox is checked, and for it to be disabled when the checkbox is unchecked?

private void checkTime_Checked(object sender, RoutedEventArgs e)
{
    txtTime_SR.Enabled = checkTime.Checked;
}

Are you using the standard .NET TextBox and CheckBox controls?

EDIT: Ok, so it is WPF. Do this:

private void checkTime_Checked(object sender, RoutedEventArgs e)
{
    txtTime_SR.IsEnabled = checkTime.IsChecked;
}
Dave New
  • 38,496
  • 59
  • 215
  • 394
  • I'm not well experienced in .Net. For the text box there is no "Enabled" method. There is only "IsEnabled" method. :( – Zarco Oct 30 '12 at 08:40
  • Then try with `IsEnabled` and `IsChecked` - it should work if they aren't read-only properties. – Dave New Oct 30 '12 at 08:42
1

Even simpler

<CheckBox Content="Include the following..." IsChecked="{Binding IncludeMyItems}" Name="checkBox_includeMyItems" />
<TextBox Name="myItems" Text="{Binding MyItems}" Height="23" Width="165" IsEnabled="{Binding IsChecked, ElementName=checkBox_includeMyItems}" />
Ryan Loggerythm
  • 2,877
  • 3
  • 31
  • 39
0
private void checkTime_Checked(object sender, RoutedEventArgs e)
{
 txtTime_SR.Enabled = checkTime.Checked;
}

OR

private void checkTime_Checked(object sender, RoutedEventArgs e)
{
    txtTime_SR.IsEnabled = checkTime.IsChecked;
}
andy
  • 5,979
  • 2
  • 27
  • 49
0

I know this is an old post, however this should work. Use the Checked/Unchecked events.

   private void checkbox_otherflag_Unchecked(object sender, RoutedEventArgs e)
    {
        this.textbox_otherflagtext.IsEnabled = false;
    }

    private void checkbox_otherflag_Checked(object sender, RoutedEventArgs e)
    {
        this.textbox_otherflagtext.IsEnabled = true;
    }
JordanTDN
  • 181
  • 1
  • 12