DigitalJedi805
DigitalJedi805

Reputation: 1460

Why does only one of my controls get added to this form?

I'm working on a debugging application and I'm building a form ( based on a very small extension of the System.Windows.Form ) that is intended to take in a Constructor and create a new Parameter control for each Parameter in the Constructor.

My issue at the moment is that for some reason, my ParameterControls are being added to the form, but only the first one added is visible at the end of the operation.

The code in question and the supporting method; are as follows:

class ConstructorDialog : Namespace.Forms.Form
{
    protected void InitializeInterface()
    {
        if (this.TargetType == null)
        {
            throw new InvalidOperationException("Cannot GenerateFields for ConstructorDialog. ConstructorDialog TargetType is null.");
        }
        else if (this.TargetConstructor == null)
        {
        }
        else
        {
            foreach( ParameterInfo Parameter in this.TargetConstructor.GetParameters())
            {
                try
                {
                    ParameterControl NewParameterControl = new ParameterControl(Parameter);
                    NewParameterControl.Location = new Point(0, 30 + (30 * Parameter.Position));
                    this.AddControl(NewParameterControl);
                    continue;
                }
                catch (Exception e)
                {
                }
            }
            return;
        }
    }
}

class Namespace.Forms.Form : System.Windows.Forms.Form
{
    public Control AddControl(Control Control)
    {
        if (Control == null)
            throw new InvalidOperationException("Form cannot AddControl. Control is null.");
        else
        {
            this.Controls.Add(Control);
            return Control;
        }
    }
}


class Namespace.Debugging.ParameterControl : Namespace.Forms.UserControl
{
    protected void InitializeInterface()
    {
        if (this.TargetParameter == null)
        {
            throw new InvalidOperationException("Cannot InitializeInterface for ConstructorParameterControl. ConstructorParameterControl TargetParameter is null.");
        }
        else
        {
            this.Controls.Clear();

            this.AddLabel(this.TargetParameter.Name + "_Label", this.TargetParameter.Name, new Point(25,0));

            return;
        }
    }
}

class Namespace.Forms.UserControl : System.Windows.Forms.UserControl
{
    public Label AddLabel(String LabelName, String LabelText, Point Location)
    {
        if (String.IsNullOrEmpty(LabelName))
            throw new ArgumentNullException();
        else if (String.IsNullOrEmpty(LabelText))
            throw new ArgumentNullException();
        else
        {
            Label NewLabel = new Label();

            NewLabel.Name = LabelName;
            NewLabel.Text = LabelText;
            NewLabel.Location = Location;

            return this.AddLabel(NewLabel);
        }
    }

    public Label AddLabel(Label Label)
    {
        if (Label == null)
            throw new ArgumentNullException();
        else
        {
            this.Controls.Add(Label);
            return Label;
        }
    }
}

My Form extension is still in its infancy, so it's quite likely that I overlooked something ( especially since my forms knowledge is only apprentice-worthy ), but this operation seems simple enough and in my assessment ought to work.

Some debugging information:

The controls are being added to the base 'Controls' collection.
The positions of the controls are being set to what they ought to, so it is not a matter of them overlapping.
No exceptions are encountered during execution.

Upvotes: 0

Views: 79

Answers (1)

DigitalJedi805
DigitalJedi805

Reputation: 1460

As suggested by @sysexpand, I manually set the height and width of the ParameterControl objects, as well as setting the 'Visible' property to true and seem to have resolved the issue.

My assessment of this is that by setting these variables before the ParameterControl is a member of its parent, these variables are being overwritten when the control is added to its parent.

Upvotes: 1

Related Questions