How to select items in a ListView? - c#

This is my code:
for (int i = 0; i < 30; i++)
{
FileListView.Items.Add(new ListViewItem(new[] { "asd1", "asd2" }));
if (i < 10)
{
FileListView.Items[i].Selected = true;
}
}
FileListView.ItemDrag += new ItemDragEventHandler(FileListView_ItemDrag);
but when I Run the application, I can't see the first 10 items selected. For see them, I need to click on one of them, and they will highlights (but of course deselected immediatly, since it is like click on a single row).
How can I preselect 10 items? So a user see them selected and then can click to drag/drop to some destination...

The items are being selected but the control is not activated. Use FileListView.Select() to activate the control.

It sounds like your ListView is not focused so when you select the items they won't highlight.
You can either focus the control before hand like this:
FileListView.Focus();
Or what's probably better is to disable the HideSelection property. This allows the ListView to display selected items when not focused.
FileListView.HideSelection = false;
Edit: With OPs new information that they need to show blue, give keyboard focus to the control once you're done:
FileListView.Select();

Did you set the multiselect property with the designer or by code ?
FileListView.MultiSelect=true ;
Try also:
FileListView.Invalidate() after the loop.

Related

How to scroll the area like drop down and list box in Winapp driver with c#?

I am automating desktop application where i have list box in which many items are listed and i want to click on the item which is hidden and that item will shows after scrolling the list. Which code will work for this? I am working with WinAppdriver, appium , c# and MSTest.
I need such lines of code with which list will scroll till the element finds.
Someone guide me this solution and it is working for me now. So, i am posting answer here.
The List Box has accessibility item "lbStates".
We want to click the item "NC" in it which has the text "NC".
In this case the "Displayed" property of most of the list items will be false.
We can click the down button in the ListBox to scroll down or We can press the keys buttons and test if the value which we want to click is displayed or not.
It is possible that the value we want to click is not present in the listbox, in such case our code will go in a deadlock and for that purpose I've introduced the integer maxClicks.
[TestMethod]
public void ListBoxTest()
{
//lbStates
var lbStates = sessionWinForm.FindElementByAccessibilityId("lbStates");
var allListItems = lbStates.FindElementsByTagName("ListItem");
var valueToClick = "NC";
var maxClicks = 10;
foreach(var ali in allListItems)
{
Debug.WriteLine($"{ali.Displayed} - {ali.Text}");
if(ali.Text.Equals(valueToClick) && !ali.Displayed)
{
var downButton = lbStates.FindElementByAccessibilityId("DownButton");
var listItemToClick = lbStates.FindElementByName(valueToClick);
while(!listItemToClick.Displayed && (maxClicks-- > 0))
{
downButton.Click();
listItemToClick = lbStates.FindElementByName(valueToClick);
if (listItemToClick.Displayed)
{
listItemToClick.Click();
}
}
}
}
}
you can just find your element with winappdriver and it will scroll itself to the element , i have automated a windows application with had a input box that accept a file and had a browser button after clicking browse button it itself scroll till the element is found.
Regarding the code you may just try finding the element normal like it is in view .
Hope this helps. :)

radiobutton in different tabs stay checked

I have a winforms app witch has a tabcontrol and in each tab some radiobuttons the code works but there is a problem.
If i go to a tab and check a radiobutton and after go to another tab and check another radiobutton the previous button stays selected.
Is there a way to whenever i check a radiobutton on another tab the app unchecks the previous button on the other tab?
The code i use is:
if (!radioButton1.Checked && !radioButton2.Checked && !radioButton3.Checked && !radioButton4.Checked)
{
MessageBox.Show("Please select an option!", "Warning");
return;
}
else if (radioButton1.Checked)
{
build build = new build("#../Images/x1.jpg", "definition", "c1");
build.ShowDialog();
}
else if (radioButton2.Checked)
{
build build = new build("#../Images/x2.jpg", "definition2","c2");
build.ShowDialog();
}
etc...
Thanks in advance,
If I understand your question correctly, you just want to uncheck all other radiobuttons? You could just use to uncheck them:
radioButtonX.Checked = False;
Have a look at the events for the tabControl. Make an event for the tabPageX.Enter and clear the radio button (radioButtonX.Checked = false) in that.
Really, unless this is a throw-away test software, I'd recommend looking into a more sophisticated architecture like Model View Presenter. (for what its worth).

Programmatically Limiting/setting the height of a ContextMenu Sub-menu

I am programmatically creating a taskbar notify icon and Right Click ContextMenu in a C# WPF application.
A couple of the ContextMenu items have submenus whicr are populated from webservice calls. One of these is can span the entire height of the users screen, because it is over 100 entries, and you get the overflow arrows. I would like to be able to set max height to like 500.
I have been unable to figure out how to limit the size of these ContextMenu submenus programmatically. Below is the code I used to create the "Directory" submenu, which in theory can contain 0 to 1,000 entries.
Is it possible?
Any help is appreciated, Thank you.
m_menu = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem newMenuItem4 = new System.Windows.Forms.MenuItem("Directory");
System.Windows.Forms.MenuItem newExistMenuItem4 = (System.Windows.Forms.MenuItem)this.m_menu.MenuItems[0];
if (numbers.Count > 0)
{
int indx = 0;
foreach (string number in getContactDirectory() )
{
newMenuItem4.MenuItems.Add(indx,
new System.Windows.Forms.MenuItem(number, new System.EventHandler(historyCall)));
indx++;
}
m_menu.MenuItems.Add(menuCounter, newMenuItem4);
menuCounter++;
}
You need to familiarize yourself with Control templates and XAML.
Have a look at the blog - http://xcalibur37.wordpress.com/2013/05/09/an-enhanced-menuitem-to-limit-submenu-height/
This should answer yoour question - Best way to set a MenuItem's sub-menu height?

Why don't my winform listview items show up selected by default?

I have a winforms app and i have listview. Through the visual designer, I add a bunch of items and set the "checked" property to true on all of the items. When i start my app none of the items are selected which seems odd.
Even after I tried adding this code:
for (int i = 0; i < listView1.Items.Count; i++)
{
listView1.Items[i].Selected = true;
}
when i startup my app (its a vsto app), none of the items are selected. I am choosing LargeIconView (not sure if that makes a difference)
How can i default a listview to have all items checked by default at startup?
The reason your code isn't working is because the ListView control doesn't have focus. Two things you could do are to
1) Set the TabIndex property of the control to be the lowest on the form (likely 0)
2) Select the ListView programmatically
private void Form1_Load(object sender, EventArgs e)
{
listView1.Select();
for (int i = 0; i < listView1.Items.Count; i++)
{
listView1.Items[i].Selected = true;
}
}
The checked property is relevant only if the ListView's property of CheckBoxes is set to True. Checked is not the same thing as Selected.
Your code to select all the items works for me. But perhaps, as keyboardP suggests, your issue is related to Focus. Edited: Yes, it only works because I am testing it and this is the only control on my form.
"How can i default a listview to have all items checked by default at startup?"
Change:
listView1.Items[i].Selected = true;
To:
listView1.Items[i].Checked = true;
Not sure why the setting isn't "sticking" if you already set them all to checked thru the IDE. Are you modifying the contents of the ListView when the form is loading?

ContextMenuStrip how do you force update (size is wrong b/c I hide some of it's items)

I have like 10 items in a ContextMenuStrip. I have an Opening Event handler which decides which of the 10 items will be visible. Based on application logic I may hide many of them by setting Visible = false and Enabled = false;
What happens is in the case that 6/10 are displayed. Their will be an area 4 menu items height of blank space the first time I click the strip. If I right click to make it appear the second time it shows up where it should be. So, the strip basically has the position calculated for a 10 item strip when in reality it's only a 6 item strip.
How do I force it to recalculate?
Here are some of the things I've tried:
contextMenuStrip1.Refresh();
contextMenuStrip1.Update();
contextMenuStrip1.PerformLayout();
contextMenuStrip1.AutoSize = true;
ToolStripItem tempItem = contextMenuStrip1.Items.Add("temp");
contextMenuStrip1.Items.Remove(tempItem);
contextMenuStrip1.Refresh();
contextMenuStrip1.Update();
contextMenuStrip1.Invalidate();
I've found that
AutoSize = false;
AutoSize = true;
after all item manipulations works. Haven't found any other solution.
In my case, I add items on opening (dynamic list of windows), and sometimes item caption was longer than the menu.
Have you tried using Invalidate()?
From the MSDN:
Invalidates the entire surface of the control and causes the control
to be redrawn. (Inherited from Control.)
Perhaps you need to use then WM_PAINT message, I have found that sometimes you need to use SendMessage (Interop) with Winforms.
http://msdn.microsoft.com/en-us/library/windows/desktop/dd145213(v=vs.85).aspx

Categories

Resources