开发者

Multiline TextBox. Maximum lines allowed

I suppose there's a limit for the lines alowed in a TextBox with the MultiLine option set to true.

I have a program that every several minutes, checks an email account, but for control purposes i put a TextBox that indicates what's been doing.

My curiosity is, does anyone know how much lines are allowed ? And does throw an exception when reached that line ?

EDIT Sorry forgot开发者_Go百科 to mention is in WinForms

EDIT 2 Perhaps, someone knows of a way to eliminate older lines, will grated appreciated


There's no limit on the number of lines that a text box can display.

There is, however, a limit on the number of characters that the control can hold. See this question for further details on this topic.


If you set the TextBox.MaxLength property to zero, the amount of text is limited only by available memory.


Another solution:

<TextBox x:Name="txtAddress" 
MaxLines="6"
TextWrapping="Wrap"  AcceptsReturn="True"
VerticalScrollBarVisibility="Hidden" 
HorizontalScrollBarVisibility="Hidden"
TextChanged="txtAddress_TextChanged"
PreviewTextInput="txtAddress_PreviewTextInput"
PreviewKeyDown="txtAddress_PreviewTextInput"/>

//...

private void txtAddress_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox txtBx = sender as TextBox;
if (txtBx.LineCount > txtBx.MaxLines)
txtAddress.Text = this._textBeforInput;
}

private string _textBeforInput = string.Empty;
private void txtAddress_PreviewTextInput(object sender, EventArgs e)
{
this._textBeforInput = txtAddress.Text;
}


There is no such limitation on Multiline TextBox both in WinForms and ASP.NET. (I have no idea of WPF :) )


I know this question is really old and has already been answered, but for those searching for an HTML/ASP.NET solution I have created a short jsFiddle http://jsfiddle.net/Z3rdZ/2/

HTML

<textarea id="limited-lines" maxlines="4"></textarea>

jQuery

$('#limited-lines').keydown(function(event){
    if ( event.which == 13 ) {
        var numberOfLines = $(this).val().split('\n').length;
        if(numberOfLines >= $(this).attr('maxlines')){
            event.preventDefault();   
        }
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜