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]
# 1

I think we need to see more of the code..... is Me.name = "form1"?

ReneeC at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

Me is the user control.

Public Class Button

Private CaptionString As String = Me.Name

<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

Private Sub Button_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

With CaptionLabel

.Text = Caption <-- This is empty!

End With

End Sub

Svennis at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

Add this code:

Private Sub button_Load(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles Me.Load

CaptionString = Me.Name

End Sub

TallDude at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4
In design time the control caption will read "Button" no matter how many buttons I populate the form with, but when I run the application, the button captions will read "Button1", "Button2" etc. So I'm still not getting what I'm looking for :)
Svennis at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5

Could it be because you never fill in the button's name property?

ReneeC at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6

I Know I'm doing something wrong, so here's the entire class:

Public Class Button

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 = ContentAlignment.MiddleCenter

.BackColor = Color.Transparent

End With

Me.Controls.Add(CaptionLabel)

AddHandler CaptionLabel.Click, AddressOf Caption_Click

End Sub

#End Region

Private Sub Caption_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

RaiseEvent Click(sender, e)

End Sub

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

End Class

Svennis at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 7

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

TallDude at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 8
Thank you for your help, but shouldn't the Button class be able to name itself like it did in VB6 with the Extender object? What if the Button class was a compiled control? By the way, the reason why I have the label code in the "general" code section is because I'm not using the form designer. I'm typing in all code by hand.
Svennis at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 9

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

TallDude at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 10
I've got half an answer. At design-time, the name of the control is accessible through its Site.Name property:

Private Sub Button_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
With CaptionLabel
.Size = Me.ClientSize
If Me.DesignMode AndAlso Caption = "" Then
.Text = Me.Site.Name
Else
.Text = Caption
End If
End With
End Sub

What I couldn't figure out, and I've researched it for a while, is how to detect that the Name property was changed in the property grid. I couldn't find any event that would allow you to force the user control to repaint itself. You might have more luck in the Windows Forms Designer forum...

nobugz at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 11

Okay, with all my Form1 'walkcontrols' stuff

commented out. The following change to the

control's paint event does the trick.

Dim ot As Boolean = True

Private Sub Button_Paint(ByVal sender As Object, _

ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

If ot = True Then

With CaptionLabel

.Size = Me.ClientSize

If Caption = "newbutton" Then

ot = False

Dim xx As newbutton = DirectCast(sender, newbutton)

'Dim zz As String = xx.Name

.Text = xx.Name

Button_Paint(sender, e)

'CaptionLabel.Invalidate()

ot = True

Else

.Text = Caption

End If

End With

End If

End Sub

TallDude at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 12

Svennis,

Let me know if you need zip copy of the project,

reflecting the last change.

TallDude at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...