This is a migrated thread and some comments may be shown as answers.

Remove empty line

16 Answers 364 Views
TextBox
This is a migrated thread and some comments may be shown as answers.
Andreas Lohr
Top achievements
Rank 1
Andreas Lohr asked on 14 Mar 2011, 08:09 PM
hi,
I have a RadTextBox in MultiLine mode with a NullText. I add text with my code, but I have always a empty line at the end.
How can I remove that? Sounds as a pretty easy task, but for some reason it doesnot work for me, the empty line at the end is never removed.
Regards, Andreas

16 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 15 Mar 2011, 10:20 AM
Hello,

I'm not sure if I understand your issue. I've tried a multiline textbox and I can't seem to see a problem with it. If you could include any supporting screenshots or additional code to explain, I'll do my best to help
Thanks
Richard
0
Andreas Lohr
Top achievements
Rank 1
answered on 15 Mar 2011, 02:52 PM
hi Richard,

thank you for your help. I attached a screenshot. There I have a textbox (Proxies), where I write (with code) IP adresses. But unfortunaly there is an empty line at the end, so that my counter is also wrong. I thought in the TextChanged event I add some code to delete all empty lines. But everything I have tried has not worked so far.

Regards
Andreas
0
Richard Slade
Top achievements
Rank 2
answered on 15 Mar 2011, 03:03 PM
Hello,

I'm afraid I haven't been able to replicate your issue. I have added a RadTextBox to a form, made the RadTextBox multiline and added a button with a click event defined as follows:

Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
    Me.RadTextBox1.Text = "Line 1" & Environment.NewLine & "Line 2"
    For Each line As String In RadTextBox1.Lines
        MessageBox.Show(line.ToString())
    Next
End Sub

This shows me 2 lines. Please could you post a sample to show how you are adding the text to the RadTextBox as it sounds as though your text contains a new line character at the end.
Thanks
Richard
0
Andreas Lohr
Top achievements
Rank 1
answered on 15 Mar 2011, 03:33 PM
hi Richard,

my codes more like this:
private void radButton1_Click(object sender, EventArgs e)
{
  radTextBox1.Text = radTextBox1.Text + "Line 1\r\n";
  radTextBox1.Text = radTextBox1.Text + "Line 2\r\n";
  foreach(string line in radTextBox1.Lines)
  {
    MessageBox.Show(line.ToString());
  }
}
A new line is added in an event.
Is there any way to remove empty lines once I have them? There could also be an empty line added by mistake from the user, so it would be great, if there is a way to delete the empty line(s), if there are any.

regards, Andreas
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 15 Mar 2011, 04:48 PM
Hello,

If you want to remove the end line breaks after they are in the RadTextBox, something like this should work for you.

radTextBox1.Text = radTextBox1.Text + "Line 1\r\n";
radTextBox1.Text = radTextBox1.Text + "Line 2\r\n\r\n"; // will remove all end line breaks
ArrayList list = new ArrayList();
for (int i = 0; i <= this.radTextBox1.Lines.Count()-1; i++)
{
    if (!String.IsNullOrEmpty(radTextBox1.Lines[i]))
    {list.Add(radTextBox1.Lines[i]);}
}
this.radTextBox1.Lines = (string[])list.ToArray(typeof(string));
foreach (string line in this.radTextBox1.Lines)
{
    MessageBox.Show(line.ToString());
}

Hope that helps but let me know if you have any questions
Richard
0
Andreas Lohr
Top achievements
Rank 1
answered on 16 Mar 2011, 02:03 PM
hi Richard,

I am sorry, probably I have described not exact, what I want. Your code seems to remove all line breaks. But the line breaks are needed, I want just that the line breaks on empty text lines are removed.

Regards, Andreas
0
Richard Slade
Top achievements
Rank 2
answered on 16 Mar 2011, 03:47 PM
Hello,

the code should only remove line breaks where there is nothing else on the line. E.g

Line 1 /break
Line 2 /break
/break (removed)
/break (removed)

I based this on the code that you sent. I'll try it again and let you know the result.
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 16 Mar 2011, 03:54 PM
Hello,

I have just tried this again, and the messagebox displays a message correctly for each line. Please could you post some code that replicates your issue with this and I'll be happy to take a look
Thanks
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 17 Mar 2011, 10:59 AM
Hi Andreas,

Did this help, or are you still having issues? Let me know if I can help further.
All the best
Richard
0
Andreas Lohr
Top achievements
Rank 1
answered on 17 Mar 2011, 06:52 PM
hi Richard,

my code looks like this:
private void radButton1_Click(object sender, EventArgs e)
    {
      radTextBox1.Text = radTextBox1.Text + "Line 1\r\n";
      radTextBox1.Text = radTextBox1.Text + "Line 2\r\n\r\n"; // will remove all end line breaks
       
      foreach (string line in this.radTextBox1.Lines)
      {
        MessageBox.Show(line.ToString());
      }
    }
 
    private void radTextBox1_TextChanged(object sender, EventArgs e)
    {
      ArrayList list = new ArrayList();
      for (int i = 0; i <= this.radTextBox1.Lines.Count() - 1; i++)
      {
        if (!String.IsNullOrEmpty(radTextBox1.Lines[i]))
        { list.Add(radTextBox1.Lines[i]); }
      }
      this.radTextBox1.Lines = (string[])list.ToArray(typeof(string));
    }
But that doesnot work. If I add the code from theTextChanged event handler in the event handler that adds the text, it also doesnot work for me. I am really going mad on this.
Any ideas?

regards, Andreas
0
Richard Slade
Top achievements
Rank 2
answered on 17 Mar 2011, 08:45 PM
Hi Andreas,

No, you shouldn't use the TextChanged event. You will make this fire again. Why not just use the Leave event of the RadTextBox

private void Form1_Load(object sender, EventArgs e)
{
    radTextBox1.Text = radTextBox1.Text + "Line 1\r\n";
    radTextBox1.Text = radTextBox1.Text + "Line 2\r\n\r\n";
}
private void radTextBox1_Leave(object sender, EventArgs e)
{
    ArrayList list = new ArrayList(); 
    for (int i = 0; i <= this.radTextBox1.Lines.Count() - 1; i++)
    {
        if (!String.IsNullOrEmpty(radTextBox1.Lines[i]))
        { list.Add(radTextBox1.Lines[i]); }
    }
    this.radTextBox1.Lines = (string[])list.ToArray(typeof(string));
    foreach (string line in this.radTextBox1.Lines)
    {
        MessageBox.Show(line.ToString());
    }
}

Hope that helps, but let me know if you need further help
Richard
0
Andreas Lohr
Top achievements
Rank 1
answered on 17 Mar 2011, 09:44 PM
hi Richard,
the problem is that the leave event is usually never fired, because the text is usually not entered by manual input, its entered  by this code:
void proxyGetter_NewProxyFound(object sender, SEO.Common.Async.EventArgs<ProxyServer, string> e)
{
  txtProxies.Text = txtProxies.Text + String.Format("{0}:{1}\r\n", e.Value1.IpAddress, e.Value1.Port);
}
That means, the leave event is usually not fired, but it could be fired, because of course I can also add text by hand.
Do you see any other event that fits my needs?

regards, Andreas
0
Richard Slade
Top achievements
Rank 2
answered on 17 Mar 2011, 10:14 PM
Hello,

From your screenshot it looks as though the user must press a button (such as validate or harvest). Can you not perform it there?
Can I ask though, why do you not remove the line breaks before they are entered? - Or even use a RadListControl to list out the items that you need, perhaps with a simple one line RadTextBox for users to add more entries to the ListControl?

Regards,
Richard
0
Andreas Lohr
Top achievements
Rank 1
answered on 17 Mar 2011, 10:29 PM
hi Richard,

validate would only delete items, but the harvest button add items and it is a async method that is executed, so new proxies are added by an eventhandler.
It has a high usability, if you can add additional items with ctrl+C.
In fact I have not thought, that it creates these problems just to delete empty lines in all cases.
But I will think over the night how I can fix this problem.
Thanks a lot for your awesome support.

Best regards
Andreas
0
Richard Slade
Top achievements
Rank 2
answered on 17 Mar 2011, 10:31 PM
Hi

You're welcome. Let me know if you want me to prepare you something using a ListControl and a RadTextBox, or indeed if you need any further help
Thanks
Richard
0
Ivan Petrov
Telerik team
answered on 22 Mar 2011, 10:26 AM
Hi Andreas,

Thanks for writing.

I performed some tests and could not find a case where the TextChanged event would not be fired. I tried adding text from separate thread, on a button click, adding it by hand and a combination of these. You can use the solution provided by Richard in the TextChanged event provided that you unsubscribe from the event in the beginning and subscribe to the event in the end of it. In addition, when the Enter event is fired, you should unsubscribe and in the Leave event you subscribe again and validate the text. Another thing you can do is add the line breaks before the IP text.

I attached a small project which I believe covers your case.

I hope it will help. If you have further questions, I would be glad to answer them.

Kind regards,
Ivan Petrov
the Telerik team
Tags
TextBox
Asked by
Andreas Lohr
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Andreas Lohr
Top achievements
Rank 1
Ivan Petrov
Telerik team
Share this question
or