Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click Button on ListView is not working

I am an trying to get a click or double click to work on my listview, and my listview1_1 click doesn't seem to work. Whenever I click on a selected item, no messagebox pops up. I followed the usual format in creating a click event, however that does not seem to work. Is it because I am loading in the data from a database into the listview, instead of data that I created inside of the listview? Thanks so much!

using System;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace sundayschoolproject
{
    public partial class Form1 : Form
     {
    string con = ("Data Source=ZTABASSUM\\SQLEXPRESS01;Initial Catalog=   Sunday School;Integrated Security=True");
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        listView1.GridLines = true;
        listView1.View = View.Details;

        //Add Columns 
        listView1.Columns.Add("ParentName", 100);
        listView1.Columns.Add("Address", 150);
        listView1.Columns.Add("City", 100);
        listView1.Columns.Add("State", 50);
        listView1.Columns.Add("Zipcode", 100);
        listView1.Columns.Add("Phone", 150);


    }

    private void button1_Click(object sender, EventArgs e)
    {
        string sql = ("Select * from Family");
        SqlConnection connection = new SqlConnection(con);

        SqlCommand cmd = new SqlCommand(sql, connection);

        connection.Open();

        SqlDataReader dr = cmd.ExecuteReader();

        listView1.Items.Clear();

        while (dr.Read())
        {
            ListViewItem lv = new ListViewItem(dr.GetString(0));
            lv.SubItems.Add(dr.GetString(1));
            lv.SubItems.Add(dr.GetString(2));
            lv.SubItems.Add(dr.GetString(3));
            lv.SubItems.Add(dr.GetString(4));
            lv.SubItems.Add(dr.GetString(5));
            listView1.Items.Add(lv);
        }


        dr.Close();
        connection.Close();



    }





        private void listView1_Click(object sender, MouseEventArgs e)
    {
        if (listView1.SelectedItems.Count > 0)
        {
            MessageBox.Show("You clicked " + listView1.SelectedItems[0].Text);
        }
        else
        {
            MessageBox.Show("Please select an item");
        }

    }

}
}
like image 371
Zohra Tabassum Avatar asked Feb 05 '26 21:02

Zohra Tabassum


1 Answers

I think that you are trying to click on one of the SubItems that a ListViewItem has and nothing happens.

Well, to be able to make a SubItem clickable, you need first to make the FullRowSelect property of the ListView you have to allow the full row selection:

listView1.FullRowSelect = true;

then you can use ListView.HitTest method that gives you information of an item that is located in a given point.

You could use the ListView.HitTest method in MouseClick or MouseDoubleClick rather than Click or DoubleClick events, because they provides an instance of MouseEventArgs class that will help you to get the mouse location that you will use it in the HitTest method to locate the clicked subitem, try this:

private void listView1_MouseClick(object sender, MouseEventArgs e)
{
    // Get the information of an item that is located in a given point (mouse location in this case).
    ListViewHitTestInfo hit = listView1.HitTest(e.Location);
    // hit.Item: Gets the ListViewItem.
    // hit.SubItem: Get the ListViewItem.ListViewSubItem

    if (listView1.SelectedItems.Count > 0)
    {
        MessageBox.Show("You clicked " + hit.SubItem.Text);
    }
    else
    {
        MessageBox.Show("Please select an item");
    }
}
like image 89
Wael Alshabani Avatar answered Feb 08 '26 14:02

Wael Alshabani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!