2012-02-04 44 views
2

這可能聽起來像一個愚蠢的問題,但我需要知道如何自動更改按鈕的位置,當其旁邊的其他按鈕仍然不可見時。我需要在Visual Studio 2005中實現這一點(我正在使用C#)。按鈕位置根據其他按鈕的可見性

爲了給出進一步的說明,假設我在我創建的表單的左上角有三個按鈕。從右到左的按鈕是: 1-回到 2-打印 3-接着

最初,並且當所述形式首次啓動時,只有下一步按鈕應該是可見的,並且它應該佔據的右上方屏幕。稍後,當用戶在屏幕上觸發一些事件時,應該會出現「打印」和「後退」按鈕,但它們也應該出現在表單的右上角,這將實現與我上面提到的相同的順序。同樣的按鈕順序是我需要達到的要求。

感謝您的幫助提前。

+0

這裏真的沒有好的答案。有第三方的佈局生成器(這就是我用過的),但通常在WPF之前,您必須依賴如下定義的錨定規則:[link](http://msdn.microsoft.com/en-us/library /ms951306.aspx) – akhisp 2012-02-04 23:40:50

+0

@ akhisp你可以使用位置和可見來編程實現它,甚至可以基於任何其他按鈕位置(甚至任何其他控件的位置)在窗體內動態計算它 – jordanhill123 2012-02-04 23:49:42

+0

@Mouhammed Soueidane您正在使用的WinForms? – jordanhill123 2012-02-04 23:50:22

回答

2

你可以使用:

Nextbutton.Visible = True; //initially 
backbutton.Visible = False; //initially 
printbutton.Visible = False; //initially 
backbutton.Enabled = False; //initially to prevent tabbing to the control and clicking on it 
printbutton.Enabled = False; //initially to prevent tabbing to the control and clicking on it 

,然後在事件處理程序(一個或多個)設置

backbutton.Visible = True; 
printbutton.Visible = True; 
backbutton.Enabled = True; 
printbutton.Enabled = True; 

您甚至可以設定printButton和後退按鈕的位置,最初,他們只是將不可見但在你想要他們的位置。

另外,如果你需要設置位置使用:

someButton.Location = //some location on your form and move all three buttons as needed. 

如果你希望他們能夠相互抵消,你甚至可以這樣做:

someButton.Location = (otherButton.Location +- /*Some offset*/) ;