Within my winform(C#) I have listView with two columns, which is used to display the results from searching an array, which is the basic details of an array entry(All data within the array is type string). I am able to add and remove items to the listview, but I would like to be able to gain the value of the row that has been clicked within the listview.
I am planning to use this to update the display full entry info, but I cannot gain the value of the selected item. I was reading elsewhere and saw people mentioning the .SelectedIndex property, but this property is not available to me when I am trying to code.
I could use two listboxes instead of a listview, but the listview is much neater. Also, I am a sofware design (high school) student, and have been learning C# for one and a half years. I am competent at programming, but I start to get lost when things start to get very complex.
Can anybody help?
The ListView.SelectedItems property works fine if the ListView is not in VirtualData mode.
you should use the SelectedItem property and Tag property
var item = (MyClass) listView1.SelectedItems[0].Tag;
tag property allow you to set any type for example MyClass
when you populate the ListView set the Tag property.
For ListView check SelectedItems
You could trap the SelectedIndexChanged event and in that you could do
ListView.SelectedListViewItemCollection listItems=
this.myListView.SelectedItems;
foreach ( ListViewItem item in listItems)
{
MessageBox.Show(item.SubItems[0].Text);
MessageBox.Show(item.SubItems[1].Text);
}
I have fixed the problem. After talking to a friend in my software class, he told me to use listView1.FocusedItem.Index, which works perfectly for my needs, as it gains the index of the selected item. Also, thank you to everyone for their input in trying to help me.
Related
I need to change Visibility of all items in listbox in code based on changing some filters. I tried to loop the items using
myListbox.ContainerFromIndex(i)
but only first a few didn't return null. The answer why I quickly found here:
Why ItemContainerGenerator.ContainerFromIndex() returns null and how to avoid this behavior?
Turning off virtualization doesn't look like respectable solution to me, also I don't feel well about scrolling every item to view before calling ContainerFromIndex, even if it could probably work.
I'm not very clever from the rest of topics dealing with bindings and dependency properties etc, maybe also because majority of them are written in xaml and not in code. But I'm pretty sure the answer should be there. I'm very new to c# and I didn't find any clear-cut solution to such a basic problem.
I will prepare something like this array of Visibilities
Visibility[] visibilities=new Visibility[100]
...
and I want myListBox with 100 items to use these values. Either automatically with some c# magic or in a cycle as I used to do in Win32.
Based on the comment of Depechie, I made a research targeted on collections and created a functional code. Here is what I did:
Instead of adding items directly to listbox by
lbGames.Items.Add(...)
I created two collections:
List<object> gameList=new List<object>();
ObservableCollection<object> filteredGameList = new ObservableCollection<object>();
Adding items this way:
gameList.Add(...); or filteredGameList.Add(...)
While gameList is holding all items (stack panels), filteredGameList is holding only those I want visible. I had some problems with using only one big collection using its Visibility property, maybe my mistake, maybe not.
Withouth ObservableCollection I wasn't able to redraw listbox, that was problem with another List<> holding filtered data. When I filter data, I first clear filteredGameList and then I add all items I want visible, never changing Visibility property. ObservableCollection always tells listbox when items change.
On the application init, I attach listbox to this filteredGamesList.
lbGames.ItemsSource = filteredGameList;
Now everything works fine.
How to this for combobox?I have long stackpanel which first it has only combobox.And when i select anything from combobox it will be added as button or other thing like on the picture.And it can be closed
[EDIT] I need anything on WPF
This should do the job nicely in WPF; http://www.codeproject.com/KB/WPF/MultiComboBox.aspx
The combo box that ships with WPF does not support selecting multiple
items...
This article describes a WPF combo box that supports both. It also
describes a list box, which the combo box inherits from, that supports
binding to the SelectedItems
Alternatively: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)
Recently I’ve come across something weird… I needed a ComboBox that will allow the user to select multiple items.
The the solution coming to mind is using CheckBoxes. I have found several examples, but neither one displayed the selected items with pretty commas like this;
i m not sure what you are asking for but you can use this.
jquery tokenizing
what about this?
jQuery Chosen Plugin
http://davidwalsh.name/dw-content/jquery-chosen.php
is there a more simple way to get combobox (WinForms) selected items text?
string result = comboBox1.Items[comboBox1.Selectedindex];
And in WPF's ComboBox?
What about
string result = (string)comboBox1.SelectedItem
Is that more simple?
#Zenuka's method works but I think the following is somewhat "more correct" (avoiding casts when possible):
String result = comboBox1.SelectedItem.ToString();
nope, sorry. That's it.
Just for interest, what part of it to you thiink is not simple?
Regarding WPF combobox: I don't think there is a good general way of getting the text of the selected combobox item in WPF. The wpf combobox displays its items in a template, which can be anything (i.e. several labels nested inside a stackpanel, inside a button etc...). The template need not even display any text, it can for instance display an image or something completly different. That said, i don't think there is much need for accessing the text strings inside a selected item's UI in a combobox. That is just UI for the user, the program should interact with the selected object (not it's UI representation). There might be some exceptions to this, tho, but in that case, you will have to make a specific solution for the item template that you use, because there is no one-size-fits-all solution for this.
I'm trying to accomplish what should be a very simple task using the ListView Control. All I am trying to do is add two Items to a ListView Control. One Item goes under the "Title" Column and the other Item goes under the "Status" Column. I have seen many examples for the ListView Control and none of them cover this particular need.
I've read the MSDN documentation on the ListView Control and find it rather odd they don't mention this... Or maybe I've overlooked it?
In ListView parlance, these are not separate items. It sounds like you want to add one item, and then what ListView calls a subitem. Assuming that Title is your first column and Status your second, you want:
ListViewItem myItem = listView.Items.Add("My Item's Title");
myItem.SubItems.Add("My Item's Status");
If you are just getting started with a ListView, you can save yourself a lot of time, headaches and frustration by using ObjectListView -- an open source wrapper around .NET WinForms ListView.
It solves many problems that you are going to find while trying to use a ListView, as well as just making it much easier to use. Just one example: it automatically handles sorting rows by clicking on column headers.
Is there a way to add item to a WinForms ListBox, to the beginning of the list without rewriting entire list in a loop?
Other way to solve my problem would be to display ListBox in reverse order (last item on the top) but I don't know how to do it.
My ListBox control is used as a log viewer where the most recent entry should be on the top.
Use the Insert method on the items of your ListBox.
If I understand correctly, can't you use the Insert(int index, object item) method? For example:
myListBox.Items.Insert(0, "First");
This inserts "First" as the first item of the listbox.
One option might be to use the .Sort() method of the ListBox
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.sort.aspx
The other of course is to put your items in a generic list and add/remove items
from that list instead of directly to the ListBox. Use the list as a datasource for your ListBox.
You should be able to set the sort order in your data source if you're timestamping the log events.
I have no scientific proof to back me up here but I think a textbox is more performant in handling log visualization.
You can also easily setup autoscrolling and if you would want to copy something, it would not require any coding.