Selecting an item in listbox programmatically - InfoPath Dev
in

InfoPath Dev

Selecting an item in listbox programmatically

Last post 10-27-2009 09:01 AM by francofs. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 10-26-2009 05:44 AM

    Selecting an item in listbox programmatically

    Hi, I'm trying to select an item in a listBox programmatically. The idea is that I want to search and display and item, that is in a listbox.

    I couldn't find anywhere how to do it. Is it possible? This is my code:

            public void btnSearchProd_Clicked(object sender, ClickedEventArgs e)
            {
                XPathNavigator domNav = MainDataSource.CreateNavigator();
                XPathNavigator paramItemNav = domNav.SelectSingleNode("/my:meusCampos/my:txtBoxes/my:myProductDesc", NamespaceManager); //Field that cointains the search argument
                XPathNodeIterator listItemNav = domNav.Select("/my:meusCampos/my:rightList/my:rightListItem", NamespaceManager);
                XPathNavigator listNav = domNav.SelectSingleNode("/my:meusCampos/my:rightList", NamespaceManager);

                //Cycle through all items in the list
                while (listItemNav.MoveNext())
                {
                    if (listItemNav.Current.Value.LastIndexOf(paramItemNav.Value) != -1)
                    {

                        // OK, found the item, now want to select it, so the user can see it. Tried several things, but nothing worked
                        XPathNavigator newNode = listItemNav.Current.Clone();

                        //What do I do here??????????
                        //newNode.InsertAfter(newNode);
                        
                        //listNav.AppendChild(newNode);
                        //listNav.MoveTo(listItemNav.Current);
                        //listNav.PrependChild(listItemNav.Current);
                       
                        //newNode.MoveTo(newNode);
                       
                        //newNode.SetValue(listItemNav.Current.Value);
                        //listNav.SetValue(newNode.Value);
                        //listNav.AppendChild(newNode);
                        break;
                    }
                   
                }

     

     Thanks in advance for any help.

    Regards,

    Fábio

  • 10-26-2009 08:24 AM In reply to

    Re: Selecting an item in listbox programmatically

    You should be able to do:

    XPathNavigator listBoxField = domNav.SelectSingleNode(path to node bound to listbox, NamespaceManager);

    listBoxField.SetValue(listItemNav.Current.Value);

    And you can remove the Clone() statement.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 10-26-2009 09:48 AM In reply to

    Re: Selecting an item in listbox programmatically

     Hi! Thank you very much for your quick response. It worked, but in a weird way. This is my code now:

            public void btnSearchProd_Clicked(object sender, ClickedEventArgs e)
            {
                // Retrieve value of the selected item in right list
                XPathNavigator domNav = MainDataSource.CreateNavigator();
                XPathNavigator paramItemNav = domNav.SelectSingleNode("/my:meusCampos/my:txtBoxes/my:myProductDesc", NamespaceManager);
                XPathNodeIterator listItemNav = domNav.Select("/my:meusCampos/my:rightList/my:rightListItem", NamespaceManager);
                XPathNavigator selectedItemNav = domNav.SelectSingleNode("/my:meusCampos/my:rightListSelectedItem", NamespaceManager);

                while (listItemNav.MoveNext())
                {
                    if (listItemNav.Current.Value.LastIndexOf(paramItemNav.Value) != -1)
                    {
                        selectedItemNav.SetValue(listItemNav.Current.Value);
                        break;
                    }
                   
                }
            }

    However, The item gets duplicated (disappears if I change the selection) and copied to the top of the list. Is this supposed to happen? I thought the listview would scroll down to the item and select it. But that doesn't happen, instead this copy is made and selected on top of the list. Any advice?

     Thank you.

    Regards,

    Fábio

  • 10-26-2009 10:05 AM In reply to

    Re: Selecting an item in listbox programmatically

    It sounds like my:rightListItem has the display values for the listbox and the actual values are in some other nearby field.  Am I right?

    If so, you need to set selectedItemNav to the "value value" that corresponds to the display value that matches your search.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 10-26-2009 11:21 AM In reply to

    Re: Selecting an item in listbox programmatically

     

    Jimmy:

    It sounds like my:rightListItem has the display values for the listbox and the actual values are in some other nearby field.  Am I right?

    If so, you need to set selectedItemNav to the "value value" that corresponds to the display value that matches your search.

     

    Sorry, didn't really get what you mean. Please take a look on how it is here:

    InfoPathSS 

     

    Could please clarify? Thank you again for your help, and sorry if didn't provide more details.

    Regards,

    Fábio

  • 10-27-2009 08:16 AM In reply to

    Re: Selecting an item in listbox programmatically

    Your screenshot shows that each rightListItem node has two children: rightListItemName and rightListItemValue.  I'm assuming that rightListItemName are the values that are displayed in the list box (the values the user can see), and rightListItemValue are the values "behind" the list box.

    When you find the item you're looking for, you need to assign the corresponding rightListItemValue value to rightListSelectedItem.  You can access this node with something like this in your for loop when you find the match you're looking for:

    XPathNavigator itemValueNode = listItemNav.Current.SelectSingleNode("my:rightListItemValue", NamespaceManager);
    selectedItemNav.SetValue(itemValueNode.Value);

     Please ensure that these rightListItemValue nodes actually have values and aren't blank.  From your description it sounds like they might be.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 10-27-2009 09:01 AM In reply to

    Re: Selecting an item in listbox programmatically

     Thanks! Worked like a charm. I figured out what you were saying afterwards, but I wasn't sure on how to access the value of the value of listItemNav.Current.

    This settles it. Thank you very much for your effort and patience. I really appreciate it.

    Regards,

    Fábio

     

    P.S.: This forum doesn't have a "Mark as Answer" thing right?

Page 1 of 1 (7 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.