[RESOLVED] Shadowing Text Property
So I'm trying to do just as the title says.. Unfortunately every one of my attempts doesn't work.
It works, but doesn't save what I've changed it to. In the designer everything works fine. I can change the text and have it update on my button control. BUT once I run or rebuild in any way, the text property changes back to it's default value, in my case "Button Control"
These are what I've tried, and I've tried all with both Shadows and Overrides - both with identical outcomes. So I've just indicated that with Shadows/Overrides.
vbnet Code:
<DefaultValue("Button Control"), Browsable(True)> _
Public Shadows/Overrides Property Text As String
Get
Return MyBase.Text
End Get
Set(ByVal value As String)
MyBase.Text = value
Me.Invalidate()
End Set
End Property
Private _Text As String = "Button Control"
<DefaultValue("Button Control"), Browsable(True)> _
Public Shadows/Overrides Property Text As String
Get
Return Me._Text
End Get
Set(ByVal value As String)
Me._Text = value
Me.Invalidate()
End Set
End Property
Re: Shadowing Text Property
Not really sure but I think you may need to add DesignerSerializationVisibility. ?
This is basically how I did one of my buttons, when I add the control to a form the text property is the same as the control name just like how a VB button works when added to a form...
Code:
Imports System.ComponentModel
Public Class MyButton
' Text Property
<EditorBrowsable(EditorBrowsableState.Always)> _
<Browsable(True)> _
<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Public Overrides Property Text As String
Get
Return MyBase.Text
End Get
Set(ByVal value As String)
MyBase.Text = value
Me.Invalidate()
End Set
End Property
End Class
Re: Shadowing Text Property
I think just using MyBase.Text, as in Edgemeal's example, will be sufficient to ensure the property will be serialized correctly. At least I found that to be true recently when overloading the Image and BackgroundImage of an inherited PictureBox. For example:
vb Code:
Public Overloads Property Image As Image
Get
Return MyBase.Image
End Get
Set(value As Image)
MyBase.Image = value
ElementHost.SetImage(value)
End Set
End Property
BB
Re: Shadowing Text Property
vbnet Code:
<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Did the trick, thanks!
I still haven't gotten to that point of utilizing attributes, serializations or interfaces yet. Still migrating to .net unfortunately and I learn by example.. At this point I haven't really seen much using either of the three. Until now! :) Can you possible explain more n depth what exactly this does and why it solved my problem?
Re: Shadowing Text Property
What is it that you're actually trying to achieve here? Just call Invalidate each time the Text is set? If so then you don't need to touch the Text property. You simply override the OnTextChanged method, which raises the TextChanged event. As you can imagine, it gets called from inside the Text property setter. You simply override the method and call the base implementation, then add your code either before or after that call, depending on whether you want your code executed before or after the event handlers.
Re: Shadowing Text Property
Quote:
Originally Posted by
jmcilhinney
What is it that you're actually trying to achieve here? Just call Invalidate each time the Text is set? If so then you don't need to touch the Text property. You simply override the OnTextChanged method, which raises the TextChanged event. As you can imagine, it gets called from inside the Text property setter. You simply override the method and call the base implementation, then add your code either before or after that call, depending on whether you want your code executed before or after the event handlers.
hmmmm.. I hadn't thought of this. Duly noted, thanks!
I originally did this there because I had something else in the set besides just the invalidation. I did a little cleaning work after having taken a break from the control and went off to learn more. Returning with more knowledge than before, I probably figured a way of dealing without whatever it was I had in there and just removed it and left everything else behind. Now, today I came back wanting to fix any inconsistent naming's I had done prior. I had 'Caption' which as you can tell I'm still pretty fresh out of vb6. Which is what I'm trying to get away from. So I changed it to text, and then began to encounter problems I didn't have answers to.
Re: Shadowing Text Property
Quote:
Originally Posted by
DavesChillaxin
vbnet Code:
<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Did the trick, thanks!
You'd have to look them up on MSDN, All I remember is I started out with a winform project, added new item, selected User Control (to create a custom button) and needed a way to use and set the Text property at design time, Some things I found while testing...
EditorBrowsableState.Always) Not needed for .Text property, but if set to Never then .Text won't be listed in IntelliSense.
Browsable(True) Exposes the Text property in properties window.
DesignerSerializationVisibility.Visible Remembers the text set in the properties window at design time.