如何在多行文本框中的一行中设置最大字符数. [英] How to set the maximum number of characters in a line of a multiline textbox.

查看:224
本文介绍了如何在多行文本框中的一行中设置最大字符数.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多行文本框,我想将每行的最大字符数设置为27,该怎么做?
如果我根据字符数设置一个固定的宽度,那么大写字母和小写字母的字符数会更少.
有没有解决此问题的方法,而无需设置文本框的宽度.我正在使用WPF和MVVM模型.

I have a multiline textbox.I want to set maximum numbers of characters in each line to 27.How do I do that?
If I set a fixed width according to the number of characters then it takes less number of characters for Capical letters and less for small letters.
Is there any solution to this without setting the width of the textbox.I am using WPF and MVVM model.

推荐答案

您好,RicaAgarwal,

标准的WPF Textbox控件没有这种属性.在这里,我找到了解决它的方法.
< TextBox Name =" textBox1"高度="200". TextWrapping ="Wrap" VerticalScrollBarVisibility ="Visible" KeyDown ="textBox1_KeyDown"</TextBox>
私有无效textBox1_KeyDown(对象发送者,KeyEventArgs e)
        {
           int textLength = textBox1.Text.Length;
                                                      如果(textLength%27 == 0)
{
               textBox1.Text + ="\ n";
            textBox1.SelectionStart = textBox1.Text.Length-1;
            }
       }
在此示例中,我处理了文本框的KeyDown事件,以检查当前行是否已达到27个字符.当它达到27个字符时,我添加了一个\ n到文本末尾以强制其开始新行.希望能对您有所帮助.

真诚的
钱凯拉
Hi RichaAgarwal,

The standard WPF Textbox control doesn’t have that kind of property. Here I found a way to work around it.
<TextBox Name="textBox1" Height="200" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" KeyDown="textBox1_KeyDown"></TextBox>
private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            int textLength = textBox1.Text.Length;
            if (textLength % 27 == 0)
            {
                textBox1.Text += "\n";
                textBox1.SelectionStart = textBox1.Text.Length - 1;
            }
        }
In this example, I handed the KeyDown event of textbox to check whether current line has reached 27 characters. When it reaches 27 characters, I add a “\n” to the end of text to force it start a new line. Hope it helps.

Sincerely,
Kira Qian


这篇关于如何在多行文本框中的一行中设置最大字符数.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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