VB.NET - Expand text box size - Asked By onay omas on 05-Sep-11 03:28 AM

how to make a textbox so that it can be expanded to bigger size so user can add more text required.
im using vb.net in window based application.
tq.
dipa ahuja replied to onay omas on 05-Sep-11 03:31 AM
Option 1:

Increase the MaxLength property of TextBox:

Option 2:

If you want more text to hold by textBox then you have choice to use the RichTextBox instead of simple textBox
onay omas replied to dipa ahuja on 05-Sep-11 03:49 AM
i want to make the width /height of the textbox will be automatically expand to bigger size.
for option 1, i know we can set maximum length of char that we can insert. but, the widht/height is static right?
can we make the width/height of the size can be expanded?
smr replied to onay omas on 05-Sep-11 04:48 AM
Hi

You can either go for a Label with the AutoSize property set to True. Or if you want to stick to TextBox, you can make use of MeasureString method and get the width of the text from there. Use that width to set the width of TextBox.
Anoop S replied to onay omas on 05-Sep-11 05:44 AM

To get the textarea to stretch, give this a go (jQuery): http://www.unwrongest.com/projects/elastic/.

As for the height of exactly one row, you could try rows="1" in the <textarea> tag, as well as removing CSS styling. Some browsers, however, may set a textarea's minimum height to more than one row.

Another solution would be to use a div on screen that, when the user clicks on it, makes the browser focus on a hidden textbox. The user types into the hidden text box, and the div is given the textbox's contents via. JavaScript. The tricky bit then is getting the flashing cursor, but you could use the pipe | character in the div and make it flash, although this starts getting long winded about the time you start it.
Anoop S replied to onay omas on 05-Sep-11 05:45 AM
Or
You can manually control the size of the textarea using the CSS height and width parameters. By binding a keypress event to the textarea, you can get the amount of text in the box and adjust the height accordingly. For example, here's some jQuery that will add 20 pixels to the height of the box for every 50 characters you put in:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">
</script>
 
<textarea id="textbox" style="height:20px;width:400px;"></textarea>
 
<script type="text/javascript">
$(document).ready(function() {
  $("#textbox").val('');
  $("#textbox").keypress(function() {
    var textLength = $("#textbox").val().length;
    if (textLength % 50 == 0) {
      var height = textLength/50;
      $("#textbox").css('height', 20+(height*20));
    }
  });
});
</script>


You can tweak the values to get the desired effect.
onay omas replied to smr on 05-Sep-11 09:53 PM
How to use the label?