0
votes

This issue is concerning C# WinForms ... I have custom user control which basically has 3 things: A label, a text box, and a list box, all arranged vertically like this:

XXXXXXXXXXXXX

I have written a small Custom ControlDesigner class to prevent the user from changing the height of this user control at design time.

I also have set the textbox and listbox to anchor to left/right locations, so that when the user changes the width of the user control, these two internal controls resize beautifully.

I want to have the Label is the horizontal center as well, but anchoring that to left/right doesn't seem to work. I'm thinking I might have to written some sort of custom resize function inside the custom control designer class, which automatically places the label in the middle when the user control is resized ?

Also note that I want to label to still remain in the center when the label text is changed.

So how do I do this ?

Here is my custom control designer class till now:

public class DropDownTextBoxDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        public override SelectionRules SelectionRules
        {
            get
            {
                SelectionRules retVal =
                      SelectionRules.Visible |
                      SelectionRules.Moveable |
                      SelectionRules.LeftSizeable |
                      SelectionRules.RightSizeable;

                return retVal;
            }
        }
    }
2
Center the text, not the label. Just set the AutoSize property to False and use TextAlign.Hans Passant

2 Answers

1
votes

Write a SizeChanged of the User Control event like so:

private void UserControl1_SizeChanged(object sender, EventArgs e)
    {
        textBox1.Left = Width / 2 - textBox1.Width / 2;
        textBox1.Top = Height / 2 - textBox1.Height / 2;
    }
0
votes

In the resize event, can you just set the left property of the label to center it in the container?

Label.Left = (Container.Width / 2) - (Label.Width / 2);