I have problem with selecting item from ComboBox,
I able to unfold ComboBox but can't select one of the items
Simply
var comboBEle = winDriver.FindElementByName("Combo1");
comboBEle.Click(); // unfold comboBox
winDriver.FindElementByName("Item1").Click(); // don't work
I tried also:
var comboB = sapSession.FindElementByClassName("Combo1");
var selEle= new SelectElement(comboB);
selEle.SelectByIndex(1);
Why winDriver don't see this element?
Related
I have a small project where I get some values from a txt and put inside of a ListView. I need to get the selected value when I click in some item, the first item that I ever select, works fine, but if I try to select again, I get an exception.
This is what I did..
Json = new StreamReader(openDialog.FileName).ReadToEnd();
var ParsedValue = JsonValue.Parse(Json);
Parsed = JsonConvert.DeserializeObject<List<Model>>(ParsedValue.ToString());
foreach (var item in Parsed)
{
var rows = new string[] { item.car, Convert.ToString(item.age )};
var items = new ListViewItem(rows)
{
Tag = item
};
ListViewCars.Items.Add(items);
}
The List view is Filled.
And to get the Item selected from the list :
private void cartsList_SelectedIndexChanged(object sender, EventArgs e)
{
ItemSelected = (Model)ListViewCars.SelectedItems[0].Tag;
}
I can only get the value that I select first when the program runs.
The exception :
System.ArgumentOutOfRangeException: 'InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index'
Check out the documentation on ListView.SelectedIndexChanged. Specifically, look at the Remarks section, which reads:
The SelectedIndices collection changes whenever the Selected property of a ListViewItem changes. The property change can occur programmatically or when the user selects an item or clears the selection of an item. When the user selects an item without pressing CTRL to perform a multiple selection, the control first clears the previous selection. In this case, this event occurs one time for each item that was previously selected and one time for the newly selected item.
I added the emphasis. This means that when you select the second item, the currently selected item is unselected and SelectedIndexChanged is triggered before the new item is selected. So when you try to get the first selected item with ListViewCars.SelectedItems[0].Tag you get that ArgumentOutOfRangeException because there are no selected items.
You need to add a check to the top of your event handler to make sure that there is at least one selected item before accessing SelectedItems[0].
I have a c# menu strip with top-level menu items (TLM items). I am dynamically adding items to one of the TLM items as follows, which works great.
DataRowCollection DRC = DataAccessClass.GetData("SELECT * FROM company ORDER BY CompanyName");
ToolStripMenuItem[] items = new ToolStripMenuItem[DRC.Count];
int itemCounter = 0;
foreach (DataRow dr in DRC)
{
string nm = dr["companyname"].ToString();
int id = Convert.ToInt16(dr["companyid"].ToString());
items[itemCounter] = new ToolStripMenuItem();
items[itemCounter].Name = string.Format("menuitem{0}", itemCounter);
items[itemCounter].Text = nm;
items[itemCounter].Click += new EventHandler(MenuItemClickHandler);
itemCounter++;
}
CompanyToolStripMenuItem.DropDownItems.AddRange(items);
Once this TLM has been populated, I want to dynamically add sub-menu items to each of the dynamic menu items created above. I am similarly creating an array of ToolStripMenuItems as above, and I am trying to add them to a menu item using this, shown here for the first menu item:
CompanyToolStripMenuItem.DropDownItems[0].DropDownItems.AddRange(submenuitems);
But it isn't working. Any ideas?
When I add CompanyToolStripMenuItem.DropDownItems[0] to a watch window, it is showing a "DropDownItems" property. When I try to type it in, the auto-complete drop-down isn't exposing the property as an option.
Try casting the selected DropDownItem item to a ToolStripMenuItem:
((ToolStripMenuItem)CompanyToolStripMenuItem.
DropDownItems[0]).DropDownItems.AddRange(submenuitems);
I have a question regarding use of "Tag" :
I have a ListBox, or ListView, in which I have the name of my objects, I addes a "Tag" property to find its corresponding object :
foreach(Operation op_ass in ListOpAss1)
{
op_ass.getNom(Properties.Settings.Default.Langue);
ListViewItem item = new ListViewItem(op_ass.Nom);
item.Tag = op_ass;
listBoxAss1.Items.Add(op_ass.Nom);
}
Now what I would like, is when I select an item in my list(or several), make an action on corresponding objects. But how can I find them back?
For example I want to remove selected objects from a List, or get the list of Operation ID (without displaying ID in my list).
Looks like you are adding the property, op_ass.Nom into the listbox instead of the ListViewItem, item. Modify your code as follows:
foreach (Operation op_ass in ListOpAss1)
{
op_ass.getNom(Properties.Settings.Default.Langue);
ListViewItem item = new ListViewItem(op_ass.Nom);
item.Tag = op_ass;
// Add the list view item instead of op_ass.Nom
listBoxAss1.Items.Add(item);
}
Now you should be able to retrieve the tag from selected item/items as follows:
var operation = ((listBox1.SelectedItem as ListViewItem).Tag) as Operation;
Alternatively, you could think of using data binding as follows:
foreach (Operation op_ass in ListOpAss1)
{
op_ass.getNom(Properties.Settings.Default.Langue);
}
listBoxAss1.DataSource = ListOpAss1;
listBoxAss1.DisplayMember = "Nom";
And access the data bound object as follows:
var operation = listBox1.SelectedItem as Operation;
using foreach is kind of deprecated you can look into implemented functions in list of objects
ListOpAss1.ForEach(x=>
{
x.getNom(Properties.Settings.Default.Langue);
var item = new ListViewItem(x.Nom);
item.Tag = x;
listBoxAss1.Items.Add(x.Nom);
});
in order to select an item in a list you can use SingleOrDefalt() or Skip(count) take (count) for multiple files or you can run native querys with conditions to search the list like this
var items = collection.Where(x=> x.City == "Burgas").ToList(); //You can use select if you want only certain properties of the object to be selected
///then you can use that new item list to remove the objects from the collection list like this
items.ForEach(x=>
{
collection.Remove(x);
});
I have a group item. Then, each group in group item, i put it into a listview
var Groups = query.GroupBy(query => query.Name);
foreach (var group in Groups)
{
if (group.records.Count() > 2)
{
ListView listview = new ListView();
var itemsource = new ObservableCollection<FileProperties>();
var header = "";
foreach (var item in group.records)
{
header = item.Name;
itemsource.Add(new FileProperties(item.Name, item.Size, item.DateModified, item.Hash, item.Path, item.IsOrigin));
}
listview.ItemsSource = itemsource;
var itemsTemplate = (DataTemplate)this.Resources["Show"];
listview.ItemTemplate = itemsTemplate;
//test is mother listview
test.Items.Add(listview);
}
}
Now, i have a question, how can i update listview UI if i change value in group items without reset mother listview
The default ListView can be grouped by using CollectionViewSource. There is no need to create a child ListView for each group of the parent ListView.
My answer here shows how to create a grouped ListView, you may take a look. Or there are a bunch of demos on internet, you can googling them.
But the most important point here is that by using the default grouped ListView, we can simply create one data collection for the whole ListView, modify the items source collection to update the grouped children automatically, we don't need to create ObservableCollections for each group any more.
I have a CheckedListBox in a WinForms app, that are bound to a LINQ query.
Is it possible to add new items in the checkedListBox? I would like to add the items to the list box and then have a user confirmation before committing them to the DB.
I guess I can have an unbound listbox with items from the DB, but was hoping for a quicker way.
I'm trying:
var avail = from c in dc.CostCenters
select new { Item = c.CostCenterID,
Description = c.CostCenterID + ": " + c.Description };
myList.DataSource = avail;
myList.DisplayMember = "Description";
myList.Items.Add(123, "New Description");
myList.Refresh();
It compiles and runs, but nothing is added in the list box control.