The comboBox9 gets populated with unique values from an Excel Range with the code below.
comboBox9.Items.Clear();
HashSet<string> distinct = new HashSet<string>();
foreach (_Excel.Range cell in range.Cells)
{
string value = (cell.Value2).ToString();
if (distinct.Add(value))
comboBox9.Items.Add(value);
}
What i tried to do unsuccessfully is set the index of the combobox9 with the first item that satisfies the if condition .
foreach (string item in comboBox9.Items)
{
if (278 * Variables.m3h / (Math.Pow(int.parse(item) / 2, 2) * 3.14) > Variables.maxvelocity)
{
comboBox9.SelectedIndex = comboBox9.Items.IndexOf(item);
}
}
Any ideas on where i'm going wrong ?
All items match the condition will be selected. Because the selection unique, the last selected item is displayed.
When the first item is selected, you need stop the selection :
foreach (int item in comboBox9.Items)
{
if (278 * Variables.m3h / (Math.Pow(item / 2, 2) * 3.14) > Variables.maxvelocity)
{
comboBox9.SelectedIndex = comboBox9.Items.IndexOf(item);
break; // a item is selected, then stop the selection
}
}
I think your code might get an exception of "Specified cast is not valid" in the line of
comboBox9.SelectedIndex = comboBox9.Items.IndexOf(item); because you are adding the items in comboBox9 as "string" type and using "int" type in foreach (int item in comboBox9.Items). So, Please Check your code. I am going to paste 2 different codes below both for int type and string type.
//FOR INT TYPE
void ComboLoadAndSelectIndex()
{
comboBox9.Items.Add(1);
comboBox9.Items.Add(10);
comboBox9.Items.Add(20);
foreach (int item in comboBox9.Items)
{
if (item == 10)//your math Condition
{
comboBox9.SelectedIndex = comboBox9.Items.IndexOf(item);
}
}
}
//FOR String Type
void ComboLoadAndSelectIndex()
{
comboBox9.Items.Add(1.ToString());
comboBox9.Items.Add(10.ToString());
comboBox9.Items.Add(20.ToString());
foreach (string item in comboBox9.Items)
{
if (item == 10.ToString())//your math Condition
{
comboBox9.SelectedIndex = comboBox9.Items.IndexOf(item);
}
}
}
NOTE: Both codes are works fine in Winforms Application. Please check and try it.
Figured it out :
comboBox9.Items.Clear();
HashSet<string> distinct = new HashSet<string>();
foreach (_Excel.Range cell in range.Cells)
{
string value = (cell.Value2).ToString();
if (distinct.Add(value))
comboBox9.Items.Add(value);
}
if (comboBox9.Items.Count > 1)
{
foreach (string item in comboBox9.Items)
{
if (278 * Variables.m3h / (Math.Pow(int.Parse(item) / 2, 2) *
3.14) < Variables.maxvelocity)
{
comboBox9.SelectedIndex = comboBox9.Items.IndexOf(item);
break;
}
}
}
else
{
comboBox9.SelectedIndex = -1;
}
Related
I have to drop down boxes I am trying to select using Selenium the first one works fine however when i try to select the second it does not pick up the values and still uses the first set of elements.
public static void AnfoldComboBox(string sComboBoxId, string sItemText)
{
Drivers.CurrentDriver.FindElement(By.CssSelector($"#{sComboBoxId} + .anfold-combobox .anfold-combobox-toggle.ui-corner-right")).Click();
IWebElement dropDownWrapper = Drivers.CurrentDriver.FindElement(By.ClassName("anfold-combobox-autocomplete"));
ReadOnlyCollection<IWebElement> items = dropDownWrapper.FindElements(By.CssSelector(".ui-menu-item > div"));
foreach (IWebElement item in items)
{
if (item.Text.Trim() == sItemText)
{
item.Click();
break;
}
}
Can you please try below code and check you are getting completer list from dropdown first ?
SelectElement test = new SelectElement(driver.FindElement(By.CssSelector(".ui-menu-item > div")));
IList<IWebElement> size = test.Options;
int myitem = size.Count;
for (int i = 0; i < myitem; i++)
{
String value = test.ElementAt(i).Text;
Console.WriteLine(value);
if (val.Equals(sItemText, StringComparison.InvariantCultureIgnoreCase))
{
val.click();
}
else{
Console.WriteLine("Not present");
}
}
I have a ListView control that I'm filtering results from with a TextBox. The code works for highlighting the backcolors of the matching results, but I want to get the total found results/highlighted objects as an int. The int that is populating now is incorrect and not in line with the found/highlighted results.
How can I get the total number of found/highlighted results?
private void textBox1_TextChanged(object sender, EventArgs e)
{
foreach (ListViewItem lvi in this.browserlistview.Items)
{
if (textBox1.TextLength > 0)
{
if (lvi.Text.IndexOf(textBox1.Text, StringComparison.CurrentCultureIgnoreCase) >= 0)
{
Color b = Color.Cyan;
lvi.BackColor = b;
foundobjlbl.Text = "Objects found: " + lvi.Text.IndexOf(textBox1.Text, StringComparison.InvariantCultureIgnoreCase).ToString();
//this is turning up incorrect integers
}
else
{
Color w = Color.White;
lvi.BackColor = w;
}
}
else if (textBox1.TextLength == 0)
{
Color w = Color.White;
lvi.BackColor = w;
foundobjlbl.Text = "Objects found : 0";
}
}
}
Does anyone see where I'm going wrong?
You're setting your count to the index of the text you're looking for in a specific item. So you're setting it once for each item you've found, so what you'll get is the index of your search term in the last item found.
What you want is to track the count of found items and set it once the search is finished, so something like this (I've left out most of the other implementation details):
var foundCount = 0;
foreach (var item in items)
{
if (IsMatch(item))
{
// set colour
foundCount++;
}
else
{
// set white
}
}
foundobjlbl.Text = $"Objects found {foundCount}";
I am trying to change the 2nd Column of a ListView on a Win-Form.
I know it's a "Sub-Item", but when I am trying to change the Sub-Item (key = 0) it changes the Text of the very first column.
int number = 1;
foreach (ListViewItem existingItem in this.ListViewDokumente.Items) {
if (existingItem.Group.Name == "ListViewGroupSEE") {
existingItem.SubItems(0).Text = number.ToString;
number = number + 1;
}
}
When I change the 0 to 1 in SubItems(0), there's anI get an ArgumentOutOfRangeException.
How can I change the 2nd Column?
Okay, my mistake, and here is what was missing:
ListViewDokumente.Items.Add(item).SubItems.Add("");
The ".Subitems.Add("")" was missing. It needed some "empty spot".
And then, the For Each Loop:
int number = 1;
foreach (ListViewItem existingItem in this.ListViewDokumente.Items) {
if (existingItem.Group.Name == "ListViewGroupSEE") {
existingItem.SubItems(1).Text = number.ToString;
number = number + 1;
}
}
It's "SubItems(1)" this time.
After I do this it's not doing anything that I want to do or otherwise, it writes twice to the file in one line.
The code bellow try to write a value from check box into a text file then find the maximum and minimum value.
List<string> myLs = new List<string>();
int checke = 0;
foreach (ListItem item in coursess.Items)
{
if (item.Selected)
{
checke = checke + 1;
}
}
if (checke < 4)
{
kk.Text = "less";
kk.Visible = true;
}
else if (checke > 6)
{
kk.Text = "More";
kk.Visible = true;
}
else if(checke == 4 || checke == 5 || checke == 6)
{
foreach (ListItem item in coursess.Items)
{
if (item.Selected)
{
myLs.Add(item.Value);
}
}
String datas = String.Join(",", myLs.ToArray());
Based on your code, it looks like a message will be displayed when less than four or more than six checkboxes are checked. As for writing the string to a file, how about something like this at the end...
System.IO.File.WriteAllText(#"C:\selectedcourses.txt", datas);
This should write the entire string, once, to the path that you choose.
I am trying to move items from one list box to another if they are multiple but I am able to move only few, means less than the count. I am not able to implement via for each and for loop as well.
if (AdvLst.SelectedIndex > -1)
{
for (int i = 0; i <= AdvLst.Items.Count - 1; i++)
{
if (AdvLst.Items[i].Selected)
{
string _value = AdvLst.SelectedItem.Value;
string _text = AdvLst.SelectedItem.Text;
ListItem item = new ListItem();
item.Text = _text;
item.Value = _value;
SelectedMortLst.Items.Add(AdvLst.Items[i]);
AdvLst.Items.Remove(AdvLst.Items[i]);
}
}
}
and via foreach loop:
foreach (ListItem li in AdvLst.Items)
{
if (li.Selected == true)
{
SelectedMortLst.Items.Add(AdvLst.SelectedItem);
AdvLst.Items.Remove(AdvLst.SelectedItem);
}
}
Solution 1
var selectedItems = AdvLst.Items.Cast<ListItem>().Where(m => m.Selected).ToArray();
SelectedMortLst.Items.AddRange(selectedItems);
//there's no removeRange, so...
foreach(var item in selectedItems)
AdvLst.Items.Remove(item);
Solution 2 (almost the same)
var selectedItems = AdvLst.Items.Cast<ListItem>().Where(m => m.Selected).ToArray();
foreach(var item in selectedItems) {
SelectedMortLst.Add(item);
AdvLst.Items.Remove(item);
}
Solution 3, for loop code corrected
for (int i = 0; i <= AdvLst.Items.Count - 1; i++)
{
if (AdvLst.Items[i].Selected)
{
string _value = AdvLst.SelectedItem.Value;
string _text = AdvLst.SelectedItem.Text;
ListItem item = new ListItem();
item.Text = _text;
item.Value = _value;
SelectedMortLst.Items.Add(AdvLst.Items[i]);
AdvLst.Items.Remove(AdvLst.Items[i]);
i--;
}
}
cause if you remove an item in the for loop, the count of the collection changes, and the item which is at i+1 place when you remove item has now index i. With i--, your for loop is adapted to that change
Do one operation at a time, either delete or add first.
You may add items to destination list first and removed from the source afterwards
List<ListItem> itemsToDelete=new List<ListItem>();
foreach (ListItem li in AdvLst.Items)
{
if (li.Selected == true)
{
SelectedMortLst.Items.Add(AdvLst.SelectedItem);
itemsToDelete.Add(AdvLst.SelectedItem);
// AdvLst.Items.Remove(AdvLst.SelectedItem);
}
}
foreach(ListItem item in itemsToDelete)
{
AdvLst.Items.Remove(item);
}