Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to strict copy paste on Textbox in vb6??

Posted on 2006-05-08
18
Medium Priority
?
3,327 Views
Last Modified: 2010-07-27
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.
0
Comment
Question by:SwamyN
  • 7
  • 4
  • 2
  • +2
15 Comments
 
LVL 23

Expert Comment

by:basicinstinct
ID: 16628665
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
 
LVL 14

Expert Comment

by:MilanKM
ID: 16628775
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
 
LVL 2

Expert Comment

by:everlaast
ID: 16628970
use masked text edit control, then you can set exactly what can be inserted in the textbox
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Accepted Solution

by:
nayernaguib earned 1000 total points
ID: 16629475
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
 
LVL 14

Expert Comment

by:nayernaguib
ID: 16629488
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
 

Author Comment

by:SwamyN
ID: 16629682
We can take care on Validate Event But instead of this if any other Property or Method Disable copy Paste on Particular Textbox.
0
 
LVL 14

Expert Comment

by:nayernaguib
ID: 16630043
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
 
LVL 14

Assisted Solution

by:MilanKM
MilanKM earned 1000 total points
ID: 16630300
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
 
LVL 14

Expert Comment

by:nayernaguib
ID: 16630423
MilanKM: You code will *indeed* disable the textbox context menu, but will definitely *not* disable Ctrl+V. :-)

_______________

  Nayer Naguib
0
 
LVL 14

Expert Comment

by:MilanKM
ID: 16633372
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
 
LVL 14

Expert Comment

by:MilanKM
ID: 16633672
Ok, Swami, now I think it will work for u for a single/ perticular TextBox. So, waiting for ur reply.

Thanks
MilanKM
0
 

Author Comment

by:SwamyN
ID: 16646361
ok MilanKm
but when we rightclick on textbox then that condition Paste is allow.
how can i manage it.
thanks
swamy
0
 
LVL 14

Expert Comment

by:MilanKM
ID: 16646432
>> 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
 
LVL 14

Expert Comment

by:MilanKM
ID: 16646461
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
 
LVL 14

Expert Comment

by:MilanKM
ID: 16816915
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

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Loops Section Overview

810 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