2013-01-23 50 views
2

這更是一個爲什麼不工作的問題。我顯然必須錯過一些東西,因爲我不能爲我的生活找出爲什麼計算寬度和高度值爲我提供了與我開始時相同的靜態值。但是,當我替代硬編碼值時,它工作得很好。所以顯然我的計算是不好的,但我似乎無法弄清楚爲什麼。誰能幫忙?如果您需要更多信息,請詢問。爲什麼我的計算工作不正常?

if(objectList[selectedIndex].Selected == true) 
{ 
     //The width would be mouselocationx - objectlocationx 
     //The height would be mouselocationy - objectlocationy 
     //Off set the position so it doesnt snap to mouse location 
     //WHY THE *%$!! DOESNT THIS WORK!!?!?!?!?!?!? 
     //int width = e.X - objectList[selectedIndex].X; 
     //int height = e.Y - objectList[selectedIndex].Y; 
     //Why is this so hard?? 
     //Point location = objectList[selectedIndex].Location; 
     //location.X = e.X - (e.X - objectList[selectedIndex].Location.X); 
     //location.Y = e.Y - (e.Y - objectList[selectedIndex].Location.Y); 
     objectList[selectedIndex].X = e.Location.X - 12; 
     objectList[selectedIndex].Y = e.Location.Y - 12; 
     objectLocationXLabel.Text = "OBJX: " + objectList[selectedIndex].X.ToString(); 
     objectLocationYLabel.Text = "OBJY: " + objectList[selectedIndex].Y.ToString(); 
     panel1.Invalidate(); 
    } 
+1

在你的評論代碼中,你使用'e.X'和'e.Y',但是在你未註釋的代碼中,你使用'e.Location.X'和'e.Location.Y' – cjk

+0

你試過調試器嗎?設置斷點並查看e.Location.X中的內容等。或者是否有編譯錯誤? – Peter

+0

是的,我不認爲這是問題所在。 e.Location.X/Y和e.X/Y是相同的值,只是獲得它的另一種方式。 – Timelord

回答

1

這完全不清楚你想要做什麼。但是,至少有兩個問題與您的註釋代碼:

location.X = e.X - (e.X - objectList[selectedIndex].Location.X); 

相當於

location.X = objectList[selectedIndex].Location.X; 

Point是值類型。所以,當你這樣做:

Point location = objectList[selectedIndex].Location; 
location.X = ...; 

location被修改,但不objectList[selectedIndex].Location

編輯回覆評論:我認爲你必須確定MouseDown事件的偏移量,將它存儲在一個成員中,並在MouseMove中使用它。

+0

對不起,很難解釋我試圖完成什麼。看到即將嘗試關閉設置對象的位置,所以它不總是捕捉到鼠標位置。如果有幫助。我想如果我發現object.x/y位置和mouse.x/y位置之間的距離,我可以使用它來關閉對象。但我想我在數學上吸吮。 – Timelord

+0

謝謝!出於某種原因,我完全忽略了這一點。 – Timelord