2015-10-06 25 views
1

在我的項目中,我有一個ListView,我想在我點擊右邊的大圖標時打開ContextMenuStrip。我嘗試了很多東西,但是我沒有成功。當我在ListView內點擊右鍵時,ContextMenuStrip會打開,但我想看看我右鍵單擊大圖標的時間。Listview Large Icon右鍵打開ContextMenuStrip

enter image description here

此外,我需要幫助有關獲得點擊圖標的名稱(屬性)。

+0

你試過我的代碼嗎?我已更新它與(垂直滾動的LV) – TaW

+0

我試過,但我不知道我明白了真的:/我無法在你的代碼在我的列表視圖項目:/ – abbays

+0

你有什麼問題?你之前有什麼代碼? – TaW

回答

1

這是一個快速和骯髒的解決方案;請不要把更多的工作,把它比我..

// a class level reference, prepare it where you want.. 
ContextMenuStrip ms = new ContextMenuStrip(); 

您應該代碼MouseDownMouseUp事件:

private void listView1_MouseDown(object sender, MouseEventArgs e) 
{ 
    // disassociate from listview at first: 
    listView1.ContextMenuStrip = null; 

    // check for right button 
    if (e.Button != System.Windows.Forms.MouseButtons.Right) return; 

    // get item info: 
    ListViewHitTestInfo hi = listView1.HitTest(e.Location); 

    // no item hit: 
    if (hi.Item == null) return; 

    // calculate the image rectangle: 

    // this contains the unscrolled y coordinate: 
    Point iloc = listView1.GetItemRect(hi.Item.Index).Location; 
    // we combine it with the x-position: 
    Rectangle r = new Rectangle(new Point (hi.Item.Position.X, iloc.Y), 
           imageList1.ImageSize); 
    // no image hit: 
    if (!r.Contains(e.Location)) return; 

    // maybe prepare or change the menue now.. 
    // here I display the image name from the keys array: 
    ms.Items[0].Text = imageList1.Images.Keys[hi.Item.ImageIndex]; 
    ms.Location = e.Location; 

    // associate with listview and show 
    listView1.ContextMenuStrip = ms; 
    ms.Show(); 
} 
0

可否請您嘗試以下方法,讓看到它羯羊或工作不... 私人無效listView1_MouseClick(對象發件人,發送MouseEventArgs E) {
如果(e.Button == MouseButtons.Right) { 如果(listView1.FocusedItem.Bounds.Contains(e.Lo陽離子)== true) { contextMenuStrip1.Show(Cursor.Position); }}
}

+0

我試過這些代碼,但是當在列表視圖中點擊某處時,仍然在contentMenuStrip工作。我只想看到contextMenuStrip當我點擊物品,並且我想要獲取物品屬性以供使用。 – abbays

0

這應該工作

private void listView1_MouseClick(object sender, MouseEventArgs e) { ListView listView = sender as ListView; if (e.Button == System.Windows.Forms.MouseButtons.Right) { ListViewItem item = listView.GetItemAt(e.X, e.Y); if (item != null) { item.Selected = true; contextMenuStrip1.Show(listView , e.Location); } } }

搜索在鼠標點擊位置的列表視圖項。如果在那裏,顯示菜單.........