[RESOLVED] Custom Control that you can add other controls too at Design time....
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: [RESOLVED] Custom Control that you can add other controls too at Design time....

  1. #1
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    [RESOLVED] Custom Control that you can add other controls too at Design time....

    This one is a little tricky to explain but i will try..

    Client asked if it posible to create a Form where the controls on it are scrollable, (Done this on a small scale before with placing Holding controls within a Holding control, adding a scrollbar and linking the lot.

    However i'm looking at creating this into a User Control that i can simply Drop into a Form, Drop The controls into it, Set the defaults (Size etc) and go..

    However the problems I've got right out the gate is ...
    #1 The Scroller is not active during design time, so i cant scroll the inner holder..
    #2 Cannot drop other controls into the Usercontrol at design time ..

    I've been mucking around with this now for 4 hours, and could have done it the long way and been finished .. but now that i'm semi committed to this, i want to know if it can be done..)

    My Progress So Far
    Code:
    Public Class ControlScroll
        Private _MinHeight As Integer
    
        Public Property ControlHeight() As Integer
            Get
                Return GroupBox1.Height
            End Get
            Set(ByVal value As Integer)
                If value > Me.Height Then
                    GroupBox1.Height = value
                Else
                    GroupBox1.Height = Me.Height
                End If
                RecalkScrollSize()
            End Set
        End Property
    
        Public Property ControlMinHeight() As Integer
            Get
                Return _MinHeight
            End Get
            Set(ByVal value As Integer)
                _MinHeight = value
            End Set
        End Property
    
        Public Property ControlPosition() As Integer
            Get
                Return VScrollBar1.Value
            End Get
            Set(ByVal value As Integer)
                VScrollBar1.Value = value
            End Set
        End Property
    
        Private Sub ControlScroll_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
            GroupBox1.Controls.Add(e.Data)
        End Sub
    
        Private Sub ControlScroll_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
            VScrollBar1.Top = 0
            VScrollBar1.Left = Me.Width - VScrollBar1.Width
            VScrollBar1.Height = Me.Height
            GroupBox1.Top = -VScrollBar1.Value
            GroupBox1.Left = 0
            GroupBox1.Width = Me.Width - VScrollBar1.Width
            If Me.Height < _MinHeight Then
                GroupBox1.Height = _MinHeight
            Else
                GroupBox1.Height = Me.Height
            End If
            RecalkScrollSize()
        End Sub
    
        Public Sub New()
    
            ' This call is required by the Windows Form Designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
    
            Me.Controls.Add(GroupBox1)
            Me.Controls.Add(VScrollBar1)
            VScrollBar1.Visible = True
            GroupBox1.Visible = True
            GroupBox1.Height = 100
            RecalkScrollSize()
        End Sub
    
        Private Sub VScrollBar1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles VScrollBar1.ValueChanged
            GroupBox1.Top = -VScrollBar1.Value
        End Sub
    
        Private Sub RecalkScrollSize()
            If GroupBox1.Height > Me.Height Then
                VScrollBar1.Enabled = True
                VScrollBar1.Maximum = GroupBox1.Height - Me.Height
                VScrollBar1.LargeChange = Me.Height
                VScrollBar1.SmallChange = 1
            Else
                VScrollBar1.Enabled = False
            End If
        End Sub
    
    End Class
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,100

    Smile Re: Custom Control that you can add other controls too at Design time....

    Quote Originally Posted by GremlinSA View Post
    #2 Cannot drop other controls into the Usercontrol at design time ..
    Hi Gremmy!

    I think the reason for this is that you have not set up your UC to be a container control. I had an issue with this some time ago. What I did was to add this line :

    Code:
    <Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design", GetType(IDesigner))> _
    above my class definition. So it ended up looking like this :

    Code:
    <Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design", GetType(IDesigner))> _
    Public NotInheritable Class HTG_Group
    This made it a Parent control which can contain other controls. In the above case HTG_Group is the name of my UC.

    Just remember that you have to add event handlers as well for the added controls, I did this :

    Code:
        Private Sub HTG_Group_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlAdded
            tmrGB.Enabled = True
            e.Control.BringToFront()
            AddHandler e.Control.Move, AddressOf MoveAddedControl
            gbAddedControl = e.Control
    
        End Sub
    
        Private Sub MoveAddedControl(ByVal sender As Object, ByVal e As System.EventArgs)
            If gbAddedControl.Top <= lblGBHead.Bottom Then gbAddedControl.Top = (lblGBHead.Top + gbAddedControl.Top) + 10
            If gbAddedControl.Left <= 0 Then gbAddedControl.Left = 1
        End Sub
    The trick comes in with the UC's ControlAdded event.

    I just had a look now at the code, and what I did to fix the scrolling problem, your first issue, which I also strangely had ( great minds think alike ) was to make use the UC's AutoScroll property, then there was no need to add scrollbars manually . I had to also make use of a Timer and the Resize event of the UC to see whether or not a new control was added, then scroll to it if necessary.

    I am attaching a project with, and I sincerely hope that it helps.
    Attached Files Attached Files

  3. #3
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Custom Control that you can add other controls too at Design time....

    Thanks to your guidance... i've managed to complete this little task...

    It still needs to be cleaned up a little. but its doing what i need.(more or less)

    When you add the control to a form, add a few control to it.. add controls so that they ago over the right or bottom edge...

    You will see the scrollbars appear...

    Now.. In the Controls Propeties.. Under Misc..
    there is two property's that you can set .. (Designers link to the Scrollers)

    Alter HscrollValue to scroll the controls left to right
    Alter VscrollValue to scroll the controls up and down ..

    Wait .. i did not say run the app .. Change it in the Designer.. Yup .. Scroll the Inner Form in the designer...

    Now i just need to clean it up a little...

    --- EDIT ---
    New download in later post (3 downloads)
    --- EDIT ---
    Last edited by GremlinSA; April 12th, 2011 at 01:30 PM.
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,100

    Re: Custom Control that you can add other controls too at Design time....

    Nice little app you have there!

    Glad I could help you for a change

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Custom Control that you can add other controls too at Design time....

    When the community gives to you .. You give back....

    The control is complete, and appears to be working correctly...

    Here it is ... Hope you all enjoy it...

    ---EDIT---
    I've removed the download (12 downloads) because the download and full explanation is now available in an Article... Guide to Building a VS 2008 User Control in VB
    Last edited by GremlinSA; May 9th, 2011 at 06:09 AM. Reason: Added link to related Article..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,100

    Re: Custom Control that you can add other controls too at Design time....

    Quote Originally Posted by GremlinSA View Post
    When the community gives to you .. You give back....
    Truer words were never spoken

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,100

    Re: Custom Control that you can add other controls too at Design time....

    I do have one little suggestion.

    The control's backcolor is WindowFrame, which is black. When added to a form, and controls dropped onto it, it is difficult to read their text. For example, a checkbox, and radiobutton. Why don't you set the Control's BackColor property to ControlDark or something, as a default?

    Just an idea

  8. #8
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Custom Control that you can add other controls too at Design time....

    Ahh .. now here's the tricky part..

    I specifically set the Default background to something that will contrast the form so that you can properly place and set up the control before adding the rest, and change the ForeColour once it's in the correct place, to just about any colour you like..

    However you are very right in the fact that WindowFrame was the wrong one to use by default.. I've changed it to ControlDark on my side, but i see no need to update the uploaded file for just that one item ...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center