Solved

C# newline

Posted on 2001-08-20
19
1,176 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
Comment Utility
isn't it "\n" or "\r\n"

0
 

Author Comment

by:bruwmac
Comment Utility

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
Comment Utility
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
 
LVL 1

Expert Comment

by:The_Brain
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The_Brain

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

not my attitude but what is considered good practice on EE
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Expert Comment

by:ShaunWilde
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
> 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
Comment Utility
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
Comment Utility

 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
Comment Utility
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
Comment Utility
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
Comment Utility
OOPS! Thats should have been "in C#" not C.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now