Debugging a VB.NET "Windows control"
Hi,
I created a simple "Windows control", with a few properties, in VB.NET (VS.NET Professional). To be able to debug my control, I added a new "Windows Application" project. I added my "Windows Control" to the references section of the new "Windows Application". After this, I can put my control to the form, so my Windows control can be accessed from the Toolbox. So far so good.
However, when I try to change the properties I added to my control, I cannot. The properties I added are grayed out. Plus, when I run my "Windows Application" my component is not visible at run time either. I checked my code and the user control, and my component on the form, everything is visible.
Did anybody experience a similar problem?
Regards
Battal Abdurrahman
Pericentric Consulting Inc.
are your properties declared as public?
Yes,
They are all public. I can see the properties in the "Properties window" when I select the component I've put on the form. But they are all grayed out and I cannot change their values.
um...how exactly are you referencing the component...did you just right mouse click on references, click on add reference, then select the projects tab, then select the project with your control in it then hit ok?
Exactly,
What I did is:
1. Right click on references,
2. Click on add reference
3. Select the projects tab
4. Select my "windows control" project in it and hit ok
I also tried directly referencing, without debugging whatsoever, and browsed and selected my ".dll" file, and the same thing. The properties are grayed out, I cannot change them, when I run the program my component is not visible.
ok, this has to be something obvious, cuz that's just silly, but it's not coming to mind...
try doing this in the load of your test form
Dim Temp As New MyControl()
Temp.Something = "blah"
Temp.Visible = True
Me.Controls.Add(Temp)
step through that and see what happens
Thanks Erik,
I'll try that.
Here's another thing to check for. It seems like you might have only declared a getter and not a setter for your property. Doing this will make your property read only in the property grid.
- mike
Mike,
There are both getter and setter functions for all the properties.
Erik,
I tried that, and it works fine. When I run the application, my control is visible. However in the design time, my properties are still readonly and grayed out.
Below is the code of my component.
--
Public Class PRTextBox
Inherits System.Windows.Forms.UserControl
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl1 overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
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.
Friend WithEvents lbl As System.Windows.Forms.Label
Friend WithEvents txt As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.lbl = New System.Windows.Forms.Label()
Me.txt = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'lbl
'
Me.lbl.Location = New System.Drawing.Point(0, 8)
Me.lbl.Name = "lbl"
Me.lbl.Size = New System.Drawing.Size(144, 24)
Me.lbl.TabIndex = 0
Me.lbl.Text = "Label1"
'
'txt
'
Me.txt.Location = New System.Drawing.Point(152, 8)
Me.txt.Name = "txt"
Me.txt.Size = New System.Drawing.Size(144, 20)
Me.txt.TabIndex = 1
Me.txt.Text = "TextBox1"
'
'PRTextBox
'
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txt, Me.lbl})
Me.Name = "PRTextBox"
Me.Size = New System.Drawing.Size(344, 40)
Me.ResumeLayout(False)
End Sub
#End Region
Public Property ControlText()
Get
Return txt.Text
End Get
Set(ByVal Value)
txt.Text = Value
End Set
End Property
Public Property ControlCaption()
Get
Return lbl.Text
End Get
Set(ByVal Value)
lbl.Text = Value
End Set
End Property
Public Property LabelWidth()
Get
Return lbl.Width
End Get
Set(ByVal Value)
lbl.Width = Value
ArrangeUI()
End Set
End Property
Public Property TextWidth()
Get
Return txt.Width
End Get
Set(ByVal Value)
txt.Width = Value
ArrangeUI()
End Set
End Property
Private Sub PRTextBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ArrangeUI()
End Sub
Private Sub ArrangeUI()
Me.Visible = True
lbl.Top = 0
lbl.Left = 0
lbl.Height = 20
txt.Left = lbl.Width
txt.Top = 0
txt.Height = 20
Me.Width = lbl.Width + txt.Width
Me.Height = lbl.Height
End Sub
Private Sub PRTextBox_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
ArrangeUI()
End Sub
End Class
--
hhhmmm...maybe this is it...you need to explicitly define what types your properties are...not sure that the property window knows how to deal with objects, which is what your properties are by default so ControlCaption should be this instead...
Public Property ControlCaption() As String
Get
Return lbl.Text
End Get
Set(ByVal Value As String)
lbl.Text = Value
End Set
End Property
Great,
Thanks a lot Erik. It worked now.
That's something I'll never forget from now on.
Thanks again.
yup, unlike VB6, everything should be explicitly declared...glad you got it
There's an easy way to make sure you don't forget:
In the project properties, make sure both Option Explicit and Option Strict are turned on. This way, you code simply won't compile if you don't declare the type of every method, variable, and field you create. In addition, setting Option Strict on ensures that you don't use possibly invalid type coersions. Both settings are on (with no way to turn them off) in C#, and they're a great idea in VB.NET coding as well.
thanks for the info ken. I'll keep that in mind.
Another question though.
We don't need any equivalent of "Propertybags" in VB.NET for persistance, right? couldn't find anything regarding this matter, but my sample control's property values are persistent. I assume that .NET automatically does that.
Is this correct?