简体   繁体   中英

How do i get information that is object sender

I'm currently trying to get access to information that is passed in through an object sender.

The application I am working on is a winforms application with a list view .
I want to get the number of the ListViewItem that the user has pressed on.
The ListView item that I have pressed on is correct when I debug.

However I am unaware of how to get the information I want from the object sender .
I want to access the ListViewItem number ,

look at the posted image ListViewItem: {24919} in this case

so I can use this number as index, when I search in a database.

Does anyone have a fast tip so I can continue with my program?

private void InvoiceListView_SelectedIndexChanged(object sender, EventArgs e)
{
    //Connect to db and search based on the the listviewItemnumber.
}

Currently the object sender containsmethod;

You can type-check the sender and work with the result:

private void InvoiceListView_SelectedIndexChanged(object sender, EventArgs e)
{
    if (!(sender is ListView listView)) return;
    //work with the listView object from here:
    listView.Items = ...
}

You can get the item selected by casting the sender to ListView and then get corresponding value as given below:

private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
ListView lw = (ListView)sender;
            
            foreach(ListViewItem lvi in lw.SelectedItems )
            {
                MessageBox.Show(lvi.SubItems[0].Text);
            }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM