Results 1 to 7 of 7

Thread: [RESOLVED] Center vertical align controls with UserControl in designer

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Resolved [RESOLVED] Center vertical align controls with UserControl in designer

    I have a UserControl that include several TextBoxes spaced horizontally. In the designer, the TextBox, Label, Button, CheckBox, etc. all will center align with each other. You notice the blue alignment guide lines change to magenta when the controls are centered with each other. Can this be done with a UserControl?

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Center vertical align controls with UserControl in designer

    Can you expand more? I did not quite get what you wanted.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Center vertical align controls with UserControl in designer

    Quote Originally Posted by wey97 View Post
    I have a UserControl that include several TextBoxes spaced horizontally. In the designer, the TextBox, Label, Button, CheckBox, etc. all will center align with each other. You notice the blue alignment guide lines change to magenta when the controls are centered with each other. Can this be done with a UserControl?
    That pink line doesn't indicate centre alignment. It indicates that the controls' text baseline is aligned, i.e. the bottom of the text in the two controls will be aligned.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: Center vertical align controls with UserControl in designer

    Quote Originally Posted by jmcilhinney View Post
    That pink line doesn't indicate centre alignment. It indicates that the controls' text baseline is aligned, i.e. the bottom of the text in the two controls will be aligned.
    I see what you mean but it's effectively the same thing. The one line controls text align with each other and the text is centered with the control input.

    I'm wondering how this is accomplished so I can align single line controls with this control in the designer.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Center vertical align controls with UserControl in designer

    It's not effectively the same thing at all. Try aligning two controls with different font sizes that way. Also, as you're aware, not all controls have a text baseline.

    I'm not sure how you define a text baseline for a control but, if what you want is to align by centre, it's irrelevant anyway. There are buttons on the designer toolbar that allow you to do things like that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Center vertical align controls with UserControl in designer

    I've never used it before, but there are two properties in a control designer that may be useful:
    - ParticipatesWithSnapLines (boolean)
    - SnapLines (IList)


    You will need to give your UserControl a custom designer and override those properties. What you need to do in the properties I'm not really sure, but I'm sure MSDN or google can give you some information.

    To create a custom designer you first need to add a reference to System.Design.
    Then you create a class that inherits ControlDesigner and let it override the two properties (possibly more, there's loads you can do with control designers!). Finally you need to tell your UserControl to use your custom designer, which you do by using the Designer attribute (see line 7).

    Here's a very quick example:
    csharp Code:
    1. using System.Windows.Forms;
    2. using System.Windows.Forms.Design;
    3. using System.Windows.Forms.Design.Behavior;
    4.  
    5. namespace SnapLinesTest
    6. {
    7.     [Designer(typeof(UserControlDesigner))]
    8.     public partial class UserControl1 : UserControl
    9.     {
    10.         public UserControl1()
    11.         {
    12.             InitializeComponent();
    13.         }
    14.  
    15.         internal class UserControlDesigner : ControlDesigner
    16.         {
    17.             public override bool ParticipatesWithSnapLines
    18.             {
    19.                 get
    20.                 {
    21.                     return true;
    22.                 }
    23.             }
    24.  
    25.             public override System.Collections.IList SnapLines
    26.             {
    27.                 get
    28.                 {
    29.                     List<SnapLine> lines = new List<SnapLine>();
    30.                     lines.Add(new SnapLine(SnapLineType.Horizontal, 5));
    31.                     lines.Add(new SnapLine(SnapLineType.Left, 3));
    32.                     return lines;
    33.                 }
    34.             }
    35.         }
    36.     }
    37. }
    I just guessed that you could simply return a List(Of SnapLine) with a few new SnapLine objects. I have no idea how to control the snaplines exactly, when I tried this I got no snaplines whatsoever.. But I'm sure you can get it to work.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: Center vertical align controls with UserControl in designer

    Nick, you got me on the right track.

    You need to cast base.SnapLines to an ArrayList to manipulate it. I added an arbitrary baseline at the bottom of one of the TextBoxes:


    CSharp Code:
    1. [Designer(typeof(SnapLineTestControlDesigner))]
    2. public partial class SnapLineTest : UserControl
    3. {
    4.     public SnapLineTest() {
    5.         InitializeComponent();
    6.     }
    7.  
    8.     class SnapLineTestControlDesigner : ControlDesigner
    9.     {
    10.         public override IList SnapLines {
    11.             get {
    12.                 ArrayList snapLines = base.SnapLines as ArrayList;
    13.                 SnapLineTest control = Control as SnapLineTest;
    14.  
    15.                 snapLines.Add(new SnapLine(SnapLineType.Baseline, control.txtName.Top + control.txtName.Height));
    16.  
    17.                 return snapLines;
    18.             }
    19.         }
    20.     }
    21. }


    That wasn't quite it, so it occurred to me that if I could access the SnapLines in the TextBox, I could use those properties for my new SnapLine. This would adjust for font changes as jmcilhinney noted:

    CSharp Code:
    1. [Designer(typeof(SnapLineTestControlDesigner))]
    2. public partial class SnapLineTest : UserControl
    3. {
    4.     public SnapLineTest() {
    5.         InitializeComponent();
    6.     }
    7.  
    8.     class SnapLineTestControlDesigner : ControlDesigner
    9.     {
    10.         public override IList SnapLines {
    11.             get {
    12.                 ArrayList snapLines = base.SnapLines as ArrayList;
    13.                 SnapLineTest control = Control as SnapLineTest;
    14.  
    15.                 if(control != null) {
    16.                     using(ControlDesigner controlDesigner = TypeDescriptor.CreateDesigner(control.txtName, typeof(IDesigner)) as ControlDesigner) {
    17.  
    18.                         if(controlDesigner != null) {
    19.                             controlDesigner.Initialize(control.txtName);
    20.  
    21.                             foreach(SnapLine line in controlDesigner.SnapLines) {
    22.                                 if(line.SnapLineType == SnapLineType.Baseline) {
    23.                                     snapLines.Add(new SnapLine(line.SnapLineType, line.Offset + control.txtName.Top, line.Filter, line.Priority));
    24.                                 }
    25.                             }
    26.                         }
    27.                     }
    28.                 }
    29.  
    30.                 return snapLines;
    31.             }
    32.         }
    33.     }
    34. }
    Last edited by wey97; Jan 14th, 2010 at 12:47 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width