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

StackOverflow https://stackoverflow.com/questions/23293294

  •  09-07-2023
  •  | 
  •  

Question

I have created a task-pane in PowerPoint VSTO add-in which I have developed on .Net 4.0. 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. If a line exceeds 8 characters, it should be truncated to 8 characters.

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

Was it helpful?

Solution

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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top