Avik
Avik

Reputation: 2137

Listbox data

Now in my program in c# that simulates the working of a lan-messenger I need to show the people who are currently online with one remote host-name on each line. However the problem here is that in order to do that I am using the function

//edited this line out, can't find a .Append for listBox or .AppendLine
//listBox.Append(String );

listBox.Items.Add(new ListItem(String));

On doing so, the carriage return and new line character(ie.. \r and \n) are being displayed as tiny little rectangular boxes at the end of the hostname. How do I get rid of that?

Upvotes: 0

Views: 964

Answers (4)

RSolberg
RSolberg

Reputation: 26972

Another option would be to consider creating an extension method for the custom trim that you are trying to achieve.

/// <summary>
/// Summary description for Helper
/// </summary>
public static class Helper
{
    /// <summary>
    /// Extension Method For Returning Custom Trim
    /// No Carriage Return or New Lines Allowed
    /// </summary>
    /// <param name="str">Current String Object</param>
    /// <returns></returns>
    public static string CustomTrim(this String str)
    {
        return str.Replace("\r\n", "");
    }
}

Your line of code then looks like this:

listBox.Items.Add(new ListItem(myString.CustomTrim());

Upvotes: 1

VVS
VVS

Reputation: 19612

Sounds like you're using some sort of TextBox in single line mode. If so, activate TextBox.Multiline.

Upvotes: 0

Gavin H
Gavin H

Reputation: 10502

You could use:

listBox.Append(String.Replace("\r\n", ""));

to get rid of those characters

Upvotes: 2

Fredrik M&#246;rk
Fredrik M&#246;rk

Reputation: 158379

You could use the Trim method of the string to remove unwanted characters from the start and end of the string. Either call it without parameters to remove all white-space (including like breaks), or pass a character array with the characters that you want removed.

Upvotes: 0

Related Questions