Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to create a WPF Usercontrol where there are three controls

-ListBox

-TextBox or Textblock

-ScrollBar

I wish to synchronize the scrolling between all of them.


XML
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100"></ColumnDefinition>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
        <ColumnDefinition Width="20*" />
    </Grid.ColumnDefinitions>

    <ListBox Grid.Column="0" HorizontalAlignment="Left" Name="lstNumber" Width="100" FontFamily="Courier New" FontSize="12" Background="Aqua" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.ScrollChanged="RichTextBox_ScrollChanged"/>
    <TextBox Grid.Column="1" HorizontalAlignment="Stretch" Name="txtData" Width="Auto" FontFamily="Courier New" FontSize="12" Margin="101,0,0,0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.ScrollChanged="RichTextBox_ScrollChanged"/>
    <ScrollBar Grid.Column="3" HorizontalAlignment="Right" Scroll="ScrollBar_Scroll"/>
</Grid>







Related C# Code for the event.


C#
private void RichTextBox_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {
        if (sender == txtData)
        {
            //what to do now?
        }
        if (sender == lstNumber)
        {
            //what to do now?
        }
    }





How to achieve this functionality?
Posted
Updated 24-Oct-17 11:04am
Comments
Subramanyam Shankar 13-Mar-15 11:36am    
use the scroll into view . use it as cascade so that if one changes the other also changes.
Sergey Alexandrovich Kryukov 13-Mar-15 14:57pm    
Generally, this is a very bad design idea. You can waste too much time on that and still have bad results. I would advise to redesign the UI instead. Perhaps I could help, but I don't know the ultimate goals of this activity.
—SA

I am trying to show a text file like a code editor. Where 3 things must be available

- Line number
- Highlight similar text, when I select a text in the whole loaded file.
- for scrolling, separate scrollbar must be available
- Up & down, Pg Up & Pg Down, Home & End Key from keyboard should function for scrolling.
 
Share this answer
 
You can use the scroll using User32


Const WM_USER As Integer = &H400
Const EM_GETSCROLLPOS As Integer = WM_USER + 221
Const EM_SETSCROLLPOS As Integer = WM_USER + 222

Declare Function SendMessage Lib "user32.dll" Alias "SendMessageW" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByRef lParam As Point) As Integer

Private Sub RichTextBox2_VScroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox2.VScroll

Dim pt As Point

SendMessage(RichTextBox2.Handle, EM_GETSCROLLPOS, 0, pt)

SendMessage(RichTextBox1.Handle, EM_SETSCROLLPOS, 0, pt)

End Sub
 
Share this answer
 
Comments
Richard Deeming 25-Oct-17 12:08pm    
TWO AND A HALF YEARS too late, and with an answer that won't work in WPF.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900