Line break in a windows application

N

Nurchi BECHED

Hello, All!

I have created an application with a multiline textbox on the form.
When I press a button, it has to show something and then break
the line and show something else.
I tried "\n", ((char)13) and ((char)10) - they don't work.
When I enter a break by hand (just press enter) and run through
each character in that string, the line break character has code
13 10 (yes, 2 numbers for some reason)
The possible reason is that 'Enter' is a "control" key (a char with
ASCII code less than 32) - they usually return 2 numbers...

What should I do to break line in the textbox?
Alternative way was to use TextWriter:
string LineBreak=(new System.IO.StringWriter()).NewLine;

but there must be another way of doing it...
and then just use that string:
this.TextBox1.AppendText("Hello"+LineBreak+"World");

That is kind of stupid...

With best regards, Nurchi BECHED.
 
G

Guest

What happens when you do this?:

this.TextBox1.AppendText("Hello"+Environment.NewLine+"World");
 
H

Hemraj Julha

Hi

Try this:

Dim sData As New StringBuilder()

sData.AppendFormat("This is line one {0}", ControlChars.CrLf)
sData.AppendFormat("This is line Two{0}", ControlChars.CrLf)
sData.AppendFormat("This is line Three{0}", ControlChars.CrLf)

Not sure about the CrLf on the ControlChars class, don't have my IDE loaded,
but if you type . you'll see a list of properties you can use.

txtOut.Text = sData.ToString()

You'll have to set the multiline property of the Textbox to true. Hope this
helps.

Cheers

Raj
 
N

Nurchi BECHED

Hello, Richard!
You wrote on Fri, 13 Aug 2004 13:55:01 -0700:

Thanks for suggestion. It still didn't work.
Something just came up to my mind:
somewhere in the program I converted received data into 'ASCII',
but even when I changed it back to 'Default,' it didn't help...

Thanks

R> this.TextBox1.AppendText("Hello"+Environment.NewLine+"World");

R> "Nurchi BECHED" wrote:

??>> Hello, All!
??>>
??>> I have created an application with a multiline textbox on the form.
??>> When I press a button, it has to show something and then break
??>> the line and show something else.
??>> I tried "\n", ((char)13) and ((char)10) - they don't work.
??>> When I enter a break by hand (just press enter) and run through
??>> each character in that string, the line break character has code
??>> 13 10 (yes, 2 numbers for some reason)
??>> The possible reason is that 'Enter' is a "control" key (a char with
??>> ASCII code less than 32) - they usually return 2 numbers...
??>>
??>> What should I do to break line in the textbox?
??>> Alternative way was to use TextWriter:
??>> string LineBreak=(new System.IO.StringWriter()).NewLine;
??>>
??>> but there must be another way of doing it...
??>> and then just use that string:
??>> this.TextBox1.AppendText("Hello"+LineBreak+"World");
??>>
??>> That is kind of stupid...
??>>
??>> With best regards, Nurchi BECHED.
??>>

With best regards, Nurchi BECHED.
 
J

Jon Skeet [C# MVP]

Nurchi BECHED said:
I have created an application with a multiline textbox on the form.
When I press a button, it has to show something and then break
the line and show something else.
I tried "\n", ((char)13) and ((char)10) - they don't work.
When I enter a break by hand (just press enter) and run through
each character in that string, the line break character has code
13 10 (yes, 2 numbers for some reason)
The possible reason is that 'Enter' is a "control" key (a char with
ASCII code less than 32) - they usually return 2 numbers...

What should I do to break line in the textbox?

Use "\r\n", otherwise known as carriage-return line-feed.

The correct line terminator for whatever platform you're on is given by
Environment.NewLine, but as TextBox is somewhat Windows-specific
anyway, you can just use "\r\n".
 
N

Nurchi BECHED

Hello, Jon!
You wrote on Sat, 14 Aug 2004 10:23:22 +0100:

??>> I have created an application with a multiline textbox on the form.
??>> When I press a button, it has to show something and then break
??>> the line and show something else.
??>> I tried "\n", ((char)13) and ((char)10) - they don't work.
??>> When I enter a break by hand (just press enter) and run through
??>> each character in that string, the line break character has code
??>> 13 10 (yes, 2 numbers for some reason)
??>> The possible reason is that 'Enter' is a "control" key (a char with
??>> ASCII code less than 32) - they usually return 2 numbers...
??>>
??>> What should I do to break line in the textbox?

JSC> Use "\r\n", otherwise known as carriage-return line-feed.

JSC> The correct line terminator for whatever platform you're on is given
JSC> by Environment.NewLine, but as TextBox is somewhat Windows-specific
JSC> anyway, you can just use "\r\n".

It doesn't work, and I have already said that Environment.NewLine didn't
help
either.
I guess if I give more background on the program, you might get some more
ideas...
The program I made consists of 2 applications: Client and Server.
It is some kind of messenger (rather something closer to NetMeeting)
2 programs just connect with each other on the network by IP address using
specified
port and then exchange text messages...

On the serverside I even tried something like this:
(tried this.tbMessageIn.AppendText() as well)

this.tbMessageIn.Text+=
this.server.Message+" |sdfsdfsdf| ";

The method that takes care of receiving messages is a tick of a timer object
with
interval 100ms, but I don't think this could be a problem...

'server' is an instance of 'TCP_Server' class which I wrote.
'Message' is a new property in the class which is defined as follows:
public string Message
{
get
{
return this.Received;
}
set
{
this.Sent=value;
}
}
//Received and Sent are private string variables for TCP_Server class

It only adds 'this.server.Message' but not the remaining ' |sdfsdfsdf| '
If I hit 'Enter' in the outgoing message before clicking 'Send', it goes
there as part of the message...
I mean
The
Text
Like
This

Would be transferred as 4 lines, so it doesn't remove '\n' characters
from the string, it for some reason just doesn't let me add anything
to a textbox locally...

With best regards, Nurchi BECHED.
 
N

Nurchi BECHED

Just wanted to point something out:
If I "add" end of line character to outgoing message, then it works just
fine,
but why it can't add the same thing locally after receiving the data

this.server.SendMessage(this.tbMessageOut.Text+"\r\n")

With best regards, Nurchi BECHED.
 

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