Every time this Dropdown is showing first Item Blank
ComboBox cb;
List<string> namesCollection=new List<string>();
namesCollection.Add("---- Select ----");
namesCollection.Add("ABC1");
namesCollection.Add("ABC2");
namesCollection.Add("ABC3");
namesCollection.Add("ABC4");
foreach(string pname in namesCollection)
cb.Items.Add(pname);
Does anyone have solution for this ?
You appear to be defining the ComboBox right there in your code, so I'll assume it's actually displayed somewhere in your form / window.
It's normal for the ComboBox to display a blank line initially.
Specify the item you want to display, immediately after populating the ComboBox with data:
cb.SelectedIndex = 0;
You probably have a line feed in your items collection. Hard to see. Adding a cb.Items.Clear() before populating is probably a good idea anyway and will get rid of the problem, if you can't locate it.
Related
Is there a method of going about selecting items from a checked list box in coded ui when the exact text contained within the list box item will not be known until runtime? Perhaps a method of selecting a list box item at a certain index?..
I've tried using click list box at point (x,y) but whenever a list item is in its place it throws and error because it is in the way, And i cant try clicking the list box item itself as the full text it displays is a randomly generated ID and i am not sure if there is another method of finding items.
Another possibility would be passing in the text of the list box item to select at run time?
This is currently what I am trying to do in order to grab the control although it's throwing errors due to type mismatch
Managed to get it working with the following:
WinList uIItemList = UIMainwindowWindow.UIClbxSearchResultsWindow.UIClbxSearchResultsList;
WinCheckBox listItem = (WinCheckBox)uIItemList.Items[0];
listItem.Checked = true;
Just set the SelectedIndex property of the list box after its been populated. The below very simple example sets it to item 3 ( Zero based index)
lstBox.SelectedIndex = 2;
As per your comments, the same can still be achieved on checked list box with the below.
chkListBox.SelectedIndex = 2;
chkListBox.SetItemCheckState(2, CheckState.Checked);
Hope that helps.
I'm using ObjectListView with C# and .Net 4.0. I wrote code that reloads the listview and then re-selects the last selected index.
The re-selection code is quite simple:
olvListView.SelectedIndex = i;
This appears to work, because the item is selected. However, if I then click the up or down arrow, the selection jumps up to the second row (no matter what row I selected), suggesting that the selection was actually set on the first row, no matter what was the value of i.
What am I doing wrong here?
The underlying ListView Control distinguishes between 'selection' and 'focus'.
olvListView.SelectedIndex = i; changes the selection but not the focus. But the focused row is the one that the keyboard input relates to.
Either change the focus the as well
olvListView.SelectedIndex = i;
olvListView.FocusedItem = olvListView.SelectedItems[0];
or call
olvListView.SelectObject(aModelObject);
The second solution would be the preferred way to select an item when working with OLV, however you say you "wrote code that reloads the listview", so the reference to the original item is probably different. Maybe you should just refresh the items that changed, instead of reloading everything. That way you could preserve the selection.
Example if your olv have datasource from "class_z.list" and foreach have only one result.
foreach(class_z a in class_z.list.Where(x=>x.id==id_value))
{
olv.SelectedObject = z;
}
I'm using a DataGridView with bounded List of objects in my application. So I have:
grid.DataSource = Files.Instance.List;
in my form load event and than I want to have two buttons - for adding and removing items from the list (so also from the grid), I though it should be as simple as:
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Files.Instance.List.Add(new DelphiFile { FilePath = openFileDialog.FileName });
grid.Refresh();
}
I'm only setting up the start path here, I wanted the rest parameters to be set by user in the grid view.
The item is added correctly but unfortunatelly it doesnt appear on the list, why?
I also have issues with deleting items:
foreach(DataGridViewRow row in grid.SelectedRows)
{
Files.Instance.List.Remove(row.DataBoundItem as DelphiFile);
}
grid.Refresh();
items are deleted correctly but again the grid doesn't seem to refresh and I'm even getting an exception because the last item in the grid doesn't have a value than :O .
What am I doing wrong?
I guess you declared Files.Instance.List as of type List<DelphiFile>, so when the collection is changed, the dataGridView doesn't know about that, use BindingList<DelphiFile> instead.
I am web developer , working on a part of my project developed in WinForms. So my question could be a basic one. Try to bear with it.
I have two list views on my page and a remove button that works for both.
Problems.
I am not able to select a row in both the list view when I run my program, may be some property needed for it?
If I am able to select the row I want to detect which list view item has been selected, so how would I do that?
I have three columns and have bound the data by using the code below.
listView1.Columns.Add("ID",20);
listView1.Columns.Add("Name",40);
listView1.Columns.Add("Mobile",40);
foreach (var item in dataList)
{
newItem = new ListViewItem();
newItem.SubItems.Add(item.ID.ToString());
newItem.SubItems.Add(item.Name);
newItem.SubItems.Add(item.Mobile.ToString());
listView1.Items.Add(newItem);
}
but the ID column is left blank and the data starts to bind in these sense.
ID Name Mobile
1 abc
2 xyz
So how do I properly show the data?
Lastly I want to use my ID column to delete the data. So if I give width=0, is this the best way to hide a column?
See ListView.FullRowSelect property.
See ListView.SelectedItems property. Note, that by default ListView allows multiselection.
Set item text via constructor: newItem = new ListViewItem(item.ID.ToString());, then add rest of subitems (except of item.ID).
If you want to delete the column, just remove it from the columns collection.
Set
listView1.Items[index].Selected=true;
I don't know if the title express what I want. I have a ListBox in WPF where I generate many elements. When I click on a element while still generating I want my selected item to not move down the list, so I cannot see it anymore, I want to stay in the exact position where I click on it.
If this is possible, can someone point some ideas on how to do it in C#?
Thanks.
Assuming that this is even a good idea and that you are using winforms
Step 1:
Determine the index of the selected item in the source.
Step 2:
When your adding items to the ListBox split the ListBox at the index where the item previously was insert the item at that point, then add on the remainder of the items, while making sure that you've removed the item if it is now elsewhere in the list.
Code:
//Let's assume that you know how to get the position of the item when it is clicked and save the
//item to a variable called OriginalItem
public void PutTheItemInTheSameSpot()
{
var listboxitems = (List<Integer>)YourListBox.DataSource;
var originalClikedItem = OriginalItem;
var topPart = new List<Integer>();
for (i = 0; i < itemPosition; i++)
{
topPart.Add(listboxItems[i]);
}
topPart.Add(originalClickedItem);
var bottomPart = listboxitems.Remove(toppart);
YourListBox.DataSource = toppart.AddRange(bottomPart);
}
Saw your edit about it being WPF
The could should work in idea.
Just a thought: you could try having your view respond to an event whenever an item is added to your ListBox. In the event handler, you could force the selected item to scroll into view presumably keeping it in the current "viewable" position:
listBox.ScrollIntoView(listBox.SelectedItem);
I've never tried this before so it may or may not produce the desired affect?