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?
Related
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.
I have main menu Called Entries. and it has many submenu items.
base on setting submenu can be set visible = false.
I want to hide Entries Main menu if all its submenus are hidden.
Here I try
int Count = 0;
foreach (ToolStripDropDownItem mnu in entriesToolStripMenuItem.DropDownItems)
{
if (mnu.Visible)
{
Count++;
}
}
if (Count == 0)
{
entriesToolStripMenuItem.Visible = false;
}
but problem is that if (mnu.Visible) always returns false because submenu visible property is false due to Entries main menu not expanded. when I click on Entries then sub menus item's visible property is coming = true.
I want to hide it in form load event.
Try using the Tag property
int Count = 0;
foreach (ToolStripDropDownItem mnu in entriesToolStripMenuItem.DropDownItems)
{
if (mnu.Tag==1)
{
Count++;
}
}
if (Count == 0)
{
entriesToolStripMenuItem.Visible = false;
}
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)
{
}
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.