It is currently Thu Mar 28, 2024 10:43 pm



Reply to topic  [ 1 post ] 
c# : playing with textareas 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 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;

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Tue Sep 11, 2007 12:52 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.