문제

In my C# code, I am having trouble implementing a System.Windows.Forms.Label into my class. I am trying to achieve a label with a transparent background so I can put this on a transparent window to have 'floating' text.

Here is my code:

72|    public class floatingText : System.Windows.Forms.Label
73|    {
74|        this.Text = "Words go here";
75|        this.TextAlign = System.Drawing.ContentAlignment.TopCenter;
76|        this.ForeColor = Color.WhiteSmoke;
77|        this.BackColor = Color.Black;
78|        this.TransparencyKey = Color.Black;
79|    }

This code gives me the following compile errors:

74|    Invalid token '=' in class, struct, or interface member declaration (CS1519)
74|    Invalid token 'this' in class, struct, or interface member declaration (CS1519)
75|    Invalid token ';' in class, struct, or interface member declaration (CS1519)
75|    Invalid token '=' in class, struct, or interface member declaration (CS1519)
76|    Invalid token ';' in class, struct, or interface member declaration (CS1519)
76|    Invalid token '=' in class, struct, or interface member declaration (CS1519)
77|    Invalid token ';' in class, struct, or interface member declaration (CS1519)
77|    Invalid token '=' in class, struct, or interface member declaration (CS1519)
78|    Invalid token ';' in class, struct, or interface member declaration (CS1519)
78|    Invalid token '=' in class, struct, or interface member declaration (CS1519)


I am new to C# and have absolutely no idea what is going on here, so if you could explain this in the simple terms, that would be much appreciated ;). I am using SharpDevelop 4.3.3 and am running Windows XP Professional

도움이 되었습니까?

해결책

You could set the properties in a constructor like so:

public class floatingText : System.Windows.Forms.Label
{
    public floatingText()
    {
        this.Text = "Words go here";
        this.TextAlign = System.Drawing.ContentAlignment.TopCenter;
        this.ForeColor = Color.WhiteSmoke;
        this.BackColor = Color.Black;
        this.TransparencyKey = Color.Black;
    }
}

Labels don't have a TransparencyKey so you may want to change the last line or did you mean to set this on the Form?

다른 팁

You shouldn't be setting properties. You should be overriding them

For example, to override Text

public override string Text
{
    get
    {
        return "Words go here";
    }
    set
    {
        base.Text = value;
    }
}

For TextAlgin

public override System.Drawing.ContentAlignment TextAlign
{
    get
    {
        return System.Drawing.ContentAlignment.TopCenter;
    }
    set
    {
        base.TextAlign = value;
    }
}

And so on... (You could remove the set if you don't want them to be changed)

Another work around will be to put those statements inside the constructor of your class.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top