ClanKiller.com
https://forums.plasmasky.com/

c# : playing with textareas
https://forums.plasmasky.com/viewtopic.php?f=24&t=2605
Page 1 of 1

Author:  Satis [ Tue Sep 11, 2007 12:52 pm ]
Post subject:  c# : playing with textareas

so... I was playing around with textarea inputs prepopulated by text data from a database. The length of the text field could be wildly varying. If I statically set the rows and cols of the textarea then it may be way too big (if it's a one-line text field that comes back) or way too small.

So I was just playing around with a "function" to dynamically adjust the text area to match the length of the text. It's not perfect.... the only way to control the height is by using the rows argument... style height doesn't do anything. In this case I set the width of the textarea to 500px so you'll need to adjust the function accordingly.

Code:
                int numlines = 0;
                string thetext = Convert.ToString(dr.GetValue(5));
                string[] brokentext = thetext.Split(Environment.NewLine.ToCharArray());
                int nulllinecounter = 0;
                foreach (string text in brokentext)
                {
                    int numlineswrapped = text.Length / 60;
                    if (numlineswrapped == 0) {
                        if (text.Length == 0)
                        {
                            nulllinecounter++;
                            if (nulllinecounter == 2)
                            {
                                numlineswrapped++;
                                nulllinecounter = 0;
                            }
                        }
                        else
                        {
                            numlineswrapped = 1;
                        }
                    }
                    numlines += numlineswrapped;
                }

numlines goes in the rows= part of the textarea. It seems that lines that have a newline without any text in it take up less room then rows that do have text in it, hence the nulllinecounter. It's not perfect but it's better than just doing something like

Code:
int numlines = Convert.ToString(dr.GetValue(5)).Length / 55;

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/