Link to home
Start Free TrialLog in
Avatar of bruwmac
bruwmacFlag for United States of America

asked on

C# newline



  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
Avatar of ShaunWilde
ShaunWilde

isn't it "\n" or "\r\n"

Avatar of bruwmac

ASKER


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.              
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
P.S, if I made a mistake you should use "\r\n\r\n";  for the .text (instead of \r\n *shrug*)

~Ixeus
Avatar of bruwmac

ASKER

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?
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

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
Avatar of bruwmac

ASKER

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.
The_Brain

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

not my attitude but what is considered good practice on EE
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
Avatar of bruwmac

ASKER

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.
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?
Avatar of bruwmac

ASKER


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.
> 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

ASKER CERTIFIED SOLUTION
Avatar of ShaunWilde
ShaunWilde

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bruwmac

ASKER


 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
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 :)
Avatar of bruwmac

ASKER

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.
Avatar of bruwmac

ASKER

OOPS! Thats should have been "in C#" not C.