Search code examples
c#stringwinformstextbox

Carriage Return & Line Feed acts as Char values


I've got a HEX to RGB converter program that accepts string inputs from two fields: A Textbox for RGB and a Textbox for HEX. Each box searches for a specific string length from specific values.

For example, if the user enters a string of 7 characters "#0000FF", only the 6 values representing RGB are processed. I.E. the '#' is Replaced with "".

Recently I've been finding that inputting only 4 valid characters, such as "000F" will result in crash. I've put a break point at the if-statement related to the .Length of the string, and it seems that 2 characters are added to the value.

So after typing "0", the length is now 3 and the input is equivalent to "0\r\n". After Typing "00", the length is then 4 and the input equivalent is "00\r\n" - and so forth.

Is there a simple way to remove the "\r" and "\n" values of the string? Why is it that Carriage Return and Line Feed appear in the textbox inputs at all?


Solution

  • The easiest way would be using String.Replace to remove the \r and \n chars from the String:

    String test = textbox1.Replace("\r","").Replace("\n","");
    

    This removes all CR and LF characters from the TextBoxes string.