Bhanu Jujjavarapu

Bhanu Jujjavarapu

  • NA
  • 10
  • 8.1k

How do I change the selection color in richtextbox after cro

Apr 21 2018 12:36 AM

Hi Sir/Madam,

I am making a windows application project where I need to design a RichTextbox with character count label.

My requirement is, User can type up to 80 characters in RichTextbox but only First 60 characters are allowed to save in database. I want to display the typed characters > 60 in Red.

Using the below code, it’s displaying exceeding characters in red from the 62nd character, it should display from the 61st character.

In some test cases, the component is not working properly.

For Example:

After typing 65 characters, Last 5 characters should display in red, after removing first two characters, the counter value gets reduced but the colour of last 5 characters still displaying in red, ideally, it should display last 3 characters in red which exceeded the limit 60.

Please check the code below and help me to solve this issue.

// Variables

int Typed_Characters_Count=0;

int Remain_Characters_Count=0;

int maxAllowedCharacters = 60;

string charactersToSave="";

I have used logic as below in RichTextbox Textchanged Event:

private void txt_wordbox_TextChanged(object sender, EventArgs e)

{

Typed_Characters_Count = txt_wordbox.TextLength;

if (Typed_Characters_Count == 0)

{

// Default

lbl_Header.Text = "Add Work Item";

lbl_Counter.Text = maxAllowedCharacters + "/" + maxAllowedCharacters;

lbl_Counter.ForeColor = Color.Black;

Btn_Save.Enabled = false;

}

else if (Typed_Characters_Count >= 1 && Typed_Characters_Count <= 60)

{

if (string.IsNullOrWhiteSpace(txt_wordbox.Text) && txt_wordbox.Text.Length > 0)

{

Btn_Save.Enabled = false;

lbl_Header.Text = "Add Work Item";

Remain_Characters_Count = maxAllowedCharacters - Typed_Characters_Count;

lbl_Counter.Text = Remain_Characters_Count.ToString() + "/" + maxAllowedCharacters;

lbl_Counter.ForeColor = Color.Black;

}

else

{

Btn_Save.Enabled = true;

lbl_Header.Text = "To save, click save icon";

// Save the selection's start and length.

int sel_start = txt_wordbox.SelectionStart;

int sel_length = txt_wordbox.SelectionLength;

CultureInfo culture_info = Thread.CurrentThread.CurrentCulture;

TextInfo text_info = culture_info.TextInfo;

txt_wordbox.Text = text_info.ToTitleCase(txt_wordbox.Text);

// Restore the selection's start and length.

txt_wordbox.Select(sel_start, sel_length);

Remain_Characters_Count = maxAllowedCharacters - Typed_Characters_Count;

lbl_Counter.Text = Remain_Characters_Count.ToString() + "/" + maxAllowedCharacters;

lbl_Counter.ForeColor = Color.Black;

}

}

else if (Typed_Characters_Count > 60)

{

lbl_Header.Text = "Reduce Characters to 60";

Remain_Characters_Count = maxAllowedCharacters - Typed_Characters_Count;

lbl_Counter.Text = Remain_Characters_Count.ToString() + "/" + maxAllowedCharacters;

lbl_Counter.ForeColor = Color.Red;

lbl_Header.ForeColor = Color.Red;

txt_wordbox.SelectionColor = Color.Red;

}


Answers (1)