Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Richtextbox doubledlick and richtextbox resize causes text to be selected automatically.

Posted on 2006-10-27
6
Medium Priority
?
486 Views
Last Modified: 2010-08-05
Hi.

This code works!  This code captures "Doubleclicks" on a RichTextBox and then maximizes the richtextbox size.  The problem is that some of the text in the richtextbox becomes selected automatically.  
The problem is not the MaximizeRichTextbox subroutine.  I've proven that.  

Can you help me to stop the automatic text selection.

Thank you.


 Private Sub txtrichbxMemo_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtrichbxMemo.MouseDown
        Static lastclick As DateTime
        If Convert.ToString(lastclick) = "#12:00:00 AM#" Then
            lastclick = DateTime.Now : Exit Sub : End If
        Dim Current_Time As DateTime = DateTime.Now
        Dim diff As TimeSpan = Current_Time.Subtract(lastclick)
        lastclick = DateTime.Now
        If diff.Hours * 1000 + diff.Minutes * 1000 + _
        diff.Seconds * 1000 + diff.Milliseconds < 500 Then MaximizeRichTextbox()
end sub
0
Comment
Question by:peispud
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17820870
What is wrong with using the DoubleClick() event?...

    Private Sub txtrichbxMemo_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtrichbxMemo.DoubleClick
        Dim start As Integer = txtrichbxMemo.SelectionStart
        Dim length As Integer = txtrichbxMemo.SelectionLength

        MaximizeRichTextbox()

        txtrichbxMemo.SelectionStart = start
        txtrichbxMemo.SelectionLength = length
    End Sub

The above code stores the current selection start/length and then restores them after calling MaximizeRichTextbox().



On a side note, your original code could have been written as:

    Private Sub txtrichbxMemo_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtrichbxMemo.MouseDown
        Static lastclick As DateTime

        If lastclick <> Nothing Then
            Dim ts As TimeSpan = DateTime.Now.Subtract(lastclick)
            If ts.TotalMilliseconds < 500 Then
                Dim start As Integer = txtrichbxMemo.SelectionStart
                Dim length As Integer = txtrichbxMemo.SelectionLength

                MaximizeRichTextbox()

                txtrichbxMemo.SelectionStart = start
                txtrichbxMemo.SelectionLength = length
            End If
        End If

        lastclick = DateTime.Now
    End Sub
0
 

Author Comment

by:peispud
ID: 17822472
The richtextbox control in visual basic.net does not have a doubleclick event..    i have been trying to create one with using addhandler with no luck.

Also, the code above does not solve the original problem.   When the richtextbox is maximized, some of the text still becomes selected (highlighted)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17822733
You must have VB.Net 2003 (or below)?  There is in fact a DoubleClick() event for the RichTextBox in VB.Net 2005.

Really can't give you any further advice then without seeing more code!

Show us your MaximizeRichTextbox() sub please so we can get a better idea what is happening...

By the way, here is one way to trap a DoubleClick in a RTB in VB.Net 2003:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private WithEvents myRtbHandler As RtbHandler

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myRtbHandler = New RtbHandler(Me.txtrichbxMemo)
    End Sub

    Private Sub myRtbHandler_RtbDoubleClicked(ByVal RTB As System.Windows.Forms.RichTextBox) Handles myRtbHandler.RtbDoubleClicked
        Debug.WriteLine("RTB was Double Clicked")
    End Sub

    Private Class RtbHandler
        Inherits NativeWindow

        Private Const WM_LBUTTONDBLCLK As Integer = &H203
        Private RTB As RichTextBox

        Public Event RtbDoubleClicked(ByVal RTB As RichTextBox)

        Public Sub New(ByVal RTB As RichTextBox)
            If Not (RTB Is Nothing) Then
                Me.RTB = RTB
                Me.AssignHandle(RTB.Handle)
            End If
        End Sub

        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            Select Case m.Msg
                Case WM_LBUTTONDBLCLK
                    RaiseEvent RtbDoubleClicked(Me.RTB)

            End Select

            MyBase.WndProc(m)
        End Sub

    End Class

End Class
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17822754
Here...made a slight modification to the WndProc() above and the default behaviour for the DoubleClick() even has been suppressed:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private WithEvents myRtbHandler As RtbHandler

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myRtbHandler = New RtbHandler(Me.txtrichbxMemo)
    End Sub

    Private Sub myRtbHandler_RtbDoubleClicked(ByVal RTB As System.Windows.Forms.RichTextBox) Handles myRtbHandler.RtbDoubleClicked
        Debug.WriteLine("Double Click on RTB")
        MaximizeRichTextbox()
    End Sub

    Private Sub MaximizeRichTextbox()
        Debug.WriteLine("MaximizeRichTextbox()")
    End Sub

    Private Class RtbHandler
        Inherits NativeWindow

        Private Const WM_LBUTTONDBLCLK As Integer = &H203
        Private RTB As RichTextBox

        Public Event RtbDoubleClicked(ByVal RTB As RichTextBox)

        Public Sub New(ByVal RTB As RichTextBox)
            If Not (RTB Is Nothing) Then
                Me.RTB = RTB
                Me.AssignHandle(RTB.Handle)
            End If
        End Sub

        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            Select Case m.Msg
                Case WM_LBUTTONDBLCLK
                    RaiseEvent RtbDoubleClicked(Me.RTB)
                    Exit Sub ' suppress default behaviour

            End Select

            MyBase.WndProc(m)
        End Sub

    End Class

End Class
0
 

Author Comment

by:peispud
ID: 17822958
You were exactly right.  I am using VB.Net 2003.

And of course,  your solution works perfectly.    Thank you .

PS... Is there a significant advantage to moving up to the latest version of VB.Net?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 17823098
I don't do enough development to really know.  I mostly only write snippets for things on EE these days...

VB.Net 2005 does have some nice features though.  The big ones that I really like are a Forms collection and default instances of forms like in the old VB6.  There are lots of other new things as well I'm sure.

Kind of a vague answer there sorry...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question