自动文本框宽度

问题描述:

有一个similar thread about this。但我想拥有自动宽度的多行文本框(适合宽度较大的行)。自动文本框宽度

有了这个代码,我可以有一个多行TextBox(自动高度)

 <div style="float:left; white-space:nowrap "> 
     <asp:TextBox style="display:inline; overflow:hidden" 
        ID="txt1" 
        runat="server" 
        Wrap="false" 
        ReadOnly="true" 
        TextMode="MultiLine" 
        BorderStyle="none" 
        BorderWidth="0"> 
     </asp:TextBox> 
    </div> 
    <div style="float:left"> 
     <asp:TextBox ID="txt2" runat="server" Text="Example textbox"></asp:TextBox> 
    </div> 

后面的代码:

txt1.Rows= text.Split("|").Length ' Sets number of rows but with low performance 
txt1.Text = text.Replace("|", Environment.NewLine) 

再次感谢您的帮助。

你可以尝试LINQ方法:

string[] rows = text.Split('|'); 
int maxLength = rows.Max(x => x.Length); 

txt1.Rows = rows.Length; 
txt1.Columns = maxLength; 

如果你是开放的插件使用像jQuery, 你应该看看自动调整大小的插件。

这些将调整为用户类型。

退房一个autoresize

$(document).ready(function(){ 
    $('textarea').autosize(); 
}); 
+0

伟大的提示nunespascal,但我不能使用jQuery,谢谢。 – Coyolero 2012-07-11 18:55:07

乔尔埃瑟顿给我如何解决这个使用LINQ一个真正的好工作的代码示例,但unfurtenly我不能使用Linq。

使用LINQ(珥埃瑟顿氏溶液)多行TextBox汽车宽度: C#

string[] rows = text.Split('|'); 
int maxLength = rows.Max(x => x.Length); 

txt1.Rows = rows.Length; 
txt1.Columns = maxLength; 

VB

Dim rows() As String = text.Split("|") 
    Dim maxLength As Integer = rows.Max(Function(x) x.Length) 
    txt1.Rows = rows.Length 
    txt1.Columns = maxLength 
    text = text.Replace("|", Environment.NewLine) 
    txt1.Text = text 

多行TextBox汽车宽度溶液2 向该 “手动” 实现,我用这种方法知道较大的行长。是不是最有效的,但它对我有效:

Dim textRows() As String = text.Split("|") 

    For Each row As String In textRows 
     row = row.Trim 
     textToDisplay = String.Format("{0}{1}{2}", textToDisplay, row, Environment.NewLine) 
     If row.Length > maxRowLenght Then 
      maxRowLenght = row.Length 
     End If 
    Next 
    txt1.Rows = textRows.Length 
    txt1.Columns = maxRowLenght 
    txt1.Text = textToDisplay