locked
Uneventful RichTextBox TextChanged Visible False Bug

    Question

  • Hi All,

     

    On Form1 I have RichTextBox1 that is initially hidden and WebBrowser1 that is initially visible when the form loads. I have attached the TextChanged event to RichTextBox1. I have a couple Radio Buttons to toggle visibility.

     

    What should happen is that when I programmatically change RichTextBox1.Text, its TextChanged event should fire which in turn updates WebBrowswer1.DocumentText.

     

    The problem: The TextChanged event doesn't fire when changing its .Text after Form1 loads. If I then swap the visibility, then swap it back again using the Radio Buttons, then programmatically change RichTextBox1.Text again, the TextChanged event now fires as expected. Note that RichTextBox1 is hidden when the event fires.

     

    Why is the TextChanged event not firing until I have made it visible?

     

    Here's my workaround:

     

    Code Snippet
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            RichTextBox1.Visible = True
            RichTextBox1.Visible = False
        End Sub

    Wednesday, February 13, 2008 7:27 PM

Answers

  • Hi All,

    I don't think that it is a bug anyway. This issue relates whether richtextbox's handle is created. If control handle isn't created, when we can modify text property, it doesn't trigger TextChanged event. You can use RichTextBox1.IsHandleCreated property to know whether the control handle is created or not. If the handle isn't created, the messgae willn't be delivered to the control. Then control doesn't trigger the event to let us handle. When you set visible to false in ide, the handle isn't created(because this is called in form's constructor). When you set visible to false in load event, the handle is created. Now control can receive messgage from windows. I think that it is the difference between two scenorios.

    Best regards,
    Riquel
    Tuesday, February 19, 2008 3:24 AM
    Moderator
  • Hi Riquel

     

    I had thouhg it might be something like that, however a normal textbox does not behave in this way.  So although it may not be a bug it is certainly inconsistent.

    Tuesday, February 19, 2008 1:10 PM

All replies

  •  Tim Mawr wrote:

    What should happen is that when I programmatically change RichTextBox1.Text, its TextChanged event should fire which in turn updates WebBrowswer1.DocumentText.

     

    The problem: The TextChanged event doesn't fire when changing its .Text after Form1 loads. If I then swap the visibility, then swap it back again using the Radio Buttons, then programmatically change RichTextBox1.Text again, the TextChanged event now fires as expected. Note that RichTextBox1 is hidden when the event fires.

     

    Why is the TextChanged event not firing until I have made it visible?

     

    Hi Tim,

     

    I make the following experiment, and the RichTextBox1_TextChanged event can be fired no matter what is RichTextBox's visibility. I cannot repro the issue, please double check.

     

    Prerequisites: RichTextBox1, RadioButton1, RadioButton2 and Button1 on Form1.

    Code Snippet

    Public Class Form1

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            RichTextBox1.Visible = False

        End Sub

       

        Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged

            RichTextBox1.Visible = False

        End Sub

     

        Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged

            RichTextBox1.Visible = True

        End Sub

     

        'The event can be fired no matter what is RichTextBox's visibility

        Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

            Me.Text = RichTextBox1.Text ' Form Title gets changed.

        End Sub

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            ' programmatically change RichTextBox1.Text

            RichTextBox1.Text &= "Change RichTextBox.Text"

        End Sub

     

    End Class

     

     

    If I misunderstood you, please let me know.

     

    Regards,

    Martin

    Friday, February 15, 2008 7:50 AM
  • Thanks Martin,

     

    I've solved the problem now.

     

    I was changing the DocumentText of the browser in the CheckChanged event, but without waiting for the subsequent DocumentCompleted event before access an element with GetElementById. This was raising an exception that was caught by an empty catch block. Doh! So the code to make the RichTextBox visible was never excuted and neither was there a MessageBox nor Debug.Print to flag it.

     

    Code Snippet
            Try
                ' Dodgy code
            Catch ex As Exception
                ' Sweep problem under the carpet
            End Try


     

    Check it out the programme here. Let me know what you think:

    http://www.lavernockenterprises.co.uk/downloads/TidyTag/TidyTag.htm

     

    Friday, February 15, 2008 9:44 AM
  • Martin, Tim

     

    I also looked at this and had no difficulty reproducing the fault.

     

    In a new project add a richtextbox and a button.  Make the textbox invisible in the designer.  Add the following code.

     

    Public Class Form1
        Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
            Me.Text = "Textbox text changed"
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            RichTextBox1.Text = "Something"
            RichTextBox1.Visible = True
        End Sub
    End
    Class

     

     

    When the button is clicked the text changes but the event doesn't fire.

     

    It does this in both VBE2005 and 2008.  Looks like a bug to me.

    Friday, February 15, 2008 12:39 PM
  • Hi Dave, Martin,

     

    I've had a further look at this problem and it does seem like a bug. When I fixed the errors in my code, I restructed it quite a bit so the event firing bug didn't manifest itself. I've also tried it out in VC++ and it generated the same problem, i.e. when richTextBox1 is made invisible in the IDE (constructor) and then when Button1 is clicked for the first time, the Text changes but the TextChanged event doesn't fire. On subsequent clicks the TextChanged event fires.

     

    Code Snippet
        private:
            System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
            {
                richTextBox1->Text =
    "Something";
                richTextBox1->Visible =
    true;
            }

       
    private:
            System::Void richTextBox1_TextChanged(System::Object^  sender, System::EventArgs^  e)
            {
                MessageBox::Show (
    "richTextBox1_TextChanged");
            }


     

    Fortunately the workaround is straight forward; make it invisible during Load instead of in the IDE (constructor).

     

    Code Snippet
        private:
            System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
            {
               
    this->richTextBox1->Visible = false;
            }


     

    Saturday, February 16, 2008 8:33 AM
  • Hi All,

    I don't think that it is a bug anyway. This issue relates whether richtextbox's handle is created. If control handle isn't created, when we can modify text property, it doesn't trigger TextChanged event. You can use RichTextBox1.IsHandleCreated property to know whether the control handle is created or not. If the handle isn't created, the messgae willn't be delivered to the control. Then control doesn't trigger the event to let us handle. When you set visible to false in ide, the handle isn't created(because this is called in form's constructor). When you set visible to false in load event, the handle is created. Now control can receive messgage from windows. I think that it is the difference between two scenorios.

    Best regards,
    Riquel
    Tuesday, February 19, 2008 3:24 AM
    Moderator
  • Hi Riquel

     

    I had thouhg it might be something like that, however a normal textbox does not behave in this way.  So although it may not be a bug it is certainly inconsistent.

    Tuesday, February 19, 2008 1:10 PM
  • I realize this is some awful thread necromancy, but I just encountered the same issue and wanted to confirm that it does, in fact, happen with labels as well.  I have a label whose text value is bound to a BindingSource, such that it will change when the Current item in the bindingsource changes (via a DataGridView).  If the label is made !Visible in Designer, its textChanged event does *not* fire off when the selected item on the DataGridView (and therefore the text of the label) changes.

    Friday, June 13, 2008 9:24 PM