Discussion:
Multiline label text?
(too old to reply)
nooboy
2008-03-05 14:02:34 UTC
Permalink
I basically want a label (just plain text on a control). But it should be
multiline which a label can't be as far as I can see. So I made a text
control, disabled it, took away the border, and set the background color to
be the same as the control. Looks fine in the designer, but at runtime the
text is grayed out.

What should I be using here? TIA
Herfried K. Wagner [MVP]
2008-03-05 14:28:45 UTC
Permalink
Post by nooboy
I basically want a label (just plain text on a control). But it should be
multiline which a label can't be as far as I can see.
The Windows Forms label control supports multple lines. Just set the
'AutoSize' property to 'False'. Hard line breaks can be added too:

\\\
Me.Label1.Text = _
"Line 1" & ControlChars.NewLine & _
"Line 2"
///
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
nooboy
2008-03-05 15:20:55 UTC
Permalink
Post by Herfried K. Wagner [MVP]
Post by nooboy
I basically want a label (just plain text on a control). But it should be
multiline which a label can't be as far as I can see.
The Windows Forms label control supports multple lines. Just set the
\\\
Me.Label1.Text = _
"Line 1" & ControlChars.NewLine & _
"Line 2"
///
I was hoping for something simple like that, thanks. Is there any reason
escape characters like \n shouldn't be used?
Jeff Gaines
2008-03-05 15:48:31 UTC
Permalink
Post by nooboy
I was hoping for something simple like that, thanks. Is there any reason
escape characters like \n shouldn't be used?
Portability, DOS and Unix use different line endings. You don't want it to
break when you port it to Linux :-)
--
Jeff Gaines
Kishor Kotecha
2008-03-06 04:11:49 UTC
Permalink
i heard of project mono. sincerely never worked with it. can we say in
general that it is a reality our .net apps can function on linux/unix?
Post by Jeff Gaines
Post by nooboy
I was hoping for something simple like that, thanks. Is there any reason
escape characters like \n shouldn't be used?
Portability, DOS and Unix use different line endings. You don't want it to
break when you port it to Linux :-)
--
Jeff Gaines
Morten Wennevik [C# MVP]
2008-03-06 09:16:01 UTC
Permalink
Post by nooboy
I was hoping for something simple like that, thanks. Is there any reason
escape characters like \n shouldn't be used?
\n is not a valid newline on MS Windows operating systems and you are not
guaranteed it is supported, although usually it works. Use \r\n if you want
to hardcode your program for MS Windows OS, or Environment.Newline
(preferred) to let the compiler decide the correct newline for the current
environment.
--
Happy Coding!
Morten Wennevik [C# MVP]
Herfried K. Wagner [MVP]
2008-03-06 23:33:49 UTC
Permalink
Post by Morten Wennevik [C# MVP]
Post by nooboy
I was hoping for something simple like that, thanks. Is there any reason
escape characters like \n shouldn't be used?
\n is not a valid newline on MS Windows operating systems and you are not
guaranteed it is supported, although usually it works. Use \r\n if you want
to hardcode your program for MS Windows OS, or Environment.Newline
(preferred) to let the compiler decide the correct newline for the current
environment.
Which newline character combination does a Windows Forms implementation for
another operating system than Windows use? I assume it's supposed to use
'[CR][LF]' too because otherwise it would break most existing Windows Forms
applications.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Morten Wennevik [C# MVP]
2008-03-07 06:14:00 UTC
Permalink
Post by Herfried K. Wagner [MVP]
Post by Morten Wennevik [C# MVP]
Post by nooboy
I was hoping for something simple like that, thanks. Is there any reason
escape characters like \n shouldn't be used?
\n is not a valid newline on MS Windows operating systems and you are not
guaranteed it is supported, although usually it works. Use \r\n if you want
to hardcode your program for MS Windows OS, or Environment.Newline
(preferred) to let the compiler decide the correct newline for the current
environment.
Which newline character combination does a Windows Forms implementation for
another operating system than Windows use? I assume it's supposed to use
'[CR][LF]' too because otherwise it would break most existing Windows Forms
applications.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
I suppose using \r\n ([CR][LF]) may break functionality if you run the code
on non windows operating systems, which is why you really should use
Environment.Newline instead. I expect winforms and its controls to
internally use Environment.Newline and according to the docs this will
translate to \n [LF] on unix and \r\n ([CR][LF]) on non unix operating
systems. So, if compiled and run on Unix, winforms/controls should use \n
([LF]). Then again, to complicate matters further, the characters \r and \n
may not even exist on all operating systems and are not guaranteed to mean
[CR] and [LF]

http://en.wikipedia.org/wiki/Newline
--
Happy Coding!
Morten Wennevik [C# MVP]
Johan Waldeck
2010-09-26 18:27:34 UTC
Permalink
This is my field in aspx

My Fieldname is "ItemDescription"

It needs to be a multiline textbox for editing, and it needs to display the same with the linefeeds etc:



<asp:TemplateField HeaderText="Description" >

<EditItemTemplate>

<asp:TextBox ID="ItemDescription" runat="server" TextMode="MultiLine" CssClass="bodyFont" Height="150px" Text='<%# Bind("ItemDescription") %>' Width="300px" />

</EditItemTemplate>


<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Eval("ItemDescription", "{0}") %>'
ondatabinding="Label1_DataBinding" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>


then in my .c file, I have the following:

protected void Label1_DataBinding(object sender, EventArgs e)
{
(sender as System.Web.UI.WebControls.Label).Text = (sender as System.Web.UI.WebControls.Label).Text.Replace("\n", "<br/>");
}

Works beautifully :)
Post by nooboy
I basically want a label (just plain text on a control). But it should be
multiline which a label can't be as far as I can see. So I made a text
control, disabled it, took away the border, and set the background color to
be the same as the control. Looks fine in the designer, but at runtime the
text is grayed out.
What should I be using here? TIA
Post by Herfried K. Wagner [MVP]
The Windows Forms label control supports multple lines. Just set the
\\\
Me.Label1.Text = _
"Line 1" & ControlChars.NewLine & _
"Line 2"
///
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Post by nooboy
I was hoping for something simple like that, thanks. Is there any reason
escape characters like \n should not be used?
Portability, DOS and Unix use different line endings. You do not want it to
break when you port it to Linux :-)
--
Jeff Gaines
Post by Kishor Kotecha
i heard of project mono. sincerely never worked with it. can we say in
general that it is a reality our .net apps can function on linux/unix?
Post by Morten Wennevik [C# MVP]
\n is not a valid newline on MS Windows operating systems and you are not
guaranteed it is supported, although usually it works. Use \r\n if you want
to hardcode your program for MS Windows OS, or Environment.Newline
(preferred) to let the compiler decide the correct newline for the current
environment.
--
Happy Coding!
Morten Wennevik [C# MVP]
Post by Herfried K. Wagner [MVP]
Which newline character combination does a Windows Forms implementation for
another operating system than Windows use? I assume it's supposed to use
'[CR][LF]' too because otherwise it would break most existing Windows Forms
applications.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Post by Morten Wennevik [C# MVP]
I suppose using \r\n ([CR][LF]) may break functionality if you run the code
on non windows operating systems, which is why you really should use
Environment.Newline instead. I expect winforms and its controls to
internally use Environment.Newline and according to the docs this will
translate to \n [LF] on unix and \r\n ([CR][LF]) on non unix operating
systems. So, if compiled and run on Unix, winforms/controls should use \n
([LF]). Then again, to complicate matters further, the characters \r and \n
may not even exist on all operating systems and are not guaranteed to mean
[CR] and [LF]
http://en.wikipedia.org/wiki/Newline
--
Happy Coding!
Morten Wennevik [C# MVP]
Select Text and click on the drop down arrow at right end
now type first line and press Enter
then type 2nd line and press Enter
follow the above steps
label2.Text = "Line1\r\nLine 2";
or,
label3.Text = "L3 Line 1" + Environment.NewLine + "L3 Line 2";
or,
label4.Text = "L4Line 1 \n L4Line 2";
Select Text and click on the drop down arrow at right end
now type first line and press Enter
then type 2nd line and press Enter
follow the above steps
label2.Text = "Line1\r\nLine 2";
or,
label3.Text = "L3 Line 1" + Environment.NewLine + "L3 Line 2";
or,
label4.Text = "L4Line 1 \n L4Line 2";
Submitted via EggHeadCafe - Software Developer Portal of Choice
ASP.NET HttpPostedFile Image Resizer
http://www.eggheadcafe.com/tutorials/aspnet/ba8d2418-6d67-40f7-989c-e90688058778/aspnet-httppostedfile-image-resizer.aspx
Loading...