wpf,how to restrict MaxLength of TextBox? -


<textbox height="?" width="?" acceptreturn="true" /> 

height=random; width=random; example,height 60, width 100. how control inputed lentgh of text less size of textbox

use maxlength

edit:

wait, what? want restrict number of chars width of textbox? why that?

edit2:

you can measure length of string using graphics.measurestring. here's extension method want:

public static class textboxextension {     public static int visiblecharcount(this textbox textbox)     {         int count = 0;          {             count++;             var teststring = new string('x', count);             var stringwidth = system.drawing.graphics.measurestring(teststring, textbox.font);                         } while (stringwidth < textbox.width);          if (stringwidth == textbox.width)              return count;         else             return count-1;     } } 

use this:

mytextbox.maxlength = mytextbox.visiblecharcount(); 

edit3:

if texbox multiline , want take height account, can use overload of measurestring takes size. leave modify example accordingly.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -