I often have the same problem. What throws a wrench in the gears is that Flash puts the scrollpane (or scrollbar) on the stage and sets its properties
before the contents of the scrollpane are loaded. Thus, the scrollpane/scrollbar will never scroll the length of a moviclip or a textfield if that _mc/_txt is generated dynamically at runtime. The way you have to work around this problem is to set the scrollpan/scrollbar properties
after the content has been loaded/generated.
Using textfields can be pesky because the textField.getTextExtent() method is almost always unreliable. What I do is make a calculation based on how many characters there are in the text (text.length returns the number of characters). Of course, I have to guess how high the textfield will be based on the width, how many characters usually fit onto one line of text at that width (based on font size and spacing), and then how many vertical pixels each line will take up. This usually can take some time at first, tweaking the values of the calculating equation until your textfields all do justice to the dynamic text, but eventually you get it right. Here's what my code might look like to calculate this (based on arial, 11pt, in a textfield of 270px wide):
ActionScript Code:
var myTxtLength = myTxt.length; // get character length for "myTxt"
var myTxtLines = (myTxtLength/60) + 3; // 60 characters per line, plus 3 extra lines just to be safe
var myTxtHeight = 15 * myTxtLines; // 15 pixels per line;
var thisTxtHeight = myTxtHeight; // this is the calculated height of the text
When you change the font, the font size, and how wide the text is, you'll have to tweak the values to get it right.
Then you can simply add up all the thisTxtHeight values (or, if the textfields are in individual movie clips, you can add up the heights of the movieclips) and get a totalHeight which you can use to manually set your scrollpane (e.g. each time you generate a textfield, include this line):
ActionScript Code:
totalHeight = totalHeight + thisTxtHeight;
However, since you have a totalHeight, you can use a scrollbar and the myScrollbar.setScrollProperties() method to do this.
What I do is put all of my smaller movieClips or textFields into a larger movieClip (called "results_mc"). Then you simply set the scrollbar to move "results_mc" up and down. First you set the scrollbar properties:
ActionScript Code:
// now we want to set the scrollbar to scroll the length of "results_mc"
var thumb = totalHeight/20; // make the width of the scrollbar "thumb" 1/20 of total height
myScrollbar.setScrollProperties(thumb,0,totalHeight); // set scrollbar's properties
myScrollbar.setChangeHandler("scroller"); // call the scroller() to control its movement
Notice that at the end I set the scrollbar to call a function called "scroller" whenever the scrollbar changes (e.g. when the user drags the "thumb" up or down). The scroller() function controls the movement:
ActionScript Code:
function scroller() { // each time the scrollbar moves, perform this function
var position = myScrollbar.getScrollPosition(); // get current position
_root.results_mc._y = -position; // when scrollbar goes one way, results_mc goes the other
} // end scroller()
Then you have a scollbar that always scrolls through the text, no matter what the dynamic content or how many movieclips and/or textfields have been created. The trick is calculating the height of each textfield and -- as you are generating the textfields -- keeping a sum of how tall the stack of textfields is getting. Once you have that totalHeight, then you can do whatever you want with your scrollbar (or scrollpane, but the scrollpane seems more complicated for what you're trying to do).
Hope this helps!
retrotron