Wrapping label text in C#

L

le_mo_mo

Hi,

I am trying to have one label on a form with multi line display of the
results from a database query. so my query looks like

Select FirstName+" "+LastName+"\r\n"+Address as Info

I want to have the label show

FirstName LastName
Address

I thought that \r\n in the returned result will make it happen but
Label shows up as

FirstName LastName\r\nAddress

Anybody knows how the string should e cosntructed so that the label
text is wrapped where is needed?

Thanks
 
G

Guest

Hi le_mo_mo,
instead of using \r\n use System.Environment.NewLine instead. This will
substitute in the correct newline characters for the system you are running
the code on.

Hope that helps
Mark R Dawson
http://www.markdawson.org
 
D

Danny Tuppeny

I am trying to have one label on a form with multi line display of the
results from a database query. so my query looks like

Select FirstName+" "+LastName+"\r\n"+Address as Info

This would add a literal "\r\n" to the string, which would be the same as
doing:

label1.Text = "Danny\\r\\nTuppeny";

IIRC, you can use linebreaks in SQL, so maybe something like

SELECT FirstName + " " + LastName + "
" + Address as Info

would work?

Alternatively, you could (though it's pretty bad ;)) replace literal \r\n
with real linebreaks:

label1.Text = dr["Info"].Replace("\\r\\n", "\r\n");
 
G

Guest

I would not recommend writing:
Select FirstName + ' ' + LastName + char(10)+char(13) <or is it 13 then
10...double check> + Address as info

That is not very readable or portable. System.Environment.NewLine should be
used
 
M

Mo

Hi,

The string has to be built into the query so I am not sure how to embed
the System codes in the Query. This is a Windows application and I have
tried variations of \\r\\n, \r\n, <br> and they do not work. I get the
basic string in the output. I am going to try CHR(10) Chr(13) and see
how it works.

Thanks,
Mo
 
M

Mo

Ok, chr(10)+chr(13) does not work neither so far the following does not
wrap the text in a C# label
\r\n
\\r\\n
chr(10)char(13)

I still have not been able to figure out how to embed the
System.Environment.Newline in my query in the database! any ideas on
this is greatly appreciated.

Mo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top