Yuriy F
Yuriy F

Reputation: 361

How to set the .Text property of a label from a "string" + stringVariable

I am working on the last form in a multi-formed Winforms application in C#. On the last form, I display schedule information of the user (what the user enters in the forms between the first and last one). Some of the information I am trying to display is a display Label that says something like "Student Schedule for " + firstName. I retrieve the first name from the first user form and save it locally in the method. I have verified that the string variable isn't empty; it actually contains the first name. However, when I try to set the text of a label to "Student Schedule for " + firstName, I get just "Schedule" as the label text.

firstName = "Tango"; //passed in from first user form, verified correct
sampleLbl.Text = "Student Schedule for " + firstName;

I expect the label text to read "Student Schedule for Tango", but it only shows "Student".

I have also tried

sampleLbl.Text = new string("Student Schedule for " + firstName);

This gives me an error that I 'cannot convert from string to char'.

Also, a side note.. The Label that doesn't display the right text is one that I added programmatically using

Label sampleLbl = new Label(); 
sampleLbl.Text = "Student Schedule for " + firstName;

When I added a label in the designer and change the text, it displays the correct text (ie. Student Schedule for Tango).

I realize this is probably a simple error and would appreciate help anyway. Thank you in advance.

Upvotes: 1

Views: 260

Answers (1)

Sunil
Sunil

Reputation: 3424

Set AutoSize to true for the label, it may be displaying correct text, but since it's not AutoSize, it hidden on the form and you can't see.

Label sampleLbl = new Label(); 
sampleLbl.AutoSize = true;

Upvotes: 1

Related Questions