none
How to enable/disable "Click" event handler of button depending on "emptiness"/"fillness" of text box (VB 2008)?

    Question

  • I wanna enable or disable Click event handler of button this way:
    1) if text box is empty, then Click event handler is removed;
    2) if text box is not empty, then Click event hanlder is added.

    Is it possible to use binding between text box and button to control Click event hadling?
    Monday, April 06, 2009 5:30 AM

Answers

  • Hi

    If you just want to do it in XAML, you can try to binding the IsEable of Button to text length in TextBox. you can try this.

    <StackPanel>
        <TextBox x:Name="txt"/>
        <Button Width="150" Height="30" IsEnabled="{Binding ElementName=txt, Path=Text.Length}"/>
    </StackPanel>

    If this isn't acceptable for your case, you might need to add event handler of TextBox to listen the Text change then Remove button click handler.
    Please refer to here to get how to moniter text change.
    http://social.msdn.microsoft.com/forums/en-US/wpf/thread/fb0745f0-6c26-4a9e-b792-3f7e8484b243

    Hope this helps.
    Yiling, MVP(Visual C++)
    Monday, April 06, 2009 6:00 AM
  • Seems like a perfect case for a command to me:
    <Window x:Class="TestProject2.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestProject2"
        Title="Window2" Height="300" Width="300">
        <Grid>
            <DockPanel>
                <Button DockPanel.Dock="Bottom" 
                        Content="Do Something" 
                        Command="local:Window2.DoSomething"/>
                <TextBox x:Name="MainText"/>
            </DockPanel>
        </Grid>
    </Window>
    
    
    
    using System.Windows;
    using System.Windows.Input;
    
    namespace TestProject2
    {
        /// <summary>
        /// Interaction logic for Window2.xaml
        /// </summary>
        public partial class Window2 : Window
        {
            public Window2()
            {
                InitializeComponent();
                InitializeCommands();
            }
    
            /// <summary>
            /// This method establishes command bindings for the class.  It will
            /// typically be called from the class constructor.
            /// </summary>
            private void InitializeCommands()
            {
                CommandBindings.Add(new CommandBinding(Window2.DoSomething, DoSomethingExecuted, CanDoSomething));
            }
    
            #region DoSomething
    
            /// <summary>
            /// The DoSomething command.
            /// </summary>
            public static RoutedUICommand DoSomething
                = new RoutedUICommand("Do Something", "DoSomething", typeof(Window2));
    private void DoSomethingExecuted(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("Please enter some text"); } private void CanDoSomething(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = string.IsNullOrEmpty(this.MainText.Text); } #endregion } }
    The advantage of using a command is that you can easily connect it to any element that supports commands, not just a Button.

    hth,
    Marcel
    • Edited by DutchMarcel Monday, April 06, 2009 12:42 PM Small mistake
    • Marked as answer by Jim Zhou - MSFT Tuesday, April 07, 2009 9:10 AM
    Monday, April 06, 2009 12:40 PM

All replies

  • Hi

    If you just want to do it in XAML, you can try to binding the IsEable of Button to text length in TextBox. you can try this.

    <StackPanel>
        <TextBox x:Name="txt"/>
        <Button Width="150" Height="30" IsEnabled="{Binding ElementName=txt, Path=Text.Length}"/>
    </StackPanel>

    If this isn't acceptable for your case, you might need to add event handler of TextBox to listen the Text change then Remove button click handler.
    Please refer to here to get how to moniter text change.
    http://social.msdn.microsoft.com/forums/en-US/wpf/thread/fb0745f0-6c26-4a9e-b792-3f7e8484b243

    Hope this helps.
    Yiling, MVP(Visual C++)
    Monday, April 06, 2009 6:00 AM
  • Seems like a perfect case for a command to me:
    <Window x:Class="TestProject2.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestProject2"
        Title="Window2" Height="300" Width="300">
        <Grid>
            <DockPanel>
                <Button DockPanel.Dock="Bottom" 
                        Content="Do Something" 
                        Command="local:Window2.DoSomething"/>
                <TextBox x:Name="MainText"/>
            </DockPanel>
        </Grid>
    </Window>
    
    
    
    using System.Windows;
    using System.Windows.Input;
    
    namespace TestProject2
    {
        /// <summary>
        /// Interaction logic for Window2.xaml
        /// </summary>
        public partial class Window2 : Window
        {
            public Window2()
            {
                InitializeComponent();
                InitializeCommands();
            }
    
            /// <summary>
            /// This method establishes command bindings for the class.  It will
            /// typically be called from the class constructor.
            /// </summary>
            private void InitializeCommands()
            {
                CommandBindings.Add(new CommandBinding(Window2.DoSomething, DoSomethingExecuted, CanDoSomething));
            }
    
            #region DoSomething
    
            /// <summary>
            /// The DoSomething command.
            /// </summary>
            public static RoutedUICommand DoSomething
                = new RoutedUICommand("Do Something", "DoSomething", typeof(Window2));
    private void DoSomethingExecuted(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("Please enter some text"); } private void CanDoSomething(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = string.IsNullOrEmpty(this.MainText.Text); } #endregion } }
    The advantage of using a command is that you can easily connect it to any element that supports commands, not just a Button.

    hth,
    Marcel
    • Edited by DutchMarcel Monday, April 06, 2009 12:42 PM Small mistake
    • Marked as answer by Jim Zhou - MSFT Tuesday, April 07, 2009 9:10 AM
    Monday, April 06, 2009 12:40 PM