4

关于这个有一个类似的线程。但我想要一个具有自动宽度的多行文本框(使宽度适合较大的行)。

使用此代码,我可以拥有一个多行文本框(自动高度)

     <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)

再次感谢您的帮助。

4

3 回答 3

3

您可以尝试一种 linq 方法:

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

txt1.Rows = rows.Length;
txt1.Columns = maxLength;
于 2012-07-11T18:19:09.730 回答
1

如果你愿意使用像 jquery 这样的插件,你应该看看 autoresize 插件。

这些也将根据用户类型调整大小。

查看一个自动调整大小

$(document).ready(function(){
    $('textarea').autosize();  
});
于 2012-07-11T18:28:13.757 回答
0

Joel Etherton 给了我一个很好的工作代码示例,说明如何使用 Linq 解决这个问题,但我无法使用 Linq。

使用 Linq 的多行文本框自动宽度(Joel Etherton 的解决方案):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

多行文本框自动宽度解决方案 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
于 2012-07-12T05:10:11.813 回答