Question : Problem: custom control, container

hi,

I designed a custom control (CustomControl1) and would like to show it at desired position (regarding to the whole screen, not any specific form) by setting the .Location property and call Show() and Hide()

there are three problems:

1).  In a main form, I created a CustomControl1 variable by drag and drop, I can show or hide the control, but within the main form's boundary.   Can I show the control outside the form by setting .Location of the control?
 
2).  Can I create a CustomControl1 dynamically?  I declared a form level variable as CustomControl1 and setup some properties in form's load event,  but I cannot see this control when I call its Show() method.

3).  in the code generated by control designer,  in Sub InitializeComponent(), what's the use of this "components" variable?   I tried to get the .Container property of CustomControl1, but got a null reference exception.   Is this container important? and how do I make use of it?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
Public Class CustomControl1
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
 
        'Add your custom paint code here
        Dim rect As Rectangle = Me.ClientRectangle
 
        Dim drawPen As New Pen(Color.Blue, 2)
        e.Graphics.DrawRectangle(drawPen, rect)
 
        Dim drawBrush As New System.Drawing.Drawing2D.LinearGradientBrush( _
        rect, Color.Blue, Color.Gold, 45)
        e.Graphics.FillRectangle(drawBrush, rect)
 
    End Sub
End Class
 
 
 
Public Class MainForm
 
    Private WithEvents obj2 As CustomControl1
 
    Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        obj2 = New CustomControl1
        obj2.Location = Me.Location + New Point(30, 30)
        obj2.Size = New Size(60, 60)
        obj2.Visible = False
    End Sub
End Class
 
 
    'Required by the Control Designer
    Private components As System.ComponentModel.IContainer
 
    ' NOTE: The following procedure is required by the Component Designer
    ' It can be modified using the Component Designer.  Do not modify it
    ' using the code editor.
     _
    Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub
Open in New Window Select All

Answer : Problem: custom control, container

Just for your clarifications
1) You can not show a control outside a form's boundry because form is the main container.
2) You need to add the control to the form by using the formname.Controls.add(control) syntax before it can be rendered by the form.
3) After the control is added to the form, the container property should refer to the form.

Using a form is the only option for you in this scenario.
Random Solutions  
 
programming4us programming4us