2014-10-19 101 views
0

我想獲取ListView項目/子項目的位置。我的意思是,我想爲這個子項目獲得一個左和頂。這是我的意思是:Listview子項目位置(C#窗體)

enter image description here

我嘗試這樣做:

int item_x = list.Items[1].SubItems[2].Bounds.X; 
int item_y = list.Items[1].SubItems[2].Bounds.Y; 

但它似乎並沒有給予正確的位置我想要的。

+1

我認爲你需要把你的ListView控件的座標關注。我認爲(我不知道或測試你的代碼是否工作)你的代碼將返回相對於你的列表視圖的座標。如果你的列表視圖在容器中而不是直接在表單上,​​你應該考慮到這一點 – 2014-10-19 21:00:12

+0

實際上這些數字對我來說看起來很好。正確的座標在列表視圖中,標題高度包括..他們是什麼,你會期望什麼? – TaW 2014-10-19 21:17:29

+0

它只是不可能是正確的......這是位置變成http://spunit.cf/x/scrn352.png的地方 – spunit 2014-10-19 21:30:32

回答

0

好的,看起來我之前寫的是正確的。它只需要進行一些加減運算,然後顯示如下:http://spunit.cf/x/scrn370.png

感謝讓我意識到這=)。

下面是它的代碼:

int item_l = list.Items[1].SubItems[2].Bounds.Left + 3; 
int item_t = list.Items[1].SubItems[2].Bounds.Top + 29; 
int item_w = list.Items[1].SubItems[2].Bounds.Width - 1; 
int item_h = list.Items[1].SubItems[2].Bounds.Height - 1; 

ListView listt = new ListView(); 
Controls.Add(listt); 
listt.BringToFront(); 
listt.Bounds = new Rectangle(new Point(item_l, item_t), new Size(item_w, item_h));