2012-02-04 124 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*/) ;