4

I have a textbox bound to a datasource. The textbox's TextChanged event updates another textbox.

The problem is, I do not want the first textbox to show, so I set its Visible property to false.

However, now the TextChanged event does not fire!

I can work around it by setting Visible=True, Left=-100000 on form load, but I'd like a proper solution.

Can anyone offer an explanation?

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272

4 Answers4

8

Set your textbox.Visible = false in the FormLoad event instead of in the designer. It has to do with handle creation. If the textbox is not visible during construction, then the handle is not created. If the textbox is made invisible after construction, then the handle will have been created and events will occur.

See this discussion on MSDN.

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
msergeant
  • 4,771
  • 3
  • 25
  • 26
3

An alternative solution to the accepted answer is to set up the TextChanged listener on Loaded, this works for me just the same (in Silverlight at least) and keeps the designer view as it should be.

dain
  • 6,475
  • 1
  • 38
  • 47
  • Thanks, this worked for me where my control was inside a pop-up (so wouldn't display until open), and so using the accepted answer had no effect. – Ralt Aug 31 '17 at 09:21
0

What type of datasource is it? It might have an event that you can use directly instead of using a textbox to listen for an update.

Paw Baltzersen
  • 2,662
  • 3
  • 24
  • 33
-1

If Visible is equal to false then the Control is not rendered. Therefore it will be unable to fire an event.

Instead, set the style to display:none. You can set/unset this programmatically using the Attributes collection:

MyTextBox.Attributes.Add("style", "display: none");
David Neale
  • 16,498
  • 6
  • 59
  • 85