2009-11-20 66 views

回答

6

如果富文本框是_rtb,那么你可以得到的可视行数:

public int NumberOfVisibleLines 
{ 
    get 
    { 
     int topIndex = _rtb.GetCharIndexFromPosition(new System.Drawing.Point(1, 1)); 
     int bottomIndex = _rtb.GetCharIndexFromPosition(new System.Drawing.Point(1, _rtb.Height - 1)); 
     int topLine = _rtb.GetLineFromCharIndex(topIndex); 
     int bottomLine = _rtb.GetLineFromCharIndex(bottomIndex); 
     int n = bottomLine - topLine + 1; 
     return n; 
    } 
} 

然后,如果你想插入符号的滚动,说,方式1/3 richtextbox的顶部,请执行以下操作:

int startLine = _rtb.GetLineFromCharIndex(ix); 
int numVisibleLines = NumberOfVisibleLines; 

// only scroll if the line to scroll-to, is larger than the 
// the number of lines that can be displayed at once. 
if (startLine > numVisibleLines) 
{ 
    int cix = _rtb.GetFirstCharIndexFromLine(startLine - numVisibleLines/3 +1); 
    _rtb.Select(cix, cix+1); 
    _rtb.ScrollToCaret(); 
}