How to select item in ListBox

Aug 24, 2010 at 2:35 PM

I'm using White 0.20 and trying to test a .NET 3.5 Winforms app. For some reason I can't seem to get a ListBox item selected. I thought it was something like this:


            var workflowList = startWorkflowWindow.Get<ListBox>("_workflowList");


But if I print out the selected item, it's always the first item in the list (the one selected by default).

I've also seen similar issues with example code not working, but I found a way around it. For example, suggests setting the text in a TextBox using the Text property. That would do nothing for me. I had to make a call to SetValue. Is there something I'm doing wrong, or is the documentation just out of date and .Text no longer works?


Aug 24, 2010 at 5:11 PM

Maybe make a check to see if it's selected and if it's not then select it?

Aug 24, 2010 at 5:59 PM

You mean something like:

            var workflowList = startWorkflowWindow.Get<ListBox>("_workflowList");
            if (workflowList.SelectedItemText != "Recursive")

I don't see how that could help.

Aug 26, 2010 at 6:05 AM


you want to select one Item from the Listbox correct?


Try the following



ListItem item = workflowList.Items.Where(p=>p.Name == "Recursive").First();

if(item.Enabled) { if(!item.isSelected) { item.Select } } else { // some Errorhandling here }

if you have automationID of the ListItem =>

ListItem item = workflowList.Items.Where(p=>p.AutomationElement.Current.AutomationId == "<id>").First();


Aug 26, 2010 at 6:53 PM

Thanks, but it doesn't work. Here is my code:

            var workflowList = startWorkflowWindow.Get<ListBox>("_workflowList");
            var item = workflowList.Items.Where(p=>p.Name == "Recursive").First();

                Console.WriteLine("Couldn't select");

            Console.WriteLine("Selected: " + workflowList.SelectedItemText);

And my output is:
Selected:New Address

My ListBox is made up of custom business objects and the DisplayMember is set. It shows the following strings:
New Address
Parcel Processing

Correct me if I'm wrong, but shouldn't workflowList.SelectedItemText return Recursive? And shouldn't I see the item change in my view as this code runs? Does this have something to do with binding to my own objects instead of to a list of strings?



Aug 27, 2010 at 6:15 AM
Edited Aug 27, 2010 at 6:19 AM

Yes it should

this might be an error in White

please try this to verify:

var item = workflowList.Items.Where(p=>p.isSelected).First();
Console.WriteLine("Selected: " + item.Name);