[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
Re: Custom Control that you can add other controls too at Design time....
Originally Posted by GremlinSA
#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 :
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.
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?
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 ...
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.