how to strict copy paste on Textbox in vb6??

hi, how to strict copy paste on Textbox in vb6?? bcz sometime i strict user to enter only aplhabets but using copy paste he can insert numeric value.
SwamyNAsked:
Who is Participating?
 
nayernaguibConnect With a Mentor Commented:
MilanKM: I disagree! First, having the code execute 1000 times per second is *very* inefficient, and would most probably freeze the application!!
              Second, this will disable the clipboard as long as the timer is enabled. You will not be able to copy and paste *anything*, even in other applications!

Here are two possible solutions to your problem:

1. Add the following code to the Validate event handler of the textbox. This will keep the focus on the textbox until the user enters valid text:

Dim tempChar As Integer
Dim flag As Boolean
flag = False
For i = 1 To Len(Text1)
  tempChar = Asc(Mid(Text1, i, 1))
  If tempChar < 65 Or (tempChar > 90 And tempChar < 97) Or tempChar > 122 Then
    flag = True
  End If
Next i
If flag Then
  MsgBox "Only alphabetical characters are allowed"
  Cancel = True
End If

2. Set the Locked property of the textbox to False. Now you can handle the KeyPress event of the textbox to check for pressed keys, and filter any unwanted characters. The problem with this method is that it needs complicated processing to work correctly. Check this out:

a. In order to check for the typed character, append it to the original textbox contents, and advance the cursor, you need to write the following in the KeyPress event handler of the textbox (the code also handles the backspace character):

If (KeyAscii >= 65 And KeyAscii <= 90) Or (KeyAscii >= 97 And KeyAscii <= 122) Then
  Text1 = Text1 & Chr(KeyAscii)
  Text1.SelStart = Len(Text1)
ElseIf KeyAscii = 8 And Len(Text1) > 0 Then 'backspace
  Text1 = Left(Text1, Len(Text1) - 1)
  Text1.SelStart = Len(Text1)
End If

b. Now there's another problem. We don't want the user to change the cursor location, simply because the above code appends newly typed characters to the end of the textbox contents. So we need to handle the MouseUp event as follows:

Text1.SelStart = Len(Text1)

and the KeyDown event as follows:

If KeyCode < 65 Or KeyCode > 90 Then
  KeyCode = 0
End If

The above lines will disable the cursor keys.

_______________

  Nayer Naguib
0
 
basicinstinctCommented:
How about using the 'change' event to check the contents of the textbox?  That way, if the user manages to insert an illegal character in any way you can strip it out...
0
 
MilanKMCommented:
Simply take a Timer control Set Timer1.Enable= True & Interval=1 Then put the following code

Private Sub Timer1_Timer()
   Clipboard.Clear
End Sub

Hope this helps
MilanKM
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
everlaastCommented:
use masked text edit control, then you can set exactly what can be inserted in the textbox
0
 
nayernaguibCommented:
Sorry! You need to set the Locked property to True, not False!
Setting the Locked property of the textbox to True also prevents pasting text to the textbox.

_______________

  Nayer Naguib
0
 
SwamyNAuthor Commented:
We can take care on Validate Event But instead of this if any other Property or Method Disable copy Paste on Particular Textbox.
0
 
nayernaguibCommented:
As I mentioned before, setting the Locked property to True will prevent pasting text to the textbox. In this case, you will need to programmatically modify textbox contents and cursor location (see my first post). Note that setting the Locked property to True will not disable the textbox (as in the case of setting the Enabled property to False); it only prevents editing the text directly by the user, but you can still handle keyboard and mouse events and modify textbox contents programmatically.

_______________

  Nayer Naguib
0
 
MilanKMConnect With a Mentor Commented:
Yes, nayer, I didn't take care about the execution of that code. I just provide a quick solution. Herewith something different. The code follows will disable the pop-up menu of textbox while right clicking.

'Take a module & paste the code follows
'----------------------------------------------
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

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

Public Const GWL_WNDPROC = -4
Public Const WM_RBUTTONUP = &H205
Public lpPrevWndProc As Long
Public lngHWnd As Long

Public Sub Hook(hWnd As Long)
lngHWnd = hWnd
lpPrevWndProc = SetWindowLong(lngHWnd, GWL_WNDPROC, _
AddressOf WindowProc)
End Sub

Public Sub UnHook()
Dim lngReturnValue As Long
lngReturnValue = SetWindowLong(lngHWnd, GWL_WNDPROC, lpPrevWndProc)
End Sub

Function WindowProc(ByVal hw As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Select Case uMsg

Case WM_RBUTTONUP
Case Else
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
End Select
End Function

'Paste the following code on FORM
'---------------------------------------
Private Sub Form_Load()
    Call Hook(Text1.hWnd) ' Assuming Text1 is the textbox, change it
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Call UnHook
End Sub

Hope this helps
MilanKM
0
 
nayernaguibCommented:
MilanKM: You code will *indeed* disable the textbox context menu, but will definitely *not* disable Ctrl+V. :-)

_______________

  Nayer Naguib
0
 
MilanKMCommented:
Again I missed one thing, thanks nayer to knock me.

Ok, to disable "Ctrl+V" ___  put the following code in the KeyPress event, The Ascii value of Ctrl+V is 22.

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 22 Then
       MsgBox "Not Alpha", vbCritical
       KeyAscii = 0
    End If
End Sub

Thanks
MilanKM
0
 
MilanKMCommented:
Ok, Swami, now I think it will work for u for a single/ perticular TextBox. So, waiting for ur reply.

Thanks
MilanKM
0
 
SwamyNAuthor Commented:
ok MilanKm
but when we rightclick on textbox then that condition Paste is allow.
how can i manage it.
thanks
swamy
0
 
MilanKMCommented:
>> but when we rightclick on textbox then that condition Paste is allow

Is not code not working..? See the following section

Private Sub Form_Load()
    Call Hook(Text1.hWnd) ' Assuming Text1 is the textbox, change it
End Sub

Here put the correct textbox name. I think u can understand what I mean.
If there any other problem then pls explain

Thanks
MilanKM
0
 
MilanKMCommented:
If U need to disable more than one textbox then do as follows

Call Hook(Text1.hWnd)
Call Hook(Text2.hWnd)
Call Hook(Text2.hWnd)

Also don't forget to put the keypress Event code to disable "ctrl+v"

Thanks
MilanKM
0
 
MilanKMCommented:
Hi Venabili,

I think, after my last two comments, it's a solution. But didn't get any reply from the asker after waiting for long.

Thanks
MilanKM
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.