Home > Net >  Make a button distance itself dynamically from another
Make a button distance itself dynamically from another

Time:06-28

I am adding a new button in a Form in which there are already 2 existing ones. The problem occurs when btnCancel's content changes to a longer string, which makes the button auto-resize - hence, overlapping the button that I have added.

enter image description here

The Proxy Settings is the button that I added. If I change Don't sign in to a much longer string, then the middle button would overlap mine.

The code for the buttons is below:

// 
// btnLogin
// 
this.btnLogin.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnLogin.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.btnLogin.Location = new System.Drawing.Point(340, 50);
this.btnLogin.Margin = new System.Windows.Forms.Padding(2);
this.btnLogin.Name = "btnLogin";
this.btnLogin.Size = new System.Drawing.Size(82, 24);
this.btnLogin.TabIndex = 0;
this.btnLogin.Text = "Sign in...";
this.btnLogin.UseVisualStyleBackColor = true;
this.btnLogin.Click  = new System.EventHandler(this.btnLogin_Click);
// 
// btnCancel
// 
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(239, 50);
this.btnCancel.Margin = new System.Windows.Forms.Padding(2);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(94, 24);
this.btnCancel.TabIndex = 1;
this.btnCancel.Text = "Don\'t sign in";
this.btnCancel.UseVisualStyleBackColor = true;
// 
// btnProxySettings
// 
this.btnProxySettings.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnProxySettings.Location = new System.Drawing.Point(138, 50);
this.btnProxySettings.Margin = new System.Windows.Forms.Padding(2);
this.btnProxySettings.Name = "btnProxySettings";
this.btnProxySettings.Size = new System.Drawing.Size(92, 24);
this.btnProxySettings.TabIndex = 2;
this.btnProxySettings.Text = "Proxy Settings";
this.btnProxySettings.UseVisualStyleBackColor = true;
this.btnProxySettings.Click  = new System.EventHandler(this.btnProxySettings_Click);

How can I make the latter button distance itself dynamically?

Edit as I noticed that my post got downvoted. I have tried several "calculations" using the data within btnCancel like location, size, width, etc. but I could not figure out a "rule" so to say. Or maybe there's even a method that I could not find which does everything "magically"? So it's not like I'm asking for you to do my homework or anything, but I found it useless for the current situation to showcase my poor attempts.

CodePudding user response:

Put the buttons in a flowLayoutPanel with a RightToLeft FlowDirection. This should guarantee proper separation without any need to manually calculate any positions. Whenever you work with auto-scaling controls it is usually a good idea to use some kind of panel with an automatic layout.

  • Related