Thread: Circular updating of combobox/textbox

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    49

    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?
    Last edited by kairozamorro; 05-03-2010 at 09:14 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    49
    Quote Originally Posted by quzah View Post
    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.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    49
    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.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I couldn't make out what you were doing with the prefix and how it related to the text field, so auto-complete was the closest thing I could think of to try to understand what was happening.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Is there a reason the prefix has to be in the textbox at all?

    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.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    49
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rotating or shifting a circular que
    By mrsirpoopsalot in forum C++ Programming
    Replies: 4
    Last Post: 10-22-2009, 12:13 PM
  2. Circular Positioning in C# form application?
    By arcaine01 in forum C# Programming
    Replies: 4
    Last Post: 05-08-2008, 02:31 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. circular shift
    By n00bcodezor in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 03:51 AM
  5. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 08:10 PM