I have an object which is defined as Pane in FlaUInspect with a number of Checkboxes loaded dynamically when the program starts. For a Unit Test I need to loop through all the checkBoxes and find one particular item based upon a string. The code below was first attempt to load the item which does load but it doesn't list all the checkboxes in the Pane.
ListBox seqPanelItems = databaseWindow.FindFirstDescendant(cf => cf.ByAutomationId("sequenceScrollViewer")).AsListBox();
var rdctSeqCheckBox = GetSeqPanelCheckbox(seqPanelItems, "RDCT");
The following code is what loops through the items.
private CheckBox GetSeqPanelCheckbox(ListBox items, string name)
{
for (int i = 0; i < items.Items.Length; i += 1)
{
//if (items[i] is not Label)
//{
// continue;
//}
if (items.Items[i].Name == name)
{
return items.Items[i - 1].AsCheckBox();
}
}
return null;
}
As the code indicates, it needs to find the particular item and return it as a checkbox item but items.Items.Length returns 0.
Below is what FlaUInspect shows.
I am considering this problem closed. I moved the CheckBox and Text to a list box.
Related
I have class say class A and i have cintialised a list (I am using HashSet) in the costructor so as to have its access throught out the program.
I Add the item in the list inside the Loaded event of comboBox. Ans i use that saved list after the loaded event I found that it do not contain the data i added.
Is this thing normal with loaded event ? Could some one please tell me way to save data added inside the List (I am using HashSet) in loaded event?
My code is :
static HashSet < string > listOfUpdatedUIElement = new HashSet < string > ();
static HashSet < string > storeUpdatedUIElement = new HashSet < string > ();
//This in constructor
GenerateParametersPreview()
{
storeUpdatedUIElement = null;
}
public Grid simeFunction() {
ComboBox cmb = new ComboBox();
cmb.Loaded += (o3, e) => {
foreach(string atrb in listOfUpdatedUIElement) //I have seen on debugging the data are updated in listOfUpdatedUIElement
{
storeUpdatedUIElement.Add(atrb);
}
};
foreach(string atrb in storeUpdatedUIElement) //Here storeUpdatedUIElement hashset contains nothing inside
{
cmb.Items.Add(atrb);
}
Grid.SetColumn(cmb, 1);
comboRowGrid.Children.Add(cmb);
Grid.SetRow(comboRowGrid, 0);
bigGrid.Children.Add(comboRowGrid); //suppose ihad created this bigGrid and it will dispaly my comboBox
return (bigGrid);
}
The events are the main tool of the Event-driven-programming paradigm.
In event driven programming you are not sure when and whether at all some condition changes (like some ComboBox is finally loaded or not), you are reacting to the notification about that change - raised event.
It means that
cmb.Loaded += (o3, e) =>
{
foreach(string atrb in listOfUpdatedUIElement)//I have seen on debugging the data are updated in listOfUpdatedUIElement
{
storeUpdatedUIElement.Add(atrb);
}
};
won't(at least it is hardly possible) execute before the
foreach(string atrb in storeUpdatedUIElement) //Here storeUpdatedUIElement hashset contains nothing inside
{
cmb.Items.Add(atrb);
}
That's why the storeUpdatedUIElement is empty when the loop enumerates it.
SOLUTION:
So, if you want to update your ComboBox items on Loaded event you should put all the relevant code inside the event:
cmb.Loaded += (o3, e) =>
{
foreach(string atrb in listOfUpdatedUIElement)//I have seen on debugging the data are updated in listOfUpdatedUIElement
{
storeUpdatedUIElement.Add(atrb);
}
foreach(string atrb in storeUpdatedUIElement) //Here storeUpdatedUIElement hashset contains nothing inside
{
cmb.Items.Add(atrb);
}
};
P.S.: In such a case you should probably unite those two loops in one:
foreach(string atrb in listOfUpdatedUIElement)//I have seen on debugging the data are updated in listOfUpdatedUIElement
{
storeUpdatedUIElement.Add(atrb); // Remove it too if it is not used anywhere else
cmb.Items.Add(atrb);
}
simeFunction is working using a new new combobox that is never added to your form.
You are expecting your list to be populated when this combobox is loaded and since it is never added to your form, it never will be loaded.
lblSelected.Text = string.Empty; // empty the label that says you already have an item
foreach (GridViewRow row in gvSnacktastic.Rows) // check all the items in the grid view
{
CheckBox checkItOut = row.Cells[0].Controls[0] as CheckBox; // get the checkbox
if (checkItOut != null && checkItOut.Checked) // if the checkbox exists and is checked
{
bool storeVariable = true; // store a variable that tells us if we need to add it to the list
**foreach(ListBoxItem listItem in lbSelected.Items)** // Loop through list box items to see if we already have it. i haven't used listbox in a long time, this might be slightly wrong
{
// I'm not sure if it's .Text - compare the text of the listbox item to the checkbox item description
if(listItem.Text== checkItOut.Text)
{
lblSelected.Text = "The Item " + listItem.Text + " has already been added."; // make our already have it label
storeVariable = false; // remember that we don't need to add this item
}
}
if(storeVariable) // if we do need to add this item
{
lbSelected.Items.Add(checkItOut.Text); // create a new list box item with the check box item's description - this code is not complete
}
}
}
}
The highlighted (**) area is reading with an error as I am debugging. Any idea what I'm doing wrong?
I'm sure this is a simple question to answer, but I am not finding the right information online.
Please Change
foreach(ListBoxItem listItem in lbSelected.Items)
{
}
TO
foreach(ListItem listItem in lbSelected.Items)
{
}
I have a ListView with MultiSelect = false, View = Details and CheckBoxes = True. I'm stepping through it and controlling visibility in an application. I'm currently just using the Else portion of the code below. But it doesn't account for the first Item being selected, it just turns the second item on. And whether or not the item is checked (already visible), and it turns the visibility off. I'm comparing elements associated with the items against elements that are already visible. My app crashes at the currentItem.Checked loop. And doesn't account for combinations (first and checked). How could I code this?
int indexCount = listView1.Items.Count;
ListViewItem currentItem = listView1.SelectedItems[0];
int currentIndex = currentItem.Index;
if (currentItem.Index == 0)
{
//listView1.SelectedItems[0] on
}
if (currentItem.Index == indexCount)
{
//end
}
if (currentItem.Checked == true)
{
while (currentItem.Checked == true)
{
listView1.SelectedIndices.Clear();
listView1.SelectedIndices.Add(currentIndex + 1);
}
//listView1.SelectedItems[0] on
}
else
{
//listView1.SelectedItems[0] off
listView1.SelectedIndices.Clear();
listView1.SelectedIndices.Add(currentIndex + 1);
//listView1.SelectedItems[0] on
}
It is not clear what you are trying to do.
You are basically picking one selected item and placing it in 'currentItem'. If that item is checked as true you are looping through it until? and why?
C# winforms.
I have a listview, and for each item clicked I show its properties in labels and textboxes.
They are not binded in any way, I do manually.
So I change values in textboxes (via next and previous record buttons) and the listview.selected doesn't change.
I've done this my way, but I think maybe there's some optimization or even a single method to do this. I'm seeking something like selected=itemwithkey(idTextBox)
for (int i = 0; i < lstvClientes.Items.Count; i++) {
if (lstvClientes.Items[i].SubItems[0].Text == idTextBox.Text) {
lstvClientes.Items[i].Selected = true; break;
}
}
Suggestions?
thank you community.
You can use ListView.FindItemWithText method:
var item = lstvClientes.FindItemWithText(idTextBox.Text);
if (item != null)
item.Selected = true;
In the constructor i did:
if (listBox1.Items != null)
{
listBox1.Focus();
}
But when im running the program i cant move with the keyboards up down in listBox since the focus is on a button somewhere else in the Form. I need to click with the mouse on the listBox to get the focus.
Another problem i want that when the user add a new item to the listBox the focus will be automatic on the last added item. For this problem this is the code where im adding a new item to the listBox:
private void KeysValuesUpdate()
{
using (var w = new StreamWriter(keywords_path_file))
{
crawlLocaly1 = new CrawlLocaly();
crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
DialogResult dr = crawlLocaly1.ShowDialog(this);
if (dr == DialogResult.OK)
{
if (LocalyKeyWords.ContainsKey(mainUrl))
{
LocalyKeyWords[mainUrl].Clear();
LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
}
else
{
LocalyKeyWords[mainUrl] = new List<string>();
LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
}
Write(w);
ClearListBox();
}
if (dr == DialogResult.Cancel)
{
Write(w);
}
}
}
private void ClearListBox()
{
data.Clear();
listBox1.DataSource = null;
string sb;
foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
{
for (int i = 0; i < kvp.Value.Count(); i++)
{
sb = "Url: " + kvp.Key + " --- " + "Local KeyWord: " + kvp.Value[i] + Environment.NewLine;
data.Add(sb.ToString());
}
}
listBox1.DataSource = data;
}
The question is why i cant set the focus in any of the cases on the listBox items ?
In the first case in the constructor the focus i want it to be on the last item in the list and also each time im adding a new item so the focus will be on the last added item.
Most likely, the item is being selected, you just can't tell because a different control has the focus. There are a couple of different ways that you can solve this, depending on the design of your application.
For the first part of the question, you should set the Focus in the Page/Form Load event, since at the constructor level controls are under initialization process.
Set the focus to the ListView first whenever your form is displayed. The user typically sets focus to controls by clicking on them. However, you can also specify which controls gets the focus programmatically. One way of doing this is by setting the tab index of the control to 0 (the lowest value indicates the control that will have the initial focus). A second possibility is to use the following line of code in your form's Load event, or immediately after you set the Selected property:
listBox1.Select();
The problem with this solution is that the selected item will no longer appear highlighted when the user sets focus to a different control on your form (such as a textbox or a button).
For the second part of the question, selecting last added item in the ListBox, use the following code:
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.SetFocus();
Looks like your ClearListBox method is actually a UpdateListBox method.
listBox1.DataSource = data;
listBox1.SelectedIndex = <index of newitem>;
// or
listBox1.SelectedItem = "text of new item";
listBox1.SetFocus();
If the new item is the last item, its index is listBox1.Items.Count - 1.