Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code to reverse message entered from textBox and then show the result of reverse in another textBox ,how can using file the first file have the message and other file print in it the result of reverse

C#
for (int i = textBox1.Text.Length - 1; i >= 0; i--)
           {
               richTextBox2.Text += '\n';
               richTextBox1.Text += textBox1.Text[i].ToString();
           }

           for (int i = 0; i <= textBox1.Text.Length - 1; i++)
           {

               richTextBox2.Text += textBox1.Text[i].ToString();
           }
Posted
Comments
ZurdoDev 8-Jan-15 15:43pm    
What?
[no name] 8-Jan-15 15:57pm    
Your question is not clear. Especially because you write about files and your code does not use filesystem.
I assume you used something like Google translate...one tip: translate the english back to your language and cross check again.
Bruno
[no name] 8-Jan-15 16:34pm    
Bruno Sprecher,I write this code and use the textbox to insert the data and show it ,Idon't know how can I use file in c#,and why u assume that I used something like Google translate ):
CHill60 8-Jan-15 17:18pm    
Bruno won't see your response unless you use the Reply link to his post. However his assumption probably came from the fact we couldn't really follow what you meant ... a "feature" that is common with google translate :-)
BillWoodruff 8-Jan-15 17:54pm    
Are you wanting to read a file to get data for one of the TextBoxes ?

Yes, or no, please.

1 solution

No need to use loop. Try this
C#
char[] arr = textBox1.Text.Trim().ToCharArray();
Array.Reverse(arr);
richTextBox2.Text = new string(arr);


Hope, it helps :)

Update:

Using file read/write:
C#
string text = System.IO.File.ReadAllText(@"D:\YourFolder\file1.txt");
char[] arr = text.Trim().ToCharArray();
Array.Reverse(arr);
System.IO.File.WriteAllLines(@"D:\YourFolder\file2.txt",new string(arr));
 
Share this answer
 
v3
Comments
[no name] 8-Jan-15 16:49pm    
where is the file??! (:
Suvendu Shekhar Giri 8-Jan-15 17:05pm    
Sorry dear :( missed that thing.
Check the updated answer. Is this what you were looking for?
[no name] 9-Jan-15 8:59am    
@Suvendu Shekhar,please I have error in this line System.IO.File.WriteAllLines(@"D:\YourFolder\file2.txt",new string(arr));
has some invalid arguments
[no name] 9-Jan-15 9:36am    
I use the file in this way but I have problem is for ex: if I enter "send"in new1.txt the result in new2.txt reverse this okay but each char print in new line string path1 = "E:/new1.txt";
string path2 = "E:/new2.txt";

// Open the file to read from.
string[] readText = File.ReadAllLines(path1);
foreach (string s in readText)
{

char[] arr = s.Trim().ToCharArray();
Array.Reverse(arr);
foreach (char s2 in arr)
{
textBox1.Text += s2.ToString();


string appendText = s2 + Environment.NewLine;

File.AppendAllText(path2, appendText);


}
}
BillWoodruff 9-Jan-15 15:08pm    
You should revise your original question to include this information.

Of course each character has a newline added: you are adding it in your loop.

You are doing this rather waste-of-time exercise about as inefficiently as possible.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900