peispud
asked on
Richtextbox doubledlick and richtextbox resize causes text to be selected automatically.
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(By Val sender As Object, ByVal e As System.Windows.Forms.Mouse EventArgs) 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(last click)
lastclick = DateTime.Now
If diff.Hours * 1000 + diff.Minutes * 1000 + _
diff.Seconds * 1000 + diff.Milliseconds < 500 Then MaximizeRichTextbox()
end sub
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(By
Static lastclick As DateTime
If Convert.ToString(lastclick
lastclick = DateTime.Now : Exit Sub : End If
Dim Current_Time As DateTime = DateTime.Now
Dim diff As TimeSpan = Current_Time.Subtract(last
lastclick = DateTime.Now
If diff.Hours * 1000 + diff.Minutes * 1000 + _
diff.Seconds * 1000 + diff.Milliseconds < 500 Then MaximizeRichTextbox()
end sub
ASKER
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)
Also, the code above does not solve the original problem. When the richtextbox is maximized, some of the text still becomes selected (highlighted)
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.txtrichbxMem o)
End Sub
Private Sub myRtbHandler_RtbDoubleClic ked(ByVal RTB As System.Windows.Forms.RichT extBox) Handles myRtbHandler.RtbDoubleClic ked
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.Messa ge)
Select Case m.Msg
Case WM_LBUTTONDBLCLK
RaiseEvent RtbDoubleClicked(Me.RTB)
End Select
MyBase.WndProc(m)
End Sub
End Class
End Class
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.txtrichbxMem
End Sub
Private Sub myRtbHandler_RtbDoubleClic
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.Messa
Select Case m.Msg
Case WM_LBUTTONDBLCLK
RaiseEvent RtbDoubleClicked(Me.RTB)
End Select
MyBase.WndProc(m)
End Sub
End Class
End Class
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.txtrichbxMem o)
End Sub
Private Sub myRtbHandler_RtbDoubleClic ked(ByVal RTB As System.Windows.Forms.RichT extBox) Handles myRtbHandler.RtbDoubleClic ked
Debug.WriteLine("Double Click on RTB")
MaximizeRichTextbox()
End Sub
Private Sub MaximizeRichTextbox()
Debug.WriteLine("MaximizeR ichTextbox ()")
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.Messa ge)
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
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.txtrichbxMem
End Sub
Private Sub myRtbHandler_RtbDoubleClic
Debug.WriteLine("Double Click on RTB")
MaximizeRichTextbox()
End Sub
Private Sub MaximizeRichTextbox()
Debug.WriteLine("MaximizeR
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.Messa
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
ASKER
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Private Sub txtrichbxMemo_DoubleClick(
Dim start As Integer = txtrichbxMemo.SelectionSta
Dim length As Integer = txtrichbxMemo.SelectionLen
MaximizeRichTextbox()
txtrichbxMemo.SelectionSta
txtrichbxMemo.SelectionLen
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(By
Static lastclick As DateTime
If lastclick <> Nothing Then
Dim ts As TimeSpan = DateTime.Now.Subtract(last
If ts.TotalMilliseconds < 500 Then
Dim start As Integer = txtrichbxMemo.SelectionSta
Dim length As Integer = txtrichbxMemo.SelectionLen
MaximizeRichTextbox()
txtrichbxMemo.SelectionSta
txtrichbxMemo.SelectionLen
End If
End If
lastclick = DateTime.Now
End Sub