Solved

vb6 Rich TextBox - prevent text export

Posted on 2014-11-29
33
144 Views
Last Modified: 2014-12-20
Dear Experts,

I want to prevent any text from being copied from a VB6 rich textbox (RTB) and pasted  to another control or application.

Also, I want to prevent any drag and drop from the RTB to another control or application.

Can you please assist?

Thanks!
0
Comment
Question by:ttobin333
  • 14
  • 14
  • 4
  • +1
33 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
Have you tried setting the Enabled property to False?
0
 

Author Comment

by:ttobin333
Comment Utility
That interferes with other functions. I am just trying to disable ways to export text contained in the box, not totally disable the box.
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
1. You can prevent the RTF textbox from receiving focus
2. You can detect attempts at selecting text and prevent them
3. You might display the text as a picturebox image
4. In the background, you could populate the clipboard object with an empty string or a warning about copyrighted material inside a loop as long as the RTF textbox has focus.
0
 

Author Comment

by:ttobin333
Comment Utility
I would like the rich text box to function as normally as possible, including editing of text and copy/pasting within the box itself. Just trying to prevent any exporting of material within the box to anywhere outside.
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
If you are going to allow copy/paste, then you are allowing the content to leave your application.  The clipboard is a universal (inter-application) Windows object.
0
 

Author Comment

by:ttobin333
Comment Utility
Yes, I am aware of that, but I am looking for a creative solution.
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
You could simulate a copy/paste between controls
0
 

Author Comment

by:ttobin333
Comment Utility
Would you please elaborate on the simulated copy/paste?
0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 200 total points
Comment Utility
If your user selects text and wants to do a copy/paste operation, your code would put the selected text into an application variable rather than the clipboard object.  Your application would retrieve the stored (selected and copied) text from the variable and insert it at the cursor location of the other/same control in your application.
0
 
LVL 11

Assisted Solution

by:Dany Balian
Dany Balian earned 300 total points
Comment Utility
you can try to disable the ctrl+c event in the box, and right mousedown click!
and as @aikimark already pointed, you can write your own copy/paste code just add 2 buttons to your form with the names btnCopy and btnPaste and try the code below (assuming your richtextbox is named rtb)

Dim cboard As String

Private Sub btnCopy_Click()
    If rtb.SelLength > 0 Then
        cboard = Mid(rtb.Text, rtb.SelStart + 1, rtb.SelLength)
    End If
End Sub

Private Sub btnPaste_Click()
    Dim t As String, pos As Long
    If Len(cboard) > 0 Then
        pos = rtb.SelStart + Len(cboard)
        t = Mid(rtb.Text, 1, rtb.SelStart) & cboard & Mid(rtb.Text, rtb.SelStart + 1, Len(rtb.Text))
    End If
    rtb.Text = t
    rtb.SelStart = pos
    rtb.SetFocus
End Sub

Private Sub rtb_KeyUp(KeyCode As Integer, Shift As Integer)
    If Shift = vbCtrlMask And (KeyCode = 67 Or KeyCode = 88) Then
        Clipboard.Clear
    End If
End Sub

Private Sub rtb_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = vbRightButton Then Exit Sub
End Sub

Open in new window


EDIT: edited the code to prevent both ctrl+c, and ctrl+x
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
You could trap the Ctrl+X, Ctrl+C, and Ctrl+V keystroke combinations as an alternative to two buttons to do the cut/copy/paste operation.
0
 
LVL 11

Expert Comment

by:Dany Balian
Comment Utility
Yes, that would be smarter! hehe
0
 

Author Comment

by:ttobin333
Comment Utility
Thanks guys, I think that will take care of the copy/paste issue. Any suggestions on preventing drag and drop from the textbox to outside, if possible without disabling drag and drop within the box?
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
Since the form real estate surrounds the textbox, you should be able to detect and thwart and dragging outside of the textbox with a form event.
0
 

Author Comment

by:ttobin333
Comment Utility
Aikimark, that's a good idea, but the form OLEDragOver does not allow use of SetData or Clear for the data object. Any suggestions on how to clear the data on dragging over the form?
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
can you cancel the drag operation?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:ttobin333
Comment Utility
Aikimark, would you please provide specific information regarding VB6?
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
You would have to do two things:
1. Transfer the selected and dragging text to some control that becomes visible when dragging starts.
2. set the new control's dragmode property to manual

When the new control is dragged outside the textbox, set its drag property to vbCancel.

Reference.
http://msdn.microsoft.com/en-us/library/aa240985%28v=vs.60%29.aspx
0
 

Author Comment

by:ttobin333
Comment Utility
Aikimark, you are referring to dragging controls…I am referring to dragging text from a rich textbox.

The point is to prevent dragging/dropping text to outside of the rich textbox. Having to drag into another control in order to prevent this does not solve the problem. The user can just drag the text directly and bypass this.
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
I was thinking about using the control in place of the text.

One thing you can do is to define a clip region around the textbox whenever the mouse moves with the left button pressed.  That way, the mouse can't move outside of the textbox until the mouse_up event.
0
 

Author Comment

by:ttobin333
Comment Utility
That sounds like an intereting option. Can you provide the specifics?
0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 200 total points
Comment Utility
Use the windows ClipCursor API
Code Examples:
http:Q_20504435.html#a7910641
http:Q_21694074.html#a15683199
http:Q_21425198.html
http:Q_20232450.html#a6633609
http:Q_10021961.html#a1433103

There is also the possibility of using a Windows Hook into the mouse, but I haven't used it.
0
 

Author Comment

by:ttobin333
Comment Utility
Oh come on, trying to patch together bits and pieces from various other solutions…not what I am looking for.

A concise, integrated solution is desired.
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
When you detect a mouse move with the left button pressed, then invoke the API to prevent the mouse from leaving the confines of the textbox
0
 
LVL 11

Accepted Solution

by:
Dany Balian earned 300 total points
Comment Utility
ok, summarizing all the comments and remarks here's the code u need:

Option Explicit
Private Declare Function ClipCursor& Lib "user32" (lpRect As RECT)
Private Declare Function ClipCursorBynum& Lib "user32" Alias "ClipCursor" (ByVal lpRect As Long)

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Dim CrsClp As RECT
Dim Throw As Long
Dim cboard As String

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Throw& = ClipCursorBynum&(0)
End Sub

Private Sub rtb_KeyUp(KeyCode As Integer, Shift As Integer)
    If Shift = vbCtrlMask And (KeyCode = 67 Or KeyCode = 88) Then
        Clipboard.Clear
        If rtb.SelLength > 0 Then
            cboard = Mid(rtb.Text, rtb.SelStart + 1, rtb.SelLength)
        End If
    ElseIf Shift = vbCtrlMask And (KeyCode = 86) Then
        Dim t As String, pos As Long
        If Len(cboard) > 0 Then
            pos = rtb.SelStart + Len(cboard)
            t = Mid(rtb.Text, 1, rtb.SelStart) & cboard & Mid(rtb.Text, rtb.SelStart + 1, Len(rtb.Text))
        End If
        rtb.Text = t
        rtb.SelStart = pos
        rtb.SetFocus
    End If
End Sub

Private Sub rtb_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then Exit Sub
    Dim xpixel As Long
    Dim ypixel As Long
    xpixel = Screen.TwipsPerPixelX
    ypixel = Screen.TwipsPerPixelY
    With CrsClp
        .Top = (Me.Top + rtb.Top) / ypixel
        .Bottom = (Me.Top + rtb.Top + rtb.Height) / ypixel
        .Left = (Me.Left + rtb.Left) / xpixel
        .Right = (Me.Left + rtb.Left + rtb.Width) / xpixel
    End With
    Throw& = ClipCursor&(CrsClp)
End Sub

Private Sub rtb_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Throw& = ClipCursorBynum&(0)
End Sub

Open in new window


btw, rtb is the richtextbox object, replace it with your richtextbox's name
0
 

Author Comment

by:ttobin333
Comment Utility
Thanks, that works partially. 2 issues:

1-The borders of the rectangle are shifted up and slightly to the left of where the textbox is.

2- After a failed external drag/drop or an internal drag/drop, the cursor remains restricted to the textbox until you perform a mouse click. I tried adding the deactivation code to the rtb_OLEDragDrop sub but this doesn't work unless drag drop is set to manual, which then requires additional drag/drop coding. Is there another way to deactivate without the extra click?
0
 
LVL 11

Expert Comment

by:Dany Balian
Comment Utility
1. if it's slightly shifted, then most probably the richtextbox is not directly inside the form; in that case, the proper coordinates need to be calculated..

just modify the code accordingly.. for example if richtextbox is inside a frame called myframe
change the .top, .bottom, .left and .right from:
        .Top = (Me.Top + rtb.Top) / ypixel
        .Bottom = (Me.Top + rtb.Top + rtb.Height) / ypixel
        .Left = (Me.Left + rtb.Left) / xpixel
        .Right = (Me.Left + rtb.Left + rtb.Width) / xpixel

Open in new window

to:
        .Top = (Me.Top + rtb.Top + myframe.top) / ypixel
        .Bottom = (Me.Top + rtb.Top + myframe.top + rtb.Height) / ypixel
        .Left = (Me.Left + rtb.Left + myframe.left) / xpixel
        .Right = (Me.Left + rtb.Left + myframe.left + rtb.Width) / xpixel

Open in new window


2. i don't think there's another way..
0
 

Author Comment

by:ttobin333
Comment Utility
Thanks Dany, I got the rectangle positioned correctly now.

As for deactivating the restriction: since rtb_MouseUp does not trigger after the drag/drop and requires the extra mouse click, I came up with the following and it seems to work:

Private Sub rtb_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If Throw& = 1 Then
    If Button = 0 Then
        Throw& = ClipCursorBynum&(0)
        Throw& = 0
    End If
End If

Open in new window


I used the If Throw& = 1 condition so it wouldn't run constantly with MouseMove. Do you see any possible problems with this?
0
 

Author Comment

by:ttobin333
Comment Utility
I just discovered that the drag restriction using ClipCursor can be bypassed if the user presses alt-tab after initiating the drag…making the drag/drop unrestricted to proceed beyond the rectangle. This was a great solution until this discovery.

Any other ideas?
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
have you tried intercepting the alt+tab?  Assign the key parameter to zero
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
Start a timer that sets focus on your application with the appactivate statement
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
use a variable that indicates that you are in the middle of a drag operation and add code to the form deactivate event that sets focus back to the form (appactivate) whenever the variable indicates dragging.
0
 

Author Closing Comment

by:ttobin333
Comment Utility
Thanks guys!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now