va.
va.

Reputation: 780

How to show one Control beside another Control dynamically?

There is a class NodeButton, and these NodeButton's are being created dynamically and then added to Parent Control which is Panel. In the NodeButton there is created an inside TextBox and it shows correctly over the NodeButton. The problem is that I need to create another inside TextBox, which should be located on the right side of the NodeButton. As the Button can be Moved, the TextBox should move too like the first one. Now the new TextBox is not visible as it is outside the Button's borders. Is this possible without creating Wrapper Control for the button and TextBox, the NodeButton class is too complicated..

+------+ +-------+
|Button| |TextBox|
+------+ +-------+

public class NodeButton : Button
{
  ...
  public NodeButton()
  {
    TextBox tb = new TextBox()
    {
       Name = ...
       Location = New Point(2,2);
       ...
       Parent = this;
    }
    this.Controls.Add(tb);    

  ..
  }
  ..
}

Upvotes: 0

Views: 1025

Answers (1)

Hans Passant
Hans Passant

Reputation: 941257

this.Controls.Add(tb); 

Don't add the textbox to the button's Controls collection, add it to the button's Parent. Roughly:

tb.Location = new Point(this.Right + 5, this.Top);
this.Parent.Controls.Add(tb);

Do make sure that the Parent is valid, it isn't clear that it might be from the context. If not, or you can't be sure, then overriding OnParentChanged is best. Using a UserControl could be advisable. So is not cramming panels full of controls, that tends to grind your UI to a crawl.

Upvotes: 2

Related Questions