[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Stop TextChanged events firing

Posted on 2004-04-20
Medium Priority
Last Modified: 2012-05-04

Have converted over a Vb6 application to Vb.Net

From the IntializeComponents rountine added by vb.net, on every initialization of every control it calls the TextChanged or ValueChanged events.

Microsoft recommend creating a property called IsInitializing and the checking this at the start of EVERY TextChanged event

This seems highly inefficient, and overkill.

Is there anyway of removing the InitializeComponents from firing on initialization of every control???
Question by:StuartK
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 25

Assisted Solution

RonaldBiemans earned 100 total points
ID: 10869461
The way Ms suggest is still the best way to go, but you can handle all your textbox.textchanged events in one go

Private Sub textbox_textChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.TextChanged,textbox2.TextChanged,textbox3.TextChanged etc...
   If Form1.DefInstance.IsInitializing = True Then
      Exit Sub
      'Do your thing here
   End If
End Sub

Accepted Solution

tgannetts earned 200 total points
ID: 10869815
Another option would be to use an AddHandler after the InitializeComponents call for the sub handling the TextChanged event, instead of using the Handles command on the sub declaration. This way you can control when you want your sub to start handling the TextChanged event.

So for example...

Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call
        AddHandler TextBox1.TextChanged, AddressOf Me.NameofSub
End Sub


Author Comment

ID: 10870481
First off thanks for both of you replying.

Ronald - That'll reduce the code running, but unfortunately we use most if not all of the TextChanged event to change the state from View to Edit later in the code. (i.e. when form opened it shows data, if any textboxes are typed in, it automatically changes state to Edit - shows the Save button)
1.Some TextChange events do a raw make SaveButton.Enabled = True.
2. Some other do the same plus more.
3.And furthermore some others do above two plus a hell of a lot more.
So could consolidate majority, but not all. I feel that this, in my application, would lose standardisation of TextChanged events against limited gain in speed.
Guess it's an all or nothing.
A good suggestion though. I'll need to assess textchanged events

tgannetts -
Can you possibly elabourate on your suggestion.

The textchanged events will still fire won't they within the InitializeComponents function??

Once again thanks guys
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Assisted Solution

navneet77 earned 200 total points
ID: 10875894
If you do what tgannetts says it means that you do not use the handles keyword when you define the methods.ie normally you have
private sub textbox1_textchanged(sender as object,e as eventargs) handles textbox1.textchanged
end sub

Instead you define the methods as
private sub textbox1_textchanged(sender as object,e as eventargs)
end sub
note there is no handles keyword used and then you manually add a handler after the initialization to the textchanged event using
addhandler textbox1.textchanged, addressof textbox1_textchanged

thus the textchanged event will still be fired but it wont be handled anywhere during initialization since you are adding the handler after initialization.

Expert Comment

ID: 10877593
I think what must be remembered is that the InitializeComponent does not call the TextChanged sub. Rather, the TextChanged sub is called by a separate thread which is created in response to an Event which in turn is raised as a result of something the InitializeComponent has done to the textbox.

The following code is an example of code from the InitializeComponent sub when a TextBox is to be created on a form:

      Me.TextBox1 = New System.Windows.Forms.TextBox()

      Me.TextBox1.Location = New System.Drawing.Point(8, 56)
      Me.TextBox1.Name = "TextBox1"
      Me.TextBox1.Size = New System.Drawing.Size(112, 20)
      Me.TextBox1.TabIndex = 1
      Me.TextBox1.Text = "Please enter text"

As soon as the 'Me.TextBox1 = New System.Windows.Forms.TextBox()' is called, Events will
be raised if the relevant actions, and occur whether you handle them or not.

When the TextBox constructor is called, it sets each of the controls properties to a default setting, so for example the Text property is set to nothing, and I believe the location is set to (0,0). When the InitializeComponent sets these properties to the values you have set through the IDE, an Event could occur for each one because the function changes the values set by the constructor. So a LocationChanged event will occur as the Control's location is changed to (8,56) and a SizeChanged event will occur because the Controls size is set to (112, 20). Depending on the control type, any number of Events could be raised just from the InitializeComponent sub setting the properties of a control to what you have specified in the IDE. If you have specifed an Event Handler for a certain event, the code in that sub will be processed.

In your example, it is most likely that the TextChanged event is being raised due to something the InitializeComponent is doing to the textbox. If you have set a Text Value for the text box, then the event will occur as soon as the [Me.TextBox1.Text = "Please enter text"] line is actioned by the sub. Be aware that a number of events will have been raised already, such as the LocationChanged event, and if you set Event Handlers for these then you can trap them.

The problem you are having is that you only want to trap the TextChanged event caused by a user. The Event Handler however traps all events which can occur as soon as the Constructor has been called. For example, if you have a button which resets the values in all the textboxes to default values, this will cause a TextChanged event to occur for each textbox.

I think therefore your options are either to add logic to the TextChanged sub as RonaldBiemans suggested, or use AddHandler and RemoveHandler to control when you want to trap the event.

Hope this is of some use.



Author Comment

ID: 10880827
Thank you guys,

Great explanation to both of you in respect of handling events.

navneet77 - explained the fact of missing of the "Handles" bit of the Event itself
And then adding it in after InitializeComponents

This means that I can get rid of the IsInitializing = True in all the Text changed events. Extra code that isn't really required after the Initialisation

tgannetts - Great explanation into what the root cause of the InitializeComponents does in firing events. It makes absolute sense.

For the faxt that navneet77 made the penny drop and tgannetts for the original idea I'll split the points.

As for Ronald - just want to recognise that your solution didn't fit with mine, but is still a suitable idea that someone else reading this might relate more to their application.

It's ideal for people that have very similiar TextChanged events or whatever events duplicated accross the application.

Thanks to all guys helping me. It really was much appreciated.


Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question