Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello, I have an UserControl in my From Project,

I have this Code in the UserControl:
C#
public partial class iktMarquee : UserControl
{
    #region Properties
    private string _Text = "";//This parameter will change when changing property 'Text' in property manager in a form
    private int charIndex = 0;
    private Color _FontColor = Color.White;

    [Description("Sets Marquee Text"), DefaultValue(""), Category("Vales"), Browsable(true)]

    public string Text
    {
        get { return _Text; }
        set { _Text = value; }
    }

    [Description("Sets Marquee Font Color"), Category("Appearance"), DefaultValue("White"),  Browsable(true)]
    public Color FontColor
    {
        get { return _FontColor; }
        set { _FontColor = value; }
    }

    [Description("Sets Marquee Interval"), Category("Behavior"), DefaultValue(1000),  Browsable(true)]
    public int Interval
    {
        get { return ScrollTimer.Interval; }
        set { ScrollTimer.Interval = value; }
    }
    #endregion

    #region Control Start
    public iktMarquee()
    {
        InitializeComponent();

    }

    private void iktMarquee_Load(object sender, EventArgs e)
    {

    }
    #endregion

    #region Painting
    public void RePaint()
    {
        System.Drawing.Pen myPen;
        myPen = new System.Drawing.Pen(_FontColor);
        System.Drawing.Graphics formGraphics = this.CreateGraphics();
        formGraphics.Clear(this.BackColor);
        formGraphics.DrawString(_Text.Remove(0,charIndex), this.Font, new SolidBrush(this.FontColor), 5,5);
        myPen.Dispose();
        formGraphics.Dispose();

    }
    #endregion

    #region Private Functions
    private void ScrollTimer_Tick(object sender, EventArgs e)
    {
        //charIndex++;
        if (charIndex > _Text.Length)
            charIndex = 0;
        RePaint();
    }

    private void iktMarquee_Resize(object sender, EventArgs e)
    {
        RePaint();
    }
    #endregion

    private void iktMarquee_Paint(object sender, PaintEventArgs e)
    {
        RePaint();
    }
}


Then I have built the project, then set the 'Text' value in UserControl to 'Teststring '

When I Start in debug mode, the 'Text' gets reset to '', while it should be 'Teststring'.

How can I fix this?

Thanks!

Edit:
Posted full code of UserControl
Posted
Updated 7-Feb-12 10:54am
v2
Comments
Sergey Alexandrovich Kryukov 7-Feb-12 16:47pm    
Why your code sample is broken at this point? Could you fix it?
--SA
ikkentim 7-Feb-12 17:08pm    
posted full code

The user control already has a text property, it's just hidden. Override and decorate with the correct attributes and you can use it without any added fields etc:
C#
[Bindable(true),
Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
EditorBrowsable(EditorBrowsableState.Always)]
public override string Text
{
    get { return base.Text; }
    set { base.Text = value; }
}
 
Share this answer
 
Comments
VJ Reddy 1-Jun-12 19:46pm    
Good answer. 5!
There is no a concept of "default property value" in .NET, because it is hardly needed as an universal concept.

You should introduce such behavior on the level of your particular class. Just add and implement some methods like Reset(), SetThisToDefault(), ResetThat() or anything like that to your liking.

Now, your problem is different: you code sample is incomplete, so I cannot tell where is the problem, but right now I can see only the Text getter, and not setter. If there is no setter, or a setter is private, you have no a way to assign a value to the property, it will always remain an empty string.

Another flaw (which does not compromise functionality but really bad for maintenance): never hard-code and immediate constants, especially strings, except null, of course. In particular, never write "", always use string.Empty. The default value is null.

—SA
 
Share this answer
 
v2
Comments
ikkentim 7-Feb-12 17:08pm    
How I need to make that setter? Isn't ... set { _Text = value; } ... right then?
DaveyM69 7-Feb-12 17:26pm    
All properties have a default value, the value depends on the type. In general, for value types it is 0 or the type's equivalent, for reference types it is null.

In the case of user controls, the default value can be set using the DefaultValue attribute, and a TypeConverter if one is needed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900