Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i need to see the input in a textbox which shows the input with newline charcters but then a button which clicks and shows the data in another textbox in a single line ignoring /n

What I have tried:

I tried to trim but its no use please help me there anybody.
Posted
Updated 19-Jul-19 0:49am

Instead of Trim, try Replace:
C#
newTextBox.Text = oldTextBox.Text.Replace("\n", "");
 
Share this answer
 
Comments
Member 14519564 19-Jul-19 6:28am    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp59
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void TextBox1_TextChanged(object sender, EventArgs e)
{
}

private void Button1_Click(object sender, EventArgs e)
{
newTextBox.Text = oldTextBox.Text.Replace("\n", "");
}
}
}
OriginalGriff 19-Jul-19 6:43am    
:sigh:

Now, why do you assume that I know in advance exactly what your controls are called, and that you don't have to rename the place holders I used appropriately?
Member 14519564 19-Jul-19 6:29am    
private void NewTextBox_TextChanged(object sender, EventArgs e)
{
throw new NotImplementedException();
}

#endregion

private System.Windows.Forms.TextBox oldTextBox;
private System.Windows.Forms.TextBox newTextBox;
private System.Windows.Forms.Button button1;
}
}
Member 14519564 19-Jul-19 6:29am    
showing error here in design part
please tell me how to solve
Member 14519564 19-Jul-19 6:30am    
showing Exception unhandled
You can do this with a single line of code:

1 set the Multiline, AcceptsTab, and AcceptsReturn properties of textBox1 to 'true.

2 set the Multiline, AcceptsTab, and AcceptsReturn properties of textBox2 to 'false.
note: these are the default values.

3 in a Button 'Click EventHandler:
textBox2.Text = textBox1.Text;
Now, do not be surprised if, when there is no white space at the end of each line in textBox1, what you get in textBox2 is lines-run-together. I'd guess you don't really want to lose all indication of where lines ended ... so:
C#
private Char myLineDelimiter = '|';
private bool doTrimLines = true;

private void MoveTextButton_Click(object sender, EventArgs e)
{
    StringBuilder sb = new StringBuilder();

    foreach (string line in textBox1.Lines)
    {
        sb.Append(doTrimLines ? line.Trim() : line); // see note
        sb.Append(myLineDelimiter);
    }

    textBox2.Text = sb.ToString();
}
Using a 'StringBuilder avoids excess memory consumption that may come into play with manipulating large collections of strings: it's used here out of habit, more than necessity.

Note: do you really want to trim the whitespace in each line ? If you do not trim the lines, and the lines have white-space made of Tabs, those Tabs will appear in textBox2 even though you set its AcceptsTab property to 'false: that's a bug in the TextBox.
 
Share this answer
 
 
Share this answer
 

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