Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic TextBox Width

Tags:

html

.net

asp.net

There is a similar thread about this. But I want to have a multiline TextBox with automatic width (fit width to the larger row).

With this code I can have a multiline TextBox (automatic height)

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

Code behind:

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

Once again, thanks for your help.

like image 926
Coyolero Avatar asked Oct 28 '25 07:10

Coyolero


1 Answers

You could try a linq approach:

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

txt1.Rows = rows.Length;
txt1.Columns = maxLength;
like image 90
Joel Etherton Avatar answered Oct 29 '25 21:10

Joel Etherton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!