Name of user control
Why does this return an empty string when used in the same user control class?
Private CaptionString As String = Me.Name
Later in the same class, CaptionString returns an empty string.
In VB6 I used the Extender object to retrieve the name of the control, but the code above doesn't work. It will only return an empty string when I really want e.g. Button1, Button2 etc.
[862 byte] By [
Svennis] at [2007-12-25]
The form using the user control will need code
Similar to this:
Option Strict Off
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
WalkControls(Me)
End Sub
Private Sub WalkControls(ByVal obj As Object)
Dim c As Control
For Each c In obj.controls
If TypeOf (c) Is buttonForum.newbutton Then
Dim n As buttonForum.newbutton = DirectCast(c, buttonForum.newbutton)
n.Caption = n.Name
End If
WalkControls(c)
Next
End Sub
End Class
The caption cannot be set inside the user control
To the name that it will become. It will have
To be set as shown above.
Also the label should be created in the User
Control designer, not inside the regular code.
Your ‘Caption.click / Raise.event’ stuff I don’t
Understand.
Also, I used ‘newbutton’ instead of ‘button’ to
Avoid mixing things up.
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class newbutton
Inherits System.Windows.Forms.UserControl
'UserControl overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.CaptionLabel = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(32, 90)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'CaptionLabel
'
Me.CaptionLabel.AutoSize = True
Me.CaptionLabel.Location = New System.Drawing.Point(29, 35)
Me.CaptionLabel.Name = "CaptionLabel"
Me.CaptionLabel.Size = New System.Drawing.Size(69, 13)
Me.CaptionLabel.TabIndex = 1
Me.CaptionLabel.Text = "CaptionLabel"
Me.CaptionLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Button
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.Controls.Add(Me.CaptionLabel)
Me.Controls.Add(Me.Button1)
Me.Name = "Button"
Me.Size = New System.Drawing.Size(232, 199)
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents CaptionLabel As System.Windows.Forms.Label
End Class
Public Class newbutton
Public Shadows Event Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Private CaptionLabel As New Label
Private CaptionString As String = Me.GetType.Name
Private Sub Button_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
EquipCaption()
End Sub
#Region "Equipments"
Sub EquipCaption()
With CaptionLabel
.AutoSize = False
.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
.BackColor = System.Drawing.Color.Transparent
End With
End Sub
#End Region
Private Sub Button_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
With CaptionLabel
' .Size = Me.ClientSize
.Text = Caption
End With
End Sub
#Region "Properties"
<System.ComponentModel.Category("Appearance")> _
Property Caption() As String
Get
Return CaptionString
End Get
Set(ByVal Value As String)
CaptionString = Value
Me.Refresh()
End Set
End Property
#End Region
Private Sub CaptionLabel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CaptionLabel.Click
RaiseEvent Click(sender, e)
End Sub
End Class
I am not an expert on this but it seems that when
the using program adds a 'newbutton' in it's
designer, the code looks something like this:
Me
.Newbutton1 =
New buttonForum.newbutton
My limited knowledge says there is no way in a
'newbutton's constructor to know who ,(Newbutton1), is
asking for it to be created. It seems it has to be done
after the fact.
I have not used any GUI programming languages previously
(before VB EE.)
I took the time to go through the help tutorial before replying
to your question:
ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_mclictl/html/f50e270e-4db2-409a-8319-6db6ca5c7daf.htm
So I finally got to see the MS recommended way to create a user control,
which at this point looks pretty good.
Perhaps you are right. I have limited knowledge in this area.
ReneeC, Spotty, NoBugz, DMan, 'Big A' (Ahmedilyas), others ? What do you think