Forum Discussion

larolsen's avatar
larolsen
Contributor
14 years ago

Problem with getting items from WPF combobox

Hi



I am trying to make testscripts for a WPF application. I would like to make a helper function, that receives a combobox object, and a text. If the text exists in the combobox items, it should select it. If the text does not exist it should return a False flag.



The text in the combobox is stored in a TextBlock object within the Combobox hierarchy, and the TextBlock has a property called "Text"



This is the code have so far:





  Dim MyString

  Dim L_astrPropNames1

  Dim L_astrValueNames1

   

  ReDim L_astrPropNames1(0)

  L_astrPropNames1(0) = "Text"

   

  ReDim L_astrPropValues1(0)

  L_astrPropValues1(0) = "1800-3200 Hz"

 

  MyString = mycombobox.FindAllChildren(L_astrPropNames1,L_astrPropValues1, 1, False) 



The problem with this is that it only finds the text of the currently shown item in the combobox. So if "1800-3200 Hz" is not shown in the combobox, it returns nothing.



I tried preceding the FindAllChildren with a ComboBox.Click, but it rolls up immediately before the FindAllChildren is executed.



Does anyone have any ideas on how I could make this work? Please let me know if you need more information



Best regards

Lars Lund Olsen

Denmark
  • Hi Lars,



    You can use the wItemList property to obtain combo-box items. The following script demonstrates this approach:





    Function SelectItemByText(ComboBox, Text)

    Dim p, w, i, ItemString, Del, Item



      ItemString = ComboBox.wItemList

      Del = ComboBox.wListSeparator

      aqString.ListSeparator = Del

     

      for i = 0 to ComboBox.wItemCount-1

        Item = aqString.GetListItem(ItemString, i)

        If Item = Text Then

        

        ComboBox.ClickItem(Item)

        SelectItemByText = True

        Exit For

        Else SelectItemByText = False

        End If

      next

    End Function



    '...

    Usage:

    '...

    Set combo = ... 'Specify the target combo box's object reference

    Text = "MyItemText"

    res = SelectItemByText(combo, Text)

    If not res Then

    Log.Message("The "+Text+" item was not found")

    End If

    '...





    I recommend that you refer to "Working With Combo Box Controls" and related help topics for more information.
  • Hi and thanks for your suggestion. I tried it, but unfortunately I can't get it to work on my application.



    ItemString = ComboBox.wItemList gives med a series of empty strings, seperated by the wListSeperator character ( | | | | )



    What info can I post to help find a solution?



    Best regards

    Lars Lund Olsen
  • Hi,



    Please tell me the target control's name, version, vendor and the value of the ClrFullClassName property. Also, it would be very helpful if you provided us with a simple application demonstrating the control, so we can try to create a sample script for you.
  • Hi



    ClrFullClassName is System.Windows.Controls.ComboBox



    the control is mapped as ...  .WPFObject("ContentControl", "", 1).WPFObject("DefaultFrame", "", 1).WPFObject("ContentControl", "", 1).WPFObject("NormalizingColumnsLayoutView", "", 1).WPFObject("MiddleContent").WPFObject("ContentControl", "", 1).WPFObject("FeatureGroupContainer", "", 1).WPFObject("EmptyFrame", "", 1).WPFObject("ContentControl", "", 1).WPFObject("CompressingLayoutView", "", 1).WPFObject("ItemsControl", "", 1).WPFObject("ConfigView", "", 1).WPFObject("ContentControl", "", 4).WPFObject("comboBox")



    the application is in a very early stage of development, and I cannot send it to you, nor can I make a sample application. This is partly because of confidentiality, and partly because I'm in the software test group, and the software is not being developed in-house....



    I think the actual values are stored in TextBlocks inside the combobox, but I cannot access them when the combobox is rolled up.



    Best regards

    Lars Lund Olsen






  • Hi Lars,



    It looks as if the target control is owner-drawn. When testing owner-drawn combo boxes, you need to specify combo-box items using their indexes. However, you should be able to retrieve the combo-box items via the native Items collection:





    Function SelectOwnerDrawnItem(ComboBox, Text)

     For i = 0 to ComboBox.Items.Count-1

       Set Item = ComboBox.Items.Item(i)

       If Item.OleValue = Text Then

         ComboBox.ClickItem(i)

         SelectOwnerDrawnItem = True

         Exit For

       Else SelectOwnerDrawnItem = False

       End If

       

     Next

     

    '...

    'Usage:

    '...

    Set combo = ... 'Specify the target combo box's object reference

    Text = "MyItemText"

    res = SelectOwnerDrawnItem(combo, Text)

    If not res Then

    Log.Message("The "+Text+" item was not found")

    End If

    '...





    I hope this helps.
  • Hi again



    Unfortunately the code did quite do it. The item I get returned is a ValuePresenter type, but after talking with our developers, I found a property within the ValuePresenter I could use. So thanks for you help, you got me on the right track :-)



    Best regards

    Lars Lund Olsen
  • rajforu2k6's avatar
    rajforu2k6
    Occasional Contributor
    Hi there,



    I am also facing similar situation, Can any one please suggest how to proceed ? I have already tried below method but it didn't work.




    Function SelectOwnerDrawnItem(ComboBox, Text)


     For i = 0 to ComboBox.Items.Count-1


       Set Item = ComboBox.Items.Item(i)


       If Item.OleValue = Text Then


         ComboBox.ClickItem(i)


         SelectOwnerDrawnItem = True


         Exit For


       Else SelectOwnerDrawnItem = False


       End If



    Regards,

    Raj