Results 1 to 5 of 5

Thread: How to move a RichTextBox with mouse

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Location
    Germany, near Munich
    Posts
    172

    How to move a RichTextBox with mouse

    Hi,

    I would like to find out, how a RichTextBox can be moved by mouse. I have written some code for it, but it does not work well. The RTB does only move, when the mouse hits its border. The mouse is not really captured, too. It releases the RTB whenever it is moving outside RTB´s area.

    Here´s the code:

    XAML:
    Code:
    <Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Grid>
            <Canvas Margin="1" Name="Canvas1">
                <RichTextBox Canvas.Left="45" Canvas.Top="55" Height="100" Name="RichTextBox1" Width="200" />
            </Canvas>
        </Grid>
    </Window>
    VB:
    Code:
    Class Window1 
        Private mMouseDownPosition As Point
        
        Private Sub RichTextBox1_PreviewMouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles RichTextBox1.PreviewMouseDown
            RichTextBox1.CaptureMouse()
            mMouseDownPosition = e.GetPosition(Canvas1)
            Beep()
        End Sub
    
        Private Sub RichTextBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles RichTextBox1.MouseMove
            Dim x As Point = e.GetPosition(Canvas1)
            If (e.LeftButton = MouseButtonState.Pressed) Then
                Canvas.SetLeft(RichTextBox1, Canvas.GetLeft(RichTextBox1) + (x.X - mMouseDownPosition.X))
                Canvas.SetTop(RichTextBox1, Canvas.GetTop(RichTextBox1) + (x.Y - mMouseDownPosition.Y))
            End If
            mMouseDownPosition = x
        End Sub
    
    
        Private Sub RichTextBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles RichTextBox1.MouseUp
            RichTextBox1.ReleaseMouseCapture()
        End Sub
    
       
    End Class

    Greetings,

    TheTree
    ********* Look at http://www.bahai.org - and enjoy it! *********

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to move a RichTextBox with mouse

    Try this
    vb.net Code:
    1. Public Class Form1
    2.     Private intXPos As Integer = 0
    3.     Private intYPos As Integer = 0
    4.     Private blnIsMoving As Boolean = False
    5.  
    6.     Private Sub RichTextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDown
    7.         intXPos = e.X
    8.         intYPos = e.Y
    9.         blnIsMoving = True
    10.     End Sub
    11.  
    12.     Private Sub RichTextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseMove
    13.         If blnIsMoving Then
    14.             RichTextBox1.Top = RichTextBox1.Top - (intYPos - e.Y)
    15.             RichTextBox1.Left = RichTextBox1.Left - (intXPos - e.X)
    16.         End If
    17.     End Sub
    18.  
    19.     Private Sub RichTextBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseUp
    20.         blnIsMoving = False
    21.     End Sub
    22.  
    23. End Class

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Location
    Germany, near Munich
    Posts
    172

    Re: How to move a RichTextBox with mouse

    Hi Hack,

    thanks for your reply. Your code runs under WindowsForms.

    I would like to code a WPF-app, so that RTBs, Lines and other controls can be added and moved by mouse.

    Greetings,

    TheTree
    ********* Look at http://www.bahai.org - and enjoy it! *********

  4. #4
    Lively Member
    Join Date
    Jul 2007
    Posts
    127

    Re: How to move a RichTextBox with mouse


  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Location
    Germany, near Munich
    Posts
    172

    Re: How to move a RichTextBox with mouse

    Hi bflosabre91,

    thank you for that link. Good code.

    Greetings,

    Michael
    ********* Look at http://www.bahai.org - and enjoy it! *********

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width