问题
I have created a simple program. One of the functions is that it counts clicks, and when the program finishes, a label is displayed with some text and the number of clicks. The code works, but the problem is that the label displays extra text, like this: system.windows.forms.label text
Stevec is set to count clicks. Lable konec already contains text, and in this case I just want to add the extra sentence with the number of clicks.
konec.Text = konec + " Kliknil si: " + Stevec.ToString() + "-krat!";
If anyone has any idea how to display only the text that I have set in the code, I would appreciate it!
回答1:
You need the Text property instead of the control itself:
konec.Text = konec.Text + " Kliknil si: " + Stevec.ToString() + "-krat!";
回答2:
The reason that you have unwanted text is because of this part:
...konec +...
+ operator used with string implicitly converts other operand to string calling its ToString() method. This way, above part is equivalent to ...konec.ToString() + ... (which results in unwanted text), but what you want is ...konec.Text + ..., that is why your code should use konec.Text instead of just konec object.
来源:https://stackoverflow.com/questions/49716416/winforms-system-windows-forms-label-text-displayed-as-text-in-label