The browse TextBoxes – we’re all familiar when them. A nice clean way of providing lookups attached to a TextBox.

I have a number of these in my existing Vessel Modification and as previously mentioned I want to create a nice seamless user experience – this unfortunately means that we need to replicate this functionality. A quick browse through the SDK DevelopersGuide.pdf I see that they have a section of “Application Resources” and under it there is the styleTextBoxBrowse, described as “Named TextBox style that includes a browse button. The browse button is a small arrow to the right point to the right”. Sounds like us!

So, a minute or two (or a bit longer) later I’ve applied the style to an existing TextBox and I end up with something like this:

A quick skim through the pdf and I can’t find a way to subscribe to the click event of the button. I also have a look through the SDK helpfile – again, can’t find anything useful.

At this point I dig out “Inspect” – it is a tool that is distributed with the Microsoft SDK (I have the 7.1 version). This tool allows you to inspect the visual compontents of running applications.

We can click on a control on an application and it will give us some detailed information – so if I click on the TextBox with the browse style I get this:

We can see the control is indeed a normal TextBox and we can see the Name that I have given it in the AutomationId (tbVessel).

Scrolling further down we see these gems

So we now know that there is no black magic here, there is indeed a button. I’ve posted a question on the http://lawsonsmartoffice.com/ for the ‘proper’ way but figured I’d investigate anyway 🙂

It doesn’t really help us that much at this point, but we can see that the button is a child – so I’d assume that I’d be able to use the VisualTreeHelper technique that I had been using a long time ago to browse the tree of objects to get information.

So I quickly hacked up some code based on the JScript I written and ran it against the user control I had created which has the TextBoxes in it.

“tbPCVoyage” is the textbox that we are looking for the button of.
“BrowseButton” is the name of the button control which we discovered from our output.

I run it and whala! I have found the control!

The issue then becomes knowing when to bind to the control. In the LaunchTask() I add an event to my controls Loaded event. Then I call a method (<vesselcontrol>.SubscribeToBrowseEvents()) to subscribe to the click events on the appropriate browse button.

Hi Scott. I’m a bit confused. Would you like to handle the click event on a button? A click is a routed command. You don’t need to find the button. If you use AddHandler on the textbox or any other parent element you will get the click event.

Ok I can have some typos becuase that is not real code, but that should be it. Try it out! You can have one handler for all buttons if you like :-). Just make sure you remove the handlers as well if you have them in a long lived object.