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