views:

69

answers:

2

I have a control I built with several input controls on it, some text boxes, a combo box and a list box among other things. One text box, a combo box, a list box and a couple of command buttons are anchored so that they resize with the form. The combo box is anchored to the left, top and right, the listbox is anchored on all four sides and the buttons are anchored on the bottom and right. They all resize and maintain their position correctly. The problem is the text box I'm trying to anchor, just like the combo box. It runs off the right side of the form at runtime when anchored to the right. Yet, I don't have this problem with any of the existing controls on the form that are anchored to the right, they all stay in position like they're supposed to.

I tried adding an additional text box and combobox and anchoring them to the right, but they too run off the right edge of the form. Why is it that the existing controls, except for one, work like they're supposed to but the one control and new ones become too wide?

For reference, I've manipulated the designer generated code to rearrange the order of the statements so that the anchoring occurs after the position and width are set, but that did no good. Here's the code as is for the misbehaving textbox:

        // 
        // txtComments
        // 
        this.txtComments.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.txtComments.Location = new System.Drawing.Point(430, 6);
        this.txtComments.Name = "txtComments";
        this.txtComments.Size = new System.Drawing.Size(166, 20);
        this.txtComments.TabIndex = 25;

And the code for the properly behaving combobox:

        // 
        // cbMacroList
        // 
        this.cbMacroList.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.cbMacroList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        this.cbMacroList.FormattingEnabled = true;
        this.cbMacroList.Location = new System.Drawing.Point(128, 78);
        this.cbMacroList.Name = "cbMacroList";
        this.cbMacroList.Size = new System.Drawing.Size(468, 21);
        this.cbMacroList.TabIndex = 30;
        this.cbMacroList.SelectedIndexChanged += new System.EventHandler(this.cbMacroList_SelectedIndexChanged);

One final piece of background is that all of these controls came off of a Form when I determined I would need to reuse the functionality the form was providing in another form. I selected all the controls, cut them and then pasted onto the new UserControl. Before the migration to the UserControl, they all worked fine. This began showing up after migration.

+1  A: 

Are you sure your misbehaving control isn't parented by something you aren't expecting, like a Panel or something?

Dave Markle
I double-checked and it is being added to `this.Controls`, so no Panels or anything interfering with positioning and layout.
jasonh
A: 

This is rather odd, but simply deleting the control from the form and placing it down again has solved the problem. I re-examined the designer generated code on the form that I use the control on and I see absolutely no difference in how it is created or added to the form. And yet, it's fixed. I wonder why it would behave this way...?

jasonh