Mouse Events ?

B

Burke ATILLA

Below code may help you,

private int X=0;
private int Y=0;
private int subItemSelected = 0 ;

public void ListView_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
li = this.GetItemAt(e.X , e.Y);
X = e.X ;
Y = e.Y ;
}

public void ListView_DoubleClick(object sender, System.EventArgs e)
{
int nStart = X ;
int spos = 0 ;
int epos = this.Columns[0].Width ;

for ( int i=0; i < this.Columns.Count ; i++)
{
if ( nStart > spos && nStart < epos )
{
subItemSelected = i ;
break;
}

spos = epos ;
epos += this.Columns[i+1].Width;
}
}
Console.WriteLine("SUB ITEM SELECTED = " +
li.SubItems[subItemSelected].Text);
subItemText = li.SubItems[subItemSelected].Text ;

string colName = this.Columns[subItemSelected].Text ;
}

Burke.
 
T

TM

My Windows Form application has a ListView control, it has 3 columns. I use
onMouseUp event to capture the user's x,y mouse coordinates. How do I use
these X & Y to find out the excact column and the row number of thew
ListView control so that I can retrieve an item in it.

Example: string ls=myListView.Items[???].ToString(); The ??? is the index
point to the list items.

TIA

Thomas
 
D

DalePres

I'd use MouseDown rather than MouseUp, as I did in the code below. But if
you really need MouseUp just change it.

private void listView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)

{

ListViewItem item = listView1.GetItemAt(e.X, e.Y);

if (item != null)

{

MessageBox.Show(item.GetType().ToString());

}

}

Hope this helps,

Dale
 
T

TM

Thanks Burke, that works great!

Thomas

Burke ATILLA said:
Below code may help you,

private int X=0;
private int Y=0;
private int subItemSelected = 0 ;

public void ListView_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
li = this.GetItemAt(e.X , e.Y);
X = e.X ;
Y = e.Y ;
}

public void ListView_DoubleClick(object sender, System.EventArgs e)
{
int nStart = X ;
int spos = 0 ;
int epos = this.Columns[0].Width ;

for ( int i=0; i < this.Columns.Count ; i++)
{
if ( nStart > spos && nStart < epos )
{
subItemSelected = i ;
break;
}

spos = epos ;
epos += this.Columns[i+1].Width;
}
}
Console.WriteLine("SUB ITEM SELECTED = " +
li.SubItems[subItemSelected].Text);
subItemText = li.SubItems[subItemSelected].Text ;

string colName = this.Columns[subItemSelected].Text ;
}

Burke.

TM said:
My Windows Form application has a ListView control, it has 3 columns. I use
onMouseUp event to capture the user's x,y mouse coordinates. How do I use
these X & Y to find out the excact column and the row number of thew
ListView control so that I can retrieve an item in it.

Example: string ls=myListView.Items[???].ToString(); The ??? is the index
point to the list items.

TIA

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top