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

x
?
Solved

vb6 Rich TextBox - prevent text export

Posted on 2014-11-29
33
Medium Priority
?
192 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
[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
  • 14
  • 14
  • 4
  • +1
33 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40472319
Have you tried setting the Enabled property to False?
0
 

Author Comment

by:ttobin333
ID: 40472574
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 46

Expert Comment

by:aikimark
ID: 40472594
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:ttobin333
ID: 40472700
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 46

Expert Comment

by:aikimark
ID: 40472992
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
ID: 40472995
Yes, I am aware of that, but I am looking for a creative solution.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 40473026
You could simulate a copy/paste between controls
0
 

Author Comment

by:ttobin333
ID: 40473038
Would you please elaborate on the simulated copy/paste?
0
 
LVL 46

Assisted Solution

by:aikimark
aikimark earned 800 total points
ID: 40473478
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 1200 total points
ID: 40473487
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 46

Expert Comment

by:aikimark
ID: 40473560
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
ID: 40473595
Yes, that would be smarter! hehe
0
 

Author Comment

by:ttobin333
ID: 40475138
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 46

Expert Comment

by:aikimark
ID: 40475147
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
ID: 40478938
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 46

Expert Comment

by:aikimark
ID: 40479568
can you cancel the drag operation?
0
 

Author Comment

by:ttobin333
ID: 40483670
Aikimark, would you please provide specific information regarding VB6?
0
 
LVL 46

Expert Comment

by:aikimark
ID: 40483849
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
ID: 40488061
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 46

Expert Comment

by:aikimark
ID: 40488064
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
ID: 40489790
That sounds like an intereting option. Can you provide the specifics?
0
 
LVL 46

Assisted Solution

by:aikimark
aikimark earned 800 total points
ID: 40489848
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
ID: 40490256
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 46

Expert Comment

by:aikimark
ID: 40490362
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 1200 total points
ID: 40493311
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
ID: 40495503
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
ID: 40496417
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
ID: 40498568
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
ID: 40499584
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 46

Expert Comment

by:aikimark
ID: 40499682
have you tried intercepting the alt+tab?  Assign the key parameter to zero
0
 
LVL 46

Expert Comment

by:aikimark
ID: 40499686
Start a timer that sets focus on your application with the appactivate statement
0
 
LVL 46

Expert Comment

by:aikimark
ID: 40499690
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
ID: 40511323
Thanks guys!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

688 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