Forum Discussion

mfoster711's avatar
mfoster711
Regular Contributor
12 years ago

User Form text of selected item

How do you get the text of the currently selected item in a tcxListBox control of a User Form?



I seem to be spinning my wheels and not finding the solution.


  • Hi Mark



    According to this article http://support.smartbear.com/viewarticle/28763/



    The ItemIndex property specifies the index of the currently selected list box item.

    The Items property returns the TStrings which contains the list of strings displayed by the component.



    So this would get the text of the currently selected item

    Set TStringsObj = TcxListBoxObj.Items

    String = TStringsObj.Items(TcxListBoxObj.ItemIndex)


  • paul_scroce's avatar
    paul_scroce
    Frequent Contributor


    Hi Mark



    According to this article http://support.smartbear.com/viewarticle/28763/



    The ItemIndex property specifies the index of the currently selected list box item.

    The Items property returns the TStrings which contains the list of strings displayed by the component.



    So this would get the text of the currently selected item

    Set TStringsObj = TcxListBoxObj.Items

    String = TStringsObj.Items(TcxListBoxObj.ItemIndex)


    • vik33's avatar
      vik33
      Occasional Contributor

      Hello there,

      I am having hard time in fetching the selected item of a TcxListBox control. I am using testcomplete 10.50.XXX version.

       

      This is what i am doing:

      Set obj_lst_Din = UserForms.UserForm_Kroll.lst_DINs  'lst_DINs is the name of the listbox

      var_Selected_Count = obj_lst_Din.SelCount

      For i = 0 To UserForms.UserForm_Kroll.lst_DINs.Items.Count -1
                    If UserForms.UserForm_Kroll.lst_DINs.Selected(i) then
                                     'do something 
                     End if
      Next

       

      1. To my surprise, control never goes inside the If statement when i click on any item of a TcxListBox.

      2. Another thing is(which might be the cause), For some reason, i am unable to trigger OnClick event at all when i click on any item of a TcxListBox

      Instead, the event that triggers is - _OnEnter , so i started writing my code inside _OnEnter event

      3. var_Selected_Count is always 0.

       

      I have no clue why item is not shown as selected at run time when i click on the item of a  TcxListBox.

       

      Please help.

      Thanks,

      • Andrey_M's avatar
        Andrey_M
        Contributor

        Hi vik33.

         

        When you assign obj_lst_Din.SelCount to variable, the actual value (not reference to) of property (.SelCount) gets copied into variable. That means this variable does not reflect future changes of property value, this is expected behaviour.

         

        Here sample of project with list box (unfortunately, current forum engine does not allow to attach zips to post directly, I used my MS OneDrive storage; I will delete this link in couple of weeks). Click handler works perfectly well (I used your cycle in composition of selected items list).

         

        Regards,

        Andrey

  • Hi Mark.



    As far as I know, when the tcxListBox control selected value changes, the Text property get the value of the selected one.



    At least, that is what I do to get the value. But I guess you can get the same value using the index of the selected value.



    Regards, 



    Guillaume.
  • mfoster711's avatar
    mfoster711
    Regular Contributor
    Paul, this worked:


    So this would get the text of the currently selected item


    Set TStringsObj = TcxListBoxObj.Items


    String = TStringsObj.Items(TcxListBoxObj.ItemIndex)



    I was trying to do it on one line which SHOULD look like this:


    String = TcxListBoxObj.Items.Items(TcxListBoxObj.ItemIndex)




    But I was entering it like this:

    String = TcxListBoxObj.Items(TcxListBoxObj.ItemIndex)



    Having Items.Items twice was throwing me off and seems wrong but it works.

  • Ravik's avatar
    Ravik
    Super Contributor
    Hi Marks, Do one thing try code like below


    list = aqObject.GetPropertyValue(pageObject/Listbox),"wItemList"



    items = Split(List,";")



    For each item in Items



    pageObject.Select(listbox).Keys(aqconvert.VarToStr(item))



    pageObject.Click



    Next It will be work without selecting twice value and will not miss any value also.