Results 1 to 7 of 7

Thread: [RESOLVED] Shadowing Text Property

  1. #1

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Resolved [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:
    1. <DefaultValue("Button Control"), Browsable(True)> _
    2.     Public Shadows/Overrides Property Text As String
    3.         Get
    4.             Return MyBase.Text
    5.         End Get
    6.         Set(ByVal value As String)
    7.             MyBase.Text = value
    8.             Me.Invalidate()
    9.         End Set
    10.     End Property
    11.  
    12.     Private _Text As String = "Button Control"
    13.     <DefaultValue("Button Control"), Browsable(True)> _
    14.     Public Shadows/Overrides Property Text As String
    15.         Get
    16.             Return Me._Text
    17.         End Get
    18.         Set(ByVal value As String)
    19.             Me._Text = value
    20.             Me.Invalidate()
    21.         End Set
    22.     End Property
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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

  3. #3
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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:
    1. Public Overloads Property Image As Image
    2.     Get
    3.          Return MyBase.Image
    4.     End Get
    5.     Set(value As Image)
    6.          MyBase.Image = value
    7.          ElementHost.SetImage(value)
    8.     End Set
    9. End Property

    BB

  4. #4

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Shadowing Text Property

    vbnet Code:
    1. <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?
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Shadowing Text Property

    Quote Originally Posted by jmcilhinney View Post
    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.
    Last edited by DavesChillaxin; Dec 4th, 2011 at 10:23 PM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Shadowing Text Property

    Quote Originally Posted by DavesChillaxin View Post
    vbnet Code:
    1. <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.
    Last edited by Edgemeal; Dec 5th, 2011 at 02:33 PM. Reason: updated

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width