Home > Blockchain >  How to keep the carriage return by Ignoring the carriage return C#
How to keep the carriage return by Ignoring the carriage return C#

Time:04-05

I thank you in advance.

I have a function that removes any number of characters past the maximum length. However, it also removes carriage returns. So, if I write a paragraph and use carriage returns for the new paragraph the carriage returns would also be counted and removed.

Does anybody have a good idea how I can achieve removing anything over the maximum length of characters without removing or counting the carriage returns?

    public static string RemoveExcessCharacters(string value, int maxLen)
    {
        return (value.Length > maxLen) ? value.Substring(0, maxLen) : value;
    }

The input code. Some of the code I was just testing like. newString in the code.

        private void txtWrite_TextChanged(object sender, EventArgs e)
    {
        try
        {
            int g = Convert.ToInt32(txtNumbersOnly.Text);
            if (string.IsNullOrEmpty(txtNumbersOnly.Text) || g == 0)
            {
                txtWrite.Text = "";
                MessageBox.Show("Please provide character Count. Character Count cannot be zero or Null.", "Maximum Characters", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtNumbersOnly.Text = "777";
                txtNumbersOnly.Focus();
                return;
            }
            else
            {
                cCount = Convert.ToInt32(txtNumbersOnly.Text);
                if (cCount == 0)
                {
                    txtWrite.Text = "";
                    MessageBox.Show("Please provide character Count. Character Count cannot be zero.", "Maximum Characters", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtNumbersOnly.Focus();
                    return;
                }
                else
                {
                    // Testing again
                    //x = txtWrite.Text.TrimEnd();
                    x = txtWrite.Text;
                    //var newString = string.Join(" ", Regex.Split(x, @"(?:\r\n|\n|\r)")).Length;
                    //var newString = string.Join(" ", Regex.Split(x, @"(?:\r\n|\n|\r)")).Length;
                    //string v = x.Replace("\n", "").Replace("\r", "");
                    string cleaned = x;
                    string cleaned2 = cleaned.Length.ToString();
                    int cleaned3 = Convert.ToInt32(cleaned2);
                    lblChar.Text = "Character Count: "   cleaned2;
                    //
                    if (cleaned3 > cCount)
                    {
                        string strTest = x.ToString();
                        MessageBox.Show("You are over the maximum character length.\nI will trim this down for you now.", "Maximum Character Length", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        int intTrans = Convert.ToInt32(x.Length);
                        if (intTrans > cCount)
                        {
                            // Remove ending Characters over the max amount.
                            string value = RemoveExcessCharacters(txtWrite.Text, cCount);
                            // Re-add corrected text to the textbox.
                            txtWrite.Text = value;
                            // Move cursor to end
                            txtWrite.SelectionStart = txtWrite.Text.Length;
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }
            //
            return;
            //
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }
    }

CodePudding user response:

Don't cut the end off the user's words; stackoverflow doesn't when you write a comment - it just counts down the chars you have left and let's you exceed but doesn't submit. This is nicer because it's not arbitrarily deciding to delete what the user typed

As such, a simple button click submit validation of:

public void SomeButton_Click(object sender, EventArgs e){

  if(someTextBox.Text.Count(c => !"\r\n".Contains(c)) > 500)
    MessageBox.Show("You have exceeded 500 chars, not including new lines. Shorten your message before you submit");
  else
    SubmitMessage(someTextBox.Text);
}

You can apply the same technique to the TextChanged event if you want to eg provide a label that constantly counts down as the user types

CodePudding user response:

in a for loop you can check every character and count the length without counting end lines. it would help you to count correctly.

  •  Tags:  
  • c#
  • Related