PDA

Click to See Complete Forum and Search --> : cut, copy, paste and control array


ianpaisley
Oct 30th, 1999, 05:59 PM
Hi,

I've discovered a snippet of code that will enable me to prevent people cutting, copying and pasting text from my textbox. It works fine with a text box, but I can't figure out how to work it if I have an array of boxes. I keep getting a runtime error 13 - Type mismatch.

I'd be grateful if you could help me out because I've tried lots of ways, and just can't do it.

It involves a module, a form and a textbox (text1). In the module:

Option Explicit

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long


Public Const GWL_WNDPROC = (-4)
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_CUT = &H300
Public Const WM_COPY = &H301
Public Const WM_PASTE = &H302


Public lpPrevWndFunc As Long

Sub Hook(ctlthis As Control)
lpPrevWndFunc = SetWindowLong(ctlthis.hwnd, GWL_WNDPROC, AddressOf WndProc)
End Sub


Sub UnHook(ctlthis As Control)
Call SetWindowLong(ctlthis.hwnd, GWL_WNDPROC, lpPrevWndFunc)
End Sub


Function WndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case Msg
Case WM_RBUTTONDOWN, WM_CUT, WM_COPY, WM_PASTE
WndProc = 0
Case Else
WndProc = CallWindowProc(lpPrevWndFunc, hwnd, Msg, wParam, lParam)
End Select
End Function


And in the form:

Private Sub Form_Load()
Call Hook(Text1)
End Sub

Now it works fine if I just have one Text1. But the way my program will be, I have to have four text1s, a control array.

Whenever I run my program I get that runtime error - type mismatch, and in the form load 'Call Hook(Text1)' turns out in yellow.

I've tried Call Hook(text1(index)) and lots of others, but had no luck.

Thanks for any help!

DiGiTaIErRoR
Oct 30th, 1999, 06:05 PM
try this:
change Hook(text1)
to whatever the name of the other object
hook(text2) etc.

------------------
DiGiTaIErRoR

ianpaisley
Oct 30th, 1999, 08:14 PM
Okay, I've figured out a way:

Private Sub Form_Load()
Call Hook(Txtordanswers(0))
Call Hook(Txtordanswers(1))
Call Hook(Txtordanswers(2))
Call Hook(Txtordanswers(3))
Call Hook(Txtordanswers(4))
End Sub

Is there a better way, though? How can I sum it up in one line, instead of going line by line? I'd really love to know.

Compwiz
Oct 30th, 1999, 08:43 PM
Try this:

Dim txtCounter As Long
For txtCounter = 0 To Txtordanswers.Count - 1
Call Hook(Txtordanswers(txtCounter))
Next


This is untested but if I doesn't work it should at least give you the idea.

------------------
Tom Young, 14 Year Old
tom@e-bizinternet.com
ICQ: 15743470
AIM: TomY10
PERL, JavaScript and VB Programmer

[This message has been edited by Compwiz (edited 10-31-1999).]

Bios
Nov 22nd, 1999, 12:24 PM
I belive the best and most simple way to do this would be:

for a = text1.lbound to text1.ubound
hook(text1(a))
next a



------------------
Bios
Age: 16
VB, C, Perl, Java (Script), HTML, ASP Programer