2013-04-15 107 views
0

我有2個文本框,一個用於X,另一個用於Y座標。 我希望它在X和Y的用戶inters值和當他們按下按鈕時,它將窗口移動到該位置。使用X和Y座標移動窗口位置

我加了這一點:

this.Location = new System.Drawing.Point(1000, 500); 

到這裏:

private void button1_Click(object sender, EventArgs e) 
{ 
    this.Location = new System.Drawing.Point(500, 900); 
} 

和它完美,當我按下按鈕將窗口。

如何讓窗口移動到在文本框中輸入的位置,這樣用戶可以輸入X和Y座標,當它們按下按鈕時,窗口將移動到它們指定的位置。

回答

1

在按鈕點擊處理程序中,您可以從文本框中獲取文本並將字符串轉換爲整數。由於文本來自用戶輸入,因此最好使用TryParse而不是僅使用Parse,以免引發異常:

int windowX, windowY; 

if (Int32.TryParse(txtX.Text, out windowX) && 
    Int32.TryParse(txtY.Text, out windowY)) 
{ 
    this.Location = new System.Drawing.Point(windowX, windowY); 
} 
else 
{ 
    // Tell the user they didn't enter a valid number 
}