2010-10-21 14 views
0

我有一個DataGridView在我的表單之一,在某一點我調整其位置的變化。我希望能夠在運行時將其恢復到設計者的默認位置。我知道我可以在更改之前保存它,然後再恢復它,但是我覺得在.NET中確實有辦法將控制位置恢復爲默認值。有什麼建議?在運行時恢復表單內的控件位置和大小。 (.NET)

回答

1

我認爲唯一的方法是存儲控件的座標和大小,然後重新應用它。

運動前:

private Point _location; 
private Size _size; 

private void EventFired(object sender, EventArgs e) 
{ 
    _location = myControl.Location; 
    _size = myControl.Size; 

    // Move control 
} 

運動後:

myControl.Location = _location; 
myControl.Size = _size; 
+0

謝謝你提醒我關於點結構,這使得它很容易 – fbernier 2010-10-21 20:38:17

1

有沒有這樣的概念爲 '默認控制位置'。所有的控件都是在InitControls()中創建的代碼定位的。要恢復「默認位置」,請在OnLoad()中捕獲Location矩形,並在需要時恢復爲該值。

+0

感謝您清除了這一點。 – fbernier 2010-10-21 20:40:01

0

對不起,死靈張貼,但您可以使用您的控件的標籤屬性(如果尚未使用)

來存儲:

myControl.Tag = myControl.Bounds 

myControl.GetType().GetProperty("Tag").SetValue(myControl, myControl.GetType().GetProperty("Bounds").GetValue(myControl, Nothing), Nothing) 

恢復:

myControl.Bounds = myControl.Tag 

myControl.GetType().GetProperty("Bounds").SetValue(myControl, myControl.GetType().GetProperty("Tag").GetValue(myControl, Nothing), Nothing) 
相關問題