[英]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.