Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1186
  • Last Modified:

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
0
bruwmac
Asked:
bruwmac
  • 8
  • 8
  • 3
1 Solution
 
ShaunWildeCommented:
isn't it "\n" or "\r\n"

0
 
bruwmacAuthor Commented:

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
 
The_BrainCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

~Ixeus
0
 
bruwmacAuthor Commented:
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
 
ShaunWildeCommented:
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
 
The_BrainCommented:
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
 
bruwmacAuthor Commented:
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
 
ShaunWildeCommented:
The_Brain

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

not my attitude but what is considered good practice on EE
0
 
ShaunWildeCommented:
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
 
bruwmacAuthor Commented:
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
 
ShaunWildeCommented:
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
 
bruwmacAuthor Commented:

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
 
ShaunWildeCommented:
> 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
 
ShaunWildeCommented:
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
 
bruwmacAuthor Commented:

 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
 
ShaunWildeCommented:
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
 
bruwmacAuthor Commented:
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
 
bruwmacAuthor Commented:
OOPS! Thats should have been "in C#" not C.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 8
  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now