How to dynamically create controls aligned to the top but after other aligned controls?

Once again, DisableAlign and EnableAlign to the rescue: procedure TForm1.FormCreate(Sender: TObject); var I: Integer; P: TPanel; begin DisableAlign; try for I := 0 to 4 do begin P := TPanel.Create(Self); P.Caption := IntToStr(I); P.Align := alTop; P.Parent := Self; end; finally EnableAlign; end; end; Explanation: When alignment is enabled, every single addition of a control … Read more

How to create a WPF UserControl with NAMED content

The answer is to not use a UserControl to do it. Create a class that extends ContentControl public class MyFunkyControl : ContentControl { public static readonly DependencyProperty HeadingProperty = DependencyProperty.Register(“Heading”, typeof(string), typeof(MyFunkyControl), new PropertyMetadata(HeadingChanged)); private static void HeadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((MyFunkyControl) d).Heading = e.NewValue as string; } public string Heading { get; set; … Read more

Find a control in Windows Forms by name

You can use the form’s Controls.Find() method to retrieve a reference back: var matches = this.Controls.Find(“button2”, true); Beware that this returns an array, the Name property of a control can be ambiguous, there is no mechanism that ensures that a control has a unique name. You’ll have to enforce that yourself.

Why will expressions as property values on a server-controls lead to a compile errors?

This: <asp:Button runat=”server” id=”Button1″ visible=”<%= true %>” /> Does not evaluate to this: <asp:Button runat=”server” id=”Button1″ visible=”true” /> <%= %> outputs directly to the response stream, and the asp markup is not part of the response stream. Its a mistake to assume the <%= %> operators are performing any kind of preprocessing on the asp … Read more

Word wrap for a label in Windows Forms

Actually, the accepted answer is unnecessarily complicated. If you set the label to AutoSize, it will automatically grow with whatever text you put in it. (This includes vertical growth.) If you want to make it word wrap at a particular width, you can set the MaximumSize property. myLabel.MaximumSize = new Size(100, 0); myLabel.AutoSize = true; … Read more

How to access a form control for another form?

Making them Singleton is not a completely bad idea, but personally I would not prefer to do it that way. I’d rather pass the reference of one to another form. Here’s an example. Form1 triggers Form2 to open. Form2 has overloaded constructor which takes calling form as argument and provides its reference to Form2 members. … Read more