Solved

C# newline

Posted on 2001-08-20
19
1,179 Views
Last Modified: 2011-09-20


  I made a windows application (VS.NET/C#) that reads text box input into an array, iterates through the array and sorts the input, displaying the output in a label. Works great, except the output writes over itself in the label. I know there has to be a way to stick a newline character in there somewhere, but can't figure out how.
Any thoughts?

bruwmac
0
Comment
Question by:bruwmac
  • 8
  • 8
  • 3
19 Comments
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6409450
isn't it "\n" or "\r\n"

0
 

Author Comment

by:bruwmac
ID: 6409586

Exactly.
The problem I am having is that the newline character appears not to be doing what I want it to. Here is the method:
          public void SortAndShow()
          {    
               
               string[] Str = new string[3];
               string txt1 = txtString1.Text;
               string txt2 = txtString2.Text;
               string txt3 = txtString3.Text;
             

               Str[0] = txt1;
               Str[1] = txt2;
               Str[2] = txt3;

               Array.Sort(Str);
               
               for(int s = 0; s < Str.Length; s++)
               {
                    lblOutput.Text = ((Str[s])  + "\r\n");// newline has no visible effect here.                    
                    Console.Write((Str[s])  + "\r\n"); // for debug
                    MessageBox.Show(Str[s]);// for debug
               }

The method works fine. Displays fine in debug output screen, and the newline works there. In fact, it is not needed there. Message box works fine, too. Displays each element of the array per messagebox as program loops through the array. I just want the label to display each element of the array on a newline, not write over itself in the same spot.              
0
 
LVL 1

Expert Comment

by:The_Brain
ID: 6409675
public void SortAndShow()
         {    
             
              string[] Str = new string[3];
              string txt1 = txtString1.Text;
              string txt2 = txtString2.Text;
              string txt3 = txtString3.Text;
             

              Str[0] = txt1;
              Str[1] = txt2;
              Str[2] = txt3;

              Array.Sort(Str);
             
              for(int s = 0; s < Str.Length; s++)
              {
                   lblOutput.Text = ((Str[s])  + "\r\n");// newline has no visible effect here.    
               
                   Console.WriteLine((Str[s])); // for debug
                   MessageBox.Show(Str[s]);// for debug
              }

This better work! :(
else I'm calling MS

~Ixeus
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:The_Brain
ID: 6409693
P.S, if I made a mistake you should use "\r\n\r\n";  for the .text (instead of \r\n *shrug*)

~Ixeus
0
 

Author Comment

by:bruwmac
ID: 6409727
I am afraid "\r\n\r\n" has no more effect than "\r\n".
The only other editing to my method that I picked up on was to Console.WriteLine. Have I missed something?
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6409730
on the following site

http://windows.oreilly.com/news/csharp_0101.html

you see this

Turning '\' Followed by 'n' Into a Real Newline
string t10 = @"\ntest\n";
string r10 = Regex.Replace(t10, @"\\n", "\n");

The_Brain - please refrain from posting an answer especially when other people have comented on the question

0
 
LVL 1

Expert Comment

by:The_Brain
ID: 6409796
Then maybe that need to remove the answer feature, I was pretty sure that would have solved his problem, thank you for sharing your attitude with us. :)

~Ixeus
0
 

Author Comment

by:bruwmac
ID: 6410026
The_Brain
Thanks for the input. I guess I don't understand the Answer vs. Comment protocol. I appreciate your effort.

ShaunWilde
Thanks for the link. Looks like a whole world of useful stuff I hope I never need to learn, though I am sure I will.
The example you cited demonstrates the Replace method and illustrates the use of @, which, when applied, makes it unnecessary to use a double \\, when, for example, hard coding in a path name.i.e. string FileName = "C:\\MyFolder\\MyFile.txt" can also be written FileName = @"C:\MyFolder\MyFile.txt".
However, none of this solves my problem.
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6410498
The_Brain

> thank you for sharing your attitude with us. :)

not my attitude but what is considered good practice on EE
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6410510
bruwmac

is the label big enough to show your text - eg is it big enough to show 2 or more lines? may be it is only showing the last line
0
 

Author Comment

by:bruwmac
ID: 6410799
The label is plenty big. If you were to step through the program, you would see the first input string get written to the label, then the second would overwrite it in the same spot, then the final string would overwrite that.

I have "solved" the problem by using three labels and an if statement. if the string is in the first cell of the array, assign it to Label1.Text, etc. However, I am not happy with my solution because I know it cannot be good programming, which is the whole point of making up these exercises for myself.
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6411306
just got home to ruun up my C# environment and I put a label on the form and

label1.Text = "this is line 1\nline 2";

had 2 rows of text on the label - not sure what to say - what does your C# code look like?
0
 

Author Comment

by:bruwmac
ID: 6411419

I don't doubt that a newline read into a label will create two lines of text. The problem comes during the iteration through the array in the for loop. I think maybe I need a second for loop to handle passing the string to the label.
Haven't quite got a grip on the concept. Not home right now to test my theory.
The code for the method is posted immediately after your first comment and is repeated in The_Brains "answer" with a minor modification where none was needed, i.e., my Console debug statement.
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6412254
> lblOutput.Text = ((Str[s])  + "\r\n");// newline has no visible effect here.    

you will have to concatenate the strings each time you set the text on the lable to a new string

the label is not an output field just a window that displays text and each iteration you are telling it to display different text

eg

lblOutput.Text += ((Str[s])  + "\r\n");

should do the trick

0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 100 total points
ID: 6412258
ie you should use the += operator to concatenate the strings - or better still concatenate all the strings into one string and then set the edit field.

Do you have previous programming experience on windows? eg VB,C
0
 

Author Comment

by:bruwmac
ID: 6414220

 Didn't a NASA rocket crash on liftoff because a C programmer confused "=" and "=="?
Using "+=" didn't occur to me.
Thanks. Now I can devise a new means to torment myself.

>> Do you have previous programming experience on windows? eg VB,C

Only in classrooms. I have taken Intro level courses in C++, Java and just completed one in VB6. I am an adult (old man) student and only turned on a computer for the first time a couple of years ago. (An outdated computer, at that. Windows 3.x) Belive it or not, I have just been invited to join Phi Theta Kappa.
I am currently teaching myself C# and whatever else I can pick up as regards .NET
Thanks for the help. It is truly appreciated.

bruwmac
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6414245
glad to help - should have spotted it earlier from your sample code - I haven't done much C# myself - I have the old .NET beta and it ran like a snail - should try the latest one :)
0
 

Author Comment

by:bruwmac
ID: 6414272
I recently posted a question in the general programming section of EE that has not received a lot of response. It is titled "Combining projects in C" if you want to take a stab at it.
0
 

Author Comment

by:bruwmac
ID: 6414276
OOPS! Thats should have been "in C#" not C.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question