Limit the max number of chars per line in a textbox

前端 未结 5 1930
慢半拍i
慢半拍i 2021-01-25 23:24

Say I have the following:



        
5条回答
  •  半阙折子戏
    2021-01-25 23:43

    I know this is an extremely late answer, but so that people who find this can get a good answer, with protection against for instance Ctrl+C and stuff:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        foreach (string line in textBox1.Lines)
        {
            if (line.Length > 60)
            {
                textBox1.Undo();
            }
        }
    
        textBox1.ClearUndo();
    }
    

    Note that this does mean you cannot use Ctrl+Z in the textbox anymore but if that doesn't bother you this is a good option, because it works with any font.

    EDIT This doesn't work with wpf textboxes, only windows forms textboxes

提交回复
热议问题