如何在超过字符限制60后更改richtextbox中的选择颜色 [英] How to change the selection color in richtextbox after crossing the character limit 60

查看:62
本文介绍了如何在超过字符限制60后更改richtextbox中的选择颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Windows应用程序项目,我需要设计一个带有字符数标签的RichTextbox。



我的要求是,用户最多可以输入80个字符RichTextbox但只允许前60个字符保存在数据库中。我想显示键入的字符>红色60。



使用下面的代码,它显示超出第62个字符的红色字符,它应显示在第61个字符。



在某些测试用例中,组件无法正常工作。



Ex:

键入65个字符后,最后5个字符应显示为红色,删除前两个字符后,计数器值得到减少但最后5个字符的颜色仍显示为红色,理想情况下,它应显示最后3个字符,红色超过限制60.



我是什么尝试过:



请检查下面的代码并帮我解决这个问题。

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 Ex:
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.

What I have tried:

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="";





我在RichTextbox中使用了如下逻辑Textchanged事件:



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;
            }

推荐答案

当字符数超出限制时,为什么不根据选择更改颜色。类似

Why not change the color based on selection when the amount of characters exceeds the limit. Something like
...
         } else if (Typed_Characters_Count > 60) {
            int sel_start = txt_wordbox.SelectionStart;
            int sel_length = txt_wordbox.SelectionLength;

            txt_wordbox.Select(60, 99999);

            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;

            // Restore the selection's start and length.
            txt_wordbox.Select(sel_start, sel_length);
         }
....


我认为你的设计理念存在缺陷:



1.您可以设置RichTextBox或TextBox的'MaxLength属性来限制字符数。你为什么要让用户输入的字符多于你使用的字符数?



2.在WinForms中,我将它实现为带有标签的UserControl和TextBox:
I think your design idea here is flawed:

1. you can set the 'MaxLength Property of either a RichTextBox or TextBox to limit the number of characters. Why would you want to let the user enter more characters than you will use ?

2. In WinForms, I'd implement this as a UserControl with a Label and a TextBox:
using System;
using System.Windows.Forms;

namespace Test
{
    public partial class TbxLimited : UserControl
    {
        public TbxLimited()
        {
            InitializeComponent();
        }

        public int TbxLimited_MaxLength
        {
            set { textBox1.MaxLength = value; }
            get { return textBox1.MaxLength; }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            label1.Text = textBox1.Text.Length.ToString();
        }
    }
}

通过公开公共'TbxLimited_MaxLength属性,您可以对字符数限制启用设计时控制。

By exposing a public 'TbxLimited_MaxLength Property, you enable design-time control over the limit of characters.


我可以推荐这个非常好的选择:用于语法突出显示的快速彩色文本框 [ ^ ]

我甚至将它用于XML文件比较实用程序!
I can recommend this really good alternative: Fast Colored TextBox for Syntax Highlighting[^]
I even used it for an XML file comparer utility !


这篇关于如何在超过字符限制60后更改richtextbox中的选择颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆