2012-07-11 102 views
3

有一個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) 

再次感謝您的幫助。

回答

2

你可以嘗試LINQ方法:

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

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

如果你是開放的插件使用像jQuery, 你應該看看自動調整大小的插件。

這些將調整爲用戶類型。

退房一個autoresize

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

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

0

喬爾埃瑟頓給我如何解決這個使用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