Circular updating of combobox/textbox

This is a discussion on Circular updating of combobox/textbox within the C# Programming forums, part of the General Programming Boards category; Heres a programming challenge for all you .net developers. Before reading my book, please note I've got a solution to ...

Circular updating of combobox/textbox

Heres a programming challenge for all you .net developers. Before reading my book, please note I've got a solution to the following, I'm just seeing if there is a better way

I've got a dropdown combobox and textbox. The combobox contains prefixes for the textbox. A user can select an item from the combobox and have it appear in front of the textbox text, removing any other prefixes from the front. Simple enough, until you start throwing in a textchanged or keypress event.

The combobox selectedindex should also update should the user trigger textchanged or keypress with text containing one of the available prefixes from the combobox. Now its not so simple :/

Updating the textbox when selectedindexchanged triggers also triggers the textchanged, which in turn triggers the selectedindexchanged event again and so forth. See what hapens? Its a continuous cycle.

I can correct this by using a class level variable, and locking down the event handlers code to only occur if its permitted.

I'd rather try another approach, such as putting an optional boolean parameter in one of the event handlers and passing a true condition when the code shouldn't be permitted to run. Problem is, I'm dealing with a combobox here, so doing that wouldn't help, because changing the index to the correct one from selectedindexchanged would just retrigger the event without the true condition of the parameter, and as far as I can see theres no way of setting the condition true for the selectedindexchanged event as I'm changing the combobox selectedindex from the textchanged event of the textbox.

Next I tried checking the sender of the selectedindexchanged event to make sure its not the textbox, but .net doesnt return the textbox though it was the textchanged event of the textbox that causes selectedindexchanged of the combo box to trigger.

Ok, now that I've confused everyone, anyone have any other approach to handling this problem?

Shouldn't you be passing the text that's changing around? If I'm understanding right:

[prefix dropdown] [text box]

I type 't' in the text box, it jumps the prefix dropdown to the first thing with a t. It should be only receiving a 't' from the text box at that point. The prefix then is what ... sending back something that starts with a 't' to the text box? Why? Ok, I don't care why. Even if it is, you need to be keeping track that I really have only typed 't'. So if the prefix sends back 'taco', you need to pay attention to the fact that I really have only typed 't', and as such, not update anything else past that first call, until I actually type something.

Then if I hit 'a', you pass to the prefix list 'ta'. It passes back 'taco' again, and you still keep track of the fact that I've only typed 'ta'.

Shouldn't you be passing the text that's changing around? If I'm understanding right:

[prefix dropdown] [text box]

I type 't' in the text box, it jumps the prefix dropdown to the first thing with a t. It should be only receiving a 't' from the text box at that point. The prefix then is what ... sending back something that starts with a 't' to the text box? Why? Ok, I don't care why. Even if it is, you need to be keeping track that I really have only typed 't'. So if the prefix sends back 'taco', you need to pay attention to the fact that I really have only typed 't', and as such, not update anything else past that first call, until I actually type something.

Then if I hit 'a', you pass to the prefix list 'ta'. It passes back 'taco' again, and you still keep track of the fact that I've only typed 'ta'.

Unless I'm completely not understanding what you're doing here.

Quzah.

That would be for autocomplete (which .net already provides without need for code) if I were to use the default dropdownstyle of a combo box w/ its autocomplete source set to the list of the same combo box.

The combo box dropdownstyle is dropdownlist, so the text property cannot be changed unless it is to an item in the list. The text of the textbox could be anything at all, and the selectedindex would change only if the text began with one of the valid options the user could choose alternitively from the list.

Think of it this way... I'm providing the user the ability to either type in the pre-fix manually in the textbox along with whatever else is required or select it from a combo box. Either way, the user does it, both the combo box and text box must be updated so they match, that is... the combo box having the right prefix selected from the list and the text box's text starting with the prefix.

Though now you mention it, I could use the default dropdown style of the combo box and ditch the textbox completely. The text property could be anything, though doing this means I'd still haft to have a variable (to store the text if the user selects an item from the dropdown so the other text is preserved), unless theres a pre-textchanged event.

I think for now I'll stick with the boolean class variable to lock things down.

I mean, why not a dropdown for the prefix, let the user enter the suffix, and then just put the two together wherever you need it?

It seems easier to have two fields where each is the sole and authoritative source of their piece of the data, versus having this interaction between them wherein the prefix data exists twice (once in the dropdown and once in the textbox) and which source is authoritative depends on which source was most recently modified.

I'll be using the Tag property of one of the controls (probably the combobox) so I don't need a variable now, great

The reason for these requirements is because of a URL field in an application I'm working on. The user needs to be able to type in the protocol manually or select it from a dropdownlist but keep the rest of the url intact.