简体   繁体   English

如何使用C#在PowerPoint TaskPane上限制多行文本框中每行的长度?

[英]How to limit the length of each line in a multiline textbox on PowerPoint TaskPane using C#?

I have created a task-pane in PowerPoint VSTO add-in which I have developed on .Net 4.0. 我已经在.Net 4.0上开发的PowerPoint VSTO加载项中创建了一个任务窗格。 On the task-pane, I have a text box where the user has to enter only numeric data. 在任务窗格上,我有一个文本框,用户只需要输入数字数据即可。 The requirement is as below: 要求如下:

The user can enter more than one numeric data by typing one data on each line. 通过在每一行上键入一个数据,用户可以输入多个数字数据。 Each data can contain up to 8 characters, including: numbers, decimals and commas. 每个数据最多可以包含8个字符,包括:数字,小数和逗号。 If a line exceeds 8 characters, it should be truncated to 8 characters. 如果一行超过8个字符,则应将其截断为8个字符。

Below is the code that I am using: 下面是我正在使用的代码:

public void splitString(string[] strText)
    {            
        string[] arr = txtEntryField.Lines;
            for (int n = 0; n < arr.Length; n++)
            {
                if (arr[n].Length > 8)
                {
                    arr[n] = arr[n].Substring(0, 8);
                }                                                                     
            }
            txtEntryField.Lines = arr;
            if (txtEntryField.Lines.Length > 0)
            {
                txtEntryField.SelectionStart = txtEntryField.Text.Length;
            }
    }

I am calling this method on txtEntryField_TextChanged event. 我在txtEntryField_TextChanged事件上调用此方法。 While I am almost there, I think the operation and user experience is not so smooth. 当我快要到那儿时,我认为操作和用户体验都不太流畅。

Updated the code so that user is not able to enter characters in the textbox. 更新了代码,以使用户无法在文本框中输入字符。 This is done by the following code: 这是通过以下代码完成的:

void txtEntryField1_KeyPress(object sender, KeyPressEventArgs e)
    {                  
        const char Delete = (char)8;
        var regex = new Regex(@"[^.,0-9\s]");
        if (regex.IsMatch(e.KeyChar.ToString()) && e.KeyChar != Delete && e.KeyChar != (char)Keys.Enter && e.KeyChar != (char)Keys.Back)
        {
            e.Handled = true;
        }
    }

Can any one help me with a better solution? 谁能帮助我提供更好的解决方案? Any help is most welcome. 任何帮助都是最欢迎的。 Thanks. 谢谢。

This worked for me: 这为我工作:

public void splitString(string[] strText)
{            
    string[] arr = txtEntryField.Lines;
        for (int n = 0; n < arr.Length; n++)
        {
            if (arr[n].Length > 8)
            {
                arr[n] = arr[n].Substring(0, 8);
            }                                                                     
        }
        txtEntryField.Lines = arr;
        if (txtEntryField.Lines.Length > 0)
        {
            txtEntryField.SelectionStart = txtEntryField.Text.Length;
        }
}

Also the below code allows the user only to enter the desired characters: 同样,以下代码仅允许用户输入所需的字符:

void txtEntryField1_KeyPress(object sender, KeyPressEventArgs e)
{                  
    const char Delete = (char)8;
    var regex = new Regex(@"[^.,0-9\s]");
    if (regex.IsMatch(e.KeyChar.ToString()) && e.KeyChar != Delete && e.KeyChar != (char)Keys.Enter && e.KeyChar != (char)Keys.Back)
    {
        e.Handled = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 限制多行文本框中的行长 - Limit line length in multiline textbox 如何在多行文本框中的每个新行中添加数字C#/ ASP.net - How to add number in each new line in a multiline textbox C# / ASP.net c#如何逐行读取和写入多行文本框? - c# How to read and write from multiline textBox line by line? 如何删除一行多行文本框中的字符串 c# - how to remove a string in a line of Multiline textbox c# 在c#中设置多行TextBox的特定行 - Set specific line of multiline TextBox in c# 在C#中更改多行文本框的特定行 - Change Particular line of Multiline textbox in C# 如何使用C#在多行文本框中最后一行之后的新行上设置光标位置? - How can I set the cursor position at the new line which is after the last line in a multiline textbox using C#? 如何删除 c# 中长度大于 25 个字符且不包含空格的多行文本框行? - How to remove lines of multiline textbox that are greater than 25 characters in length and contain no spaces, in c#? 如何从多行文本框中为 C# 中的每一行创建 2 个 arrays? 然后我需要在图中 plot 那些 arrays - How create 2 arrays from a multiline textbox for each row in C#? Then I need to plot in a graph those arrays C#多行文本框:如果包含X,则在行后添加字符串 - C# Multiline Textbox: Adding a string after a line if it contains X
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM