[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1495
  • Last Modified:

Click event on listView

Language: C#

I have a listView control on windows form. When user click a row, i want to fire the event and get data from this row. eg.

ID       Name
1        Peter
2       Ben

when user click first row, i will get "1" and "Peter" ect. ect..

How can I do this? Experts!

Thanks!
0
vbwang
Asked:
vbwang
  • 2
1 Solution
 
WinterMuteUKCommented:
First you need to hook up to the 'Click' event on the ListView as so:

/////////////////////////////////////
InitializeComponent()
{
      ListView lv = new ListView();
      lv.Click += new System.EventHandler(lv_Click);
}
/////////////////////////////////////

This will make the below method be called whenever the Click event is fired.

/////////////////////////////////////
private void lv_Click(object sender, System.EventArgs e){
      //Get data:
      string output = lv.FocusedItem.Text; //Gives the 'ID'
      foreach (ListViewItem.ListViewSubItem item in lv.FocusedItem.SubItems)
            output += ", " + item.Text; //Any other data.

      //Fire Event.
      if(RowClicked != null)
            RowClicked(this, new RowClickEventArgs(output));
}
/////////////////////////////////////

In the above method another event 'RowClicked' is fired, this is defined below. It takes a special EventHandler class that takes a string as a construction argument. You would handle this in whatever app the same way as you handled the ListView.Click event. (i.e. += to it).

/////////////////////////////////////
//The event which will be fired from the Click handler.
public event RowClickEventHandler RowClicked;

public delegate void RowClickEventHandler(object sender, RowClickEventArgs args);
[Serializable]
public class RowClickEventArgs : EventArgs
{
      private string mText;
      public string Text { get { return mText; } }
      public RowClickEventArgs(string text)
      {
            mText = text;
      }
}
/////////////////////////////////////

So in my secondary app I would do this somewhere:

/////////////////////////////////////
yourClass.RowClicked += new RowClickEventHandler(myHandler);

private void myHandler(object sender, RowClickEventArgs e){
      MessageBox.Show(e.Text);
}
/////////////////////////////////////

Cheers

Wint.
0
 
sourabhkothariCommented:
better u can use the selected index changed event.
also,don't forget to set the multiselect property of listview to false.

listview.selectedinedexchanged += new System.eventhandler(listview_selectedindexchanged);


private void listview_selectedindexchanged(.....)
{
     if(listview.selecteditems.count == 0) // don't forget this line as the event will be fired twice when you change a selection.
           return;
     string ID = listview.selecteditems[0].subitems[0].Text;
     string name = listview.selecteditems[0].subitems[1].Text;
     // do what you want with the strings...
}
0
 
WinterMuteUKCommented:
But SelectedIndexChanged would also fire if you use the keyboard to move the selection, not just a mouse click.
0
 
vbwangAuthor Commented:
Thanks Wint. This is excellent  answer!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now