Name of item in ListView - c#

I am trying to get name of clicked item in ListView. I do not declaring in code names of items in ListView. User is declaring that names in running app. What i need to do is, when i click on item in ListView, in TextBlock will be name of item, whitch i clicked.
This is my simple code yet:
private void lstViewOfUsers_ItemClick(object sender, ItemClickEventArgs e)
{
TextBlock.Text = Name of clicked item
}

To get the text of an item in the first column then you would do :
TextBlock.Text = lstViewOfUsers.SelectedItems[0].Text;
If you wanted to get the text of a subitem it would look like this :
TextBlock.Text = lstViewOfUsers.SelectedItems[0].SubItems[1].Text;

You can get the index of the clicked item by using the ItemClickEventArgs parameter
another option is to do something like this:
MyItemModel item = (MyItemModel)e.ClickedItem;

Related

Using mouse click event to get ListViewItem text

I want to use listview to populate it with data and then use mouseclick event to fill some textboxes with data. I looked up an example in msdn:
ListViewItem theClickedOne = listView1.GetItemAt(e.X, e.Y);
ListViewItem theClickedtwo = listView1.FocusedItem;
if (theClickedOne != null)
{
MessageBox.Show(theClickedtwo.ToString());
//do your thing here.
//there is a reference to the listview item we clicked on
//in our theClickedOne variable.
}
but I couldn't think about a way to use it in order to differentiate the listviewitems I use since the fist Column in my program is the same and it will only give me a string with it's name(first Column).I want to have something similar to next example but for treeview.
void treeView1_NodeMouseClick(Object sender, TreeNodeMouseClickEventArgs e)
{
MessageBox.Show(e.Node.Text);
}
When populating you ListView, set the Tag property of the items, e.g.
newItem.Tag = "Item 1";
The Tag property has type object, so you can use anything you want here to identify the item. When handling the mouse click event simply check the Tag value again:
if((string)(clickedItem.Tag) == "Item 1")
{
// do stuff for this specific item.
}

ListBox handling in wpf application

i have a list box and a button click event ,when i select an item in the list box and click on the button then the list item should appear in the text box...please help with the code.
private void Get_Click(object sender, RoutedEventArgs e)
{
tb1.Text = listbox1.SelectedItem.ToString();
but it is not working, I'm getting like this but i just need item name to appear on textbox...
System.Windows.Controls.ListBoxItem: item name
Try this:
listbox1.SelectedItem.Content.ToString()
ListBoxes wrap all content in a ListBoxItem, which is what you are seeing. ListBoxItem inherits ContentControl and therefore has a Content property. This contains the object that you want.
http://msdn.microsoft.com/en-us/library/system.windows.controls.listboxitem.aspx

get the clicked item from a IsItemClickEnabled ListView in C# and WinRT

how can I get the clicket elevent from a ListView which has the IsItemClickEnabled enabled?
I know how to get the selected Item/Index but not the clicked item.
ItemClick is working but I can not say s.th. like:
Object selection = listView1.SelectedItem;
EDIT:
I have a ListView and I need to catch the clicked item from this list in the following method:
private void listView1_ItemClick(object sender, ItemClickEventArgs e)
{
...
}
I may be missing something, but doesn't the following work for you?
private void lv_ItemClick_1(object sender, ItemClickEventArgs e)
{
var item = e.ClickedItem as String;
}
Here I assume the items in the list are simple strings, but in general they'll be whatever type you are using in the collection you've bound to the ItemsSource property of the ListView.
I guess you can also try the SelectionChanged event, and get the clicked item as e.AddedItems or MyListView.SelectedItem or MyListView.SelectedItems.

ListView Item select in winform

I want to select item in a ListView upon clicking. I also want to know what I clicked.
I work on winforms with c#.I also want to know How I can clicking the all row?
Just handle the Click event on the list and use the ListView.SelectedItems property to get what items are selected:
private void listView1_Click(object sender, EventArgs e)
{
var firstSelectedItem = listView1.SelectedItems[0];
}
u can use MouseEventArgs and get the mouse location check if it exists inside the selected item bound , that means the click was made on the selected item .
EDIT :
example :
private void myList_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (myList.SelectedItems.Count >= 1)
{
ListViewItem item = myList.SelectedItems[0];
//here i check for the Mouse pointer location on click if its contained
// in the actual selected item's bounds or not .
// cuz i ran into a problem with the ui once because of that ..
if (item.Bounds.Contains(e.Location))
{
MessageBox.Show("Double Clicked on :"+item.Text);
}
}
}
also if you use xaml for window then you must add MouseUp="listView1_Click" attribute to ListView tag

Change a selected item in a listview based on the selection in another listview

I have two list views. In the Item command event of the first Listview i am showing the second list view in modal popup using ajaxtoolkit.
protected void lvSelection_ItemCommand(object sender, ListViewCommandEventArgs e)
{
this.lvPopup.Visible = true;
this.lvPopup.DataSource = linqdataSource;
this.lvPopup.DataBind();
this.mdlPopup.Show();
}
Now in the itemcommand event of the second list view I need to change the content of the selected item in the first listview.
Is it possible to do that?
protected void lvPopup_ItemCommand(object sender, ListViewCommandEventArgs e)
{
// Set the text of the first list view item to the selected item
// of the second list view.
lstView1.Items[lstView1.SelectedIndex].Text =
lstView2.Items[lstView2.SelectedIndex].Text
}
I'd think that if you were to set the CommandName of the selector button in the first ListView to "Select" - from the second list view's ItemCommand event, you should be able to alter either the SelectedItemTemplate or the current item for the selected item in the first list.
protected void lvPopup_ItemCommand(object sender, ListViewCommandEventArgs e)
{
lvSelection.SelectedItemTemplate = "<div>woohoo!</div>";
// OR...
lvSelection.Items[lvSelection.SelectedIndex].SkinID = "SomeNewSkinForExample";
mdlPopup.Hide();
}
Have you already tried to dynamically generate the items of the List?
On the event code of the 1st list, clear the Items from the 2nd list and populate it with whatever logic suits you.

Categories

Resources