views:

283

answers:

1

i need to scroll the textblock text. For example, the textblock contains 700 words. These words are populated to the textblock from code behind as it could be a different 700 words depending on some "stuff". There is a textbox on this form as well. The user types the words in the textblock into the textbox. As they type i keep track of which word from the textblock they are on. However, not all the words in the textblock will fit in the textblock viewing area, so i need to scroll the textblock from code behind. How do i go about doing this.

I"m using silverlight 3.

Thanks shannon

sorry.. i didn't realize i didn't ask the question on it's own thread.. I'll post again..

A: 

First you'll want to wrap your TextBlock in a ScrollViewer control. At that point, you can set the vertical and horizontal offsets of the ScrollViewer in the code behind like this. Note this assumes a ScrollViewer control named ScrollViewer1.

    Private Sub ScrollViewer1_MouseWheel(ByVal sender As Object, By Val e as System.Windows.Input.MouseWheelEventArgs)
        Me.ScrollViewer1.VerticalOffset = Me.ScrollViewer1.VerticalOffset + e.Delta
    End Sub
Steve Danner
thanks for the response.. i'm using vb.net for the backend.. and i've tried to convert what you've given.. but getting an error.Dim focusedElement As FrameworkElement focusedElement = TryCast(FocusManager.GetFocusedElement(), FrameworkElement) Dim focusedVisualTransform As GeneralTransform focusedVisualTransform = focusedElement.TransformToVisual(Me.ScrollViewer1)the error is on this last line. object reference not set to an instance. I tried creating a new instantce.. no luck.. hope you can continue to provide some help
jvcoach23
i did name the scrollviewer to ScrollViewer1
jvcoach23
My code is actually aimed at moving a scrollbar as you tab through elements within it. You're getting the error because you dont' have a focused element. See my edit to handle the MouseWheel event of ScrollViewer1, which should do what you want. I also converted it to VB.
Steve Danner
just realized that you were suggesting the focusedElement.. the scrollviewer and textblock.. would they be the focused element... if the textblock is getting populated and handled completely in code behind... the user never touches anything in that textblock.
jvcoach23
Yeah, see my comments above. Also, the code I gave in the edit might need to be modifed to check for a negative offset or an offset greater than allowed...
Steve Danner
great.. thanks for the code... i think i have it working now.. your guidence pointed me in the right direction.. again many thanks
jvcoach23