Is there a way to catch maximum length PER LINE and not allow user to input more characters if max length PER LINE has been reached?

后端 未结 4 1972
攒了一身酷
攒了一身酷 2020-12-22 04:06

Good day!

Sorry if I cant make it into code but here\'s my problem:

Im coding C#, using TextBox Control, Multiline = true.

Now, my problem is i need

相关标签:
4条回答
  • 2020-12-22 04:23
    textBox.OnTextChanged += (EventArgs e) => // invoked on every text change
    {
        var lines = myTextBox.Where(x.Length > 5); // Get the long lines
        for (var i; i < lines.Count(); i++)  // iterate over long lines
        {
            lines[i] = lines[i].Substring(0, 5); // replace the line with its substring(stripped) value
        }
    }
    
    0 讨论(0)
  • 2020-12-22 04:28

    You can extract them from the lines of the Textbox like:

    var myarr = myTextBox.Lines.Select(n => n.Remove(3)).ToArray();
    

    Btw, you're most likely better off with a DataGridView Control.

    0 讨论(0)
  • 2020-12-22 04:44

    How about this, this should behave like the MaxLength property and handle the addition of the new char before it occurs, not after. When a key is pressed it checks the length of the line the cursor is currently on.

    public class MaxPerLineTextBox : TextBox
    {
        public MaxPerLineTextBox()
        {
            base.Multiline = true;
        }
    
        public override bool Multiline
        {
            get
            {
                return true;
            }
    
            set
            {
                throw new InvalidOperationException("Readonly subclass");
            }
        }
    
        public int? MaxPerLine { get; set; }
    
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (char.IsControl(e.KeyChar))
            {
                base.OnKeyPress(e);
                return;
            }
    
            int maxPerLine;
            if (this.MaxPerLine.HasValue)
            {
                maxPerLine = this.MaxPerLine.Value;
            }
            else
            {
                base.OnKeyPress(e);
                return;           
            }
    
            var count = 0;
            var lineLength = this.Lines.Select(line =>
                {
                    count += line.Length;
                    return new { line.Length, count };
                })
                .Last(c => c.count < this.SelectionStart).Length;
    
            if (lineLength < maxPerLine)
            {
                base.OnKeyPress(e);
                return;
            }
    
            e.Handled = true;
        }
    }
    
    0 讨论(0)
  • 2020-12-22 04:47

    You can handle OnTextChanged event and then use Lines property to get array of lines. Then simply check whenever any line has more characters than you allow and if so - correct it and set as a Lines again. You have to also prevent event from rising when you do such correction, use the flag or unsubscribe/subscribe technique.

    0 讨论(0)
提交回复
热议问题