Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Succesive IsEnable true/false on textbox breaks its beh... RSS

8 replies

Last post Jan 31, 2009 02:15 PM by Azetoth

(0)
  • Azetoth

    Azetoth

    0 Points

    8 Posts

    Succesive IsEnable true/false on textbox breaks its behaviors.

    Jan 28, 2009 10:35 AM | LINK

    Good day everyone.

    Ok this is the problem and the way to reproduce it. I have two textboxs and one button. The clic event Disable the UserControl and launch a timer of 1 sec.The timer event tick restore the state of UserControl (IsEnable = true)

    After a various number of clic event, textbox are graphicaly enable, but there is no way to enter text anymore. the blincking bar is vivible on textbox (even if it hasen't focus).

     Sorry for my english. I am far better in french ;)

     

     EDIT: I fogot to mention you have to insert some text in textbox between each clic to repoduce the problem (one of the textbox must have the focus when you clic on the button

     

    C# code

     namespace TestBoxEnable
    {
        public partial class Page : UserControl
        {
            DispatcherTimer _timer = new DispatcherTimer();

            public Page()
            {
                InitializeComponent();
            }

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

                _timer = new DispatcherTimer();
                _timer.Interval = new TimeSpan(0, 0, 1);
                _timer.Tick += new EventHandler(_timer_Tick);
                _timer.Start();
            }

            void _timer_Tick(object sender, EventArgs e)
            {
                this.IsEnabled = true;
            }
        }
    }

     Xmal

     <UserControl x:Class="TestBoxEnable.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="400" Height="300">
        <Canvas x:Name="LayoutRoot" Background="White">
            <TextBox Text="Test" x:Name="txt1" Canvas.Left="0" Canvas.Top="0"/>
            <TextBox Text="Test2" x:Name="txt2" Canvas.Left="0" Canvas.Top="25"/>        
            <Button Content="Disable" Click="Button_Click" x:Name="btn" Canvas.Left="0" Canvas.Top="50"/>
        </Canvas>
    </UserControl>

  • Raag80

    Raag80

    Member

    396 Points

    91 Posts

    Re: Succesive IsEnable true/false on textbox breaks its behaviors.

    Jan 28, 2009 11:17 AM | LINK

    hi,

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

              //  _timer = new DispatcherTimer();
                _timer.Interval = new TimeSpan(0, 0, 1);
                _timer.Tick += new EventHandler(_timer_Tick);
                _timer.Start();
            }


     void _timer_Tick(object sender, EventArgs e)
     {
                this.IsEnabled = true;

        //stop the timer here.
        _timer.Stop() // or try ((DispatcherTimer)sender).Stop();
     }

    ****

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Regards

    Raag

    Regards,
    Raag

    If it is Solved your problem then mark as answer bcoz other with same question can benefit.....
  • Azetoth

    Azetoth

    0 Points

    8 Posts

    Re: Succesive IsEnable true/false on textbox breaks its behaviors.

    Jan 28, 2009 11:58 AM | LINK

     Even with a cleaner code like that. the problem still persist...

    I fogot to mention you have to insert some text in textbox between each clic to repoduce the problem (one of the textbox must have the focus when you clic on the button

     

    public partial class Page : UserControl
        {
            DispatcherTimer _timer = new DispatcherTimer();

            public Page()
            {
                InitializeComponent();

                _timer.Interval = new TimeSpan(0, 0, 1);
                _timer.Tick += new EventHandler(_timer_Tick);
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                this.IsEnabled = false;
                _timer.Start();
            }

            void _timer_Tick(object sender, EventArgs e)
            {
                this.IsEnabled = true;
                ((DispatcherTimer)sender).Stop();
            }
        }

  • Raag80

    Raag80

    Member

    396 Points

    91 Posts

    Re: Re: Succesive IsEnable true/false on textbox breaks its behaviors.

    Jan 29, 2009 07:49 AM | LINK

    hi,,,

    Tell me what you want to acheive in txt1 and txt2 on each click...

    Regards,
    Raag

    If it is Solved your problem then mark as answer bcoz other with same question can benefit.....
  • Azetoth

    Azetoth

    0 Points

    8 Posts

    Re: Re: Succesive IsEnable true/false on textbox breaks its behaviors.

    Jan 29, 2009 08:57 AM | LINK

     hi, thanks for you concern.

     The purpose is the the traditionnal login credential dialog...

    So basicaly, I get value from the textbox and send it to a webservice. I Disable the "Dialog" until WS response. (so in the real case it is not a timer event but a completed event and of course it is not two textbox but also a passordbox). There is possibility of an incorrect combination of log/pass. But it will do the same with some kind of  "form" submition. And the strange behavior of textbox fire already on the fourth try.

    Can you reproduce it with the code provided?

    Fred.

  • Patrick8639

    Patrick8639

    Contributor

    2261 Points

    476 Posts

    Re: Re: Succesive IsEnable true/false on textbox breaks its behaviors.

    Jan 29, 2009 07:43 PM | LINK

    Fred,

    the timer event is perhaps not in the UI thread. Have you tried to use Dispatcher.BeginInvoke to be sure that the code that modified the UI is on the UI thread?

    I also had problems with disabling a DatePicker during a server call and re-enabling it after.

    The solution I finally use it to put a Canvas on top of the UI. I set its Width/Height to 0 for normal work and to the size of the plug-in to hide the UI from the mouse. To hide the keyboard, you had to add a UserControl to the Canvas, with the TabNavigation property set to Cycle. When you want to hide the UI, set the focus to the user control. When you receive the response from the server, set the focus to the previous control. Then main page for my application looks like the following:

     

    <UserControl xmlns="http://schemas.microsoft.com/client/2007"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 x:Class="OrdinaSoft.Windows.CMainPage"
                 TabNavigation="Cycle">
    
      <Grid Name="MainGrid">
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
    
        <Canvas Name="HideUICanvas"
                Background="Transparent"
                Height="0"
                Width="0">
          <UserControl Name="HideUIFocusCtrl"
                       TabNavigation="Cycle" />
        </Canvas>
    
      </Grid>
    
    </UserControl>
     Patrick
  • Azetoth

    Azetoth

    0 Points

    8 Posts

    Re: Re: Succesive IsEnable true/false on textbox breaks its behaviors.

    Jan 29, 2009 09:09 PM | LINK

     Thank you Patrick,

    I will try this way, but it is only a workaround.

    I didn't suspected UI thread problem because at the good time of winform, accessing UI by another Thread lauch an exception. I don't understant why it s not the case in silverlight. But that not my concern right now.

    The thing that made me think it is bug. It is that after the problem occur, TextBox are graphicaly enable but not in its behavior (you cannot enter text anymore) The textbox seems to have the focus because there is the blinking cursor on the control(so you can have blinking cursor on Both Textbox witch is ridiculous for the user experience)

    It's realy simple to reproduce (at least on my desk)

    - Clic on the first textbox

    - Insert some random text.

    - Clic on the button

    -(after one second all the control are enable.. focus still on the textbox no need to click again to this control to insert random text)

    -repeat the process 1 more time. (the focus seems leave correctly the textbox the first time when you clic the button but not the second. Cursor still blink even in IsEnable=false!)

    And after  some test, there is realy a probleme with focus...

    During the button clic event, I use UIElement element = (UIElement) FocusManager.GetFocusedElement();

    The first time element type is button. but not the second time!!! element type is textbox.

     

    Fred.


     

  • Patrick8639

    Patrick8639

    Contributor

    2261 Points

    476 Posts

    Re: Re: Succesive IsEnable true/false on textbox breaks its behaviors.

    Jan 30, 2009 04:47 AM | LINK

    Azetoth

    I didn't suspected UI thread problem because at the good time of winform, accessing UI by another Thread lauch an exception. I don't understant why it s not the case in silverlight. But that not my concern right now.

     

    Fred,

    In the case of my DatePicker, I also receive no exception for setting IsEnabled from another thread (the setting was on the UserControl at the top of the UI). The bug was there!

    The DatePicker simply no more answer to keyboard events after that.

    Patrick

  • Azetoth

    Azetoth

    0 Points

    8 Posts

    Re: Re: Succesive IsEnable true/false on textbox breaks its behaviors.

    Jan 31, 2009 02:15 PM | LINK

    Patrick,

    The UI access is on the UI Thread (with the use of Dispatcher.BenginInvoke) doesn't resolve the issue.

    But thank you very much, I ll try to use your workaround. And hope Silverlight team resolved this bug (that of course doesn't show up on WPF) for v3 release.

    Fred.

     

    My new code with accesUI on the same Thread with the sme problem.

     public Page()
            {
                InitializeComponent();

                _timer.Interval = new TimeSpan(0, 0, 1);
                _timer.Tick += new EventHandler(_timer_Tick);
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                UIElement element = (UIElement)FocusManager.GetFocusedElement();
                txt1.IsEnabled = false;
                txt2.IsEnabled = false;
                btn.IsEnabled = false;
                _timer.Start();
            }

            void _timer_Tick(object sender, EventArgs e)
            {
                Dispatcher.BeginInvoke(delegate()
                {
                    txt1.IsEnabled = true;
                    txt2.IsEnabled = true;
                    btn.IsEnabled = true;
                    ((DispatcherTimer)sender).Stop();
                });

            }