2

This might sound like a silly question, but I need to know how to change the position of a button automatically, when other buttons that are right next to it are still not visible. I need to achieve this in Visual Studio 2005 ( I'm using C#).

To give further clarifications on this, let's say that I have three buttons on the top left of a form that I have created. The buttons from right to left are: 1- Back 2- Print 3- Next

Initially, and when the form is first started, only the Next button should be visible, and it should occupy the top right of the screen. Later on, and as the user triggers some events on the screen, the Print and Back buttons should appear, but they should appear on the top right of the form as well, which would achieve the same sequence that I have mentioned above. This same sequence of buttons is a requirement that I need to achieve.

Thanks for the help in advance.

4
  • There's really no good answer here. There are 3rdparty layout generators (and that's what I have used) available but generally, before WPF, you had to rely on anchoring rules as defined here: link
    – akhisp
    Feb 4, 2012 at 23:40
  • @ akhisp you can accomplish it programmatically using location and visible and even make it dynamically calculated within the form based on a any other button location( or even the location of any other control) Feb 4, 2012 at 23:49
  • @Mouhammed Soueidane you are using winforms? Feb 4, 2012 at 23:50
  • Yes I'm using Winforms. It would have been troublesome for me to dynamically calculate the location if a button based on other buttons' visibility. This is a complexity (despite being easy) that I do not want to go through especially that maintaining my code would become harder. FlowLayoutPanel solved my problem. Thanks a lot for the help. Feb 5, 2012 at 13:40

2 Answers 2

6

You're looking for the FlowLayoutPanel:

http://msdn.microsoft.com/en-us/library/system.windows.forms.flowlayoutpanel.aspx

Set FlowDirection to RightToLeft, add your buttons, and you should be in business.

1
  • 1
    Man thanks a lot! This was exactly what I wanted. I used FlowLayout in Java, but not in .Net. Shame on me =) Feb 5, 2012 at 10:26
2

You could use:

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

and then in the event handler(s) set

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

You could even set the location of printButton and backButton initially and they just would not be visible but in the location that you want them to be.

Also, if you need to set the location use:

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

If you want them to offset from each other you could even do:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.