By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,382 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

Mouse Events ?

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <Th**************@Yahoo.Com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 15 '05 #2

P: n/a
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
"TM" <Th**************@Yahoo.Com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 15 '05 #3

P: n/a
TM
Thanks Burke, that works great!

Thomas

"Burke ATILLA" <bu***@verisoft.com.tr> wrote in message
news:Op**************@TK2MSFTNGP12.phx.gbl...
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" <Th**************@Yahoo.Com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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


Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.