|
-
Jan 27th, 2009, 05:02 PM
#1
Thread Starter
Hyperactive Member
HELP me, a silly question. A little problem with my function
Hi All
I wrote the function:
Code:
Private Function Filtruj(Index As Integer, KeyAscii As Integer, MyText As TextBox)
MyText(Index).MaxLength = 5
If KeyAscii = 13 Then
SendKeys "{TAB}"
KeyAscii = 0
ElseIf KeyAscii <> 8 Then
If InStr(1, MyText(Index), ",") And KeyAscii = 44 Then KeyAscii = 0
If Not IsNumeric(Chr$(KeyAscii)) And KeyAscii <> 44 Then
MsgBox "TYLKO liczby - separatorem ułamka jest znak PRZECINKA", vbCritical + vbOKOnly, "UWAGA !"
KeyAscii = 0
End If
End If
If Mid$(MyText(Index), 1, 1) = "," Then
MyText(Index).Text = "0" & MyText(Index).Text
MyText(Index).SelStart = Len(MyText(Index))
End If
End Function
(hmmm- first question, this function, it's a correctly?)
Now, I want to use it for an all objects of TextBox in my app. So, how I could to do it? Any suggestions.
Thanks in advance
EDIT:
I have this textboxes in the array of controls.
I know, I know, my English is bad, sorry .....
-
Jan 27th, 2009, 05:11 PM
#2
Re: HELP me, a silly question. A little problem with my function
It's close. Here are some things you might want to look at
1. Functions return values. If your function does not need to return a value, recommend renaming it to Sub vs Function.
2. Don't pass the Index, it is unnecessary if you are also passing the Textbox control itself. So your call might look like: Filtruj KeyAscii, Text1(0)
:: Your function is asking for a textbox, the MyText is simply a pointer/reference to whichever textbox you pass. It does not need an index within your code. Whether you pass an indexed textbox or one that is not indexed, MyText will correctly reference the passed control.
3. Since you will not need the Index, replace all MyText(Index) with just MyText
Oh, and where do you call it.
Call it from within your KeyPress event, renaming Text1 to whatever your actual textbox control is named... Filtruj KeyAscii, Text1(Index)
Last edited by LaVolpe; Jan 27th, 2009 at 05:16 PM.
-
Jan 27th, 2009, 05:54 PM
#3
Re: HELP me, a silly question. A little problem with my function
Besides what LaVolpe suggested if you want the changes to the text box then
pass by reference
ByRef MyText As TextBox
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Jan 27th, 2009, 05:59 PM
#4
Re: HELP me, a silly question. A little problem with my function
isnoend07...
Two points, just for clarification
1. If ByVal is not provided with the function/sub/property parameter, ByRef is assumed.
2. When passing objects, all objects (no exceptions that I am aware of) are passed ByRef even if ByVal is used. Try this as an experiment and you will see that the passed textbox properties are changed even though it is passed ByVal.
Code:
Private Sub Command1_Click()
ChangeByvalTextBox Text1, "VB Forums"
End Sub
Private Sub ChangeByvalTextBox(ByVal TBox As TextBox, ByRef newText As String)
TBox.Text = newText
End Sub
-
Jan 27th, 2009, 06:01 PM
#5
Re: HELP me, a silly question. A little problem with my function
As TextBox is an object based data type, it is always ByRef anyway - even if you specify ByVal (which can cause confusion!).
In addition to that, in VB6 all parameters are ByRef unless you explicity specify ByVal.
However, I (and many others) prefer to always specify whichever was intended.
edit: I was a bit slow!
-
Jan 27th, 2009, 06:06 PM
#6
Re: HELP me, a silly question. A little problem with my function
 Originally Posted by LaVolpe
isnoend07...
Two points, just for clarification
1. If ByVal is not provided with the function/sub/property parameter, ByRef is assumed.
2. When passing objects, all objects (no exceptions that I am aware of) are passed ByRef even if ByVal is used. Try this as an experiment and you will see that the passed textbox properties are changed even though it is passed ByVal.
Code:
Private Sub Command1_Click()
ChangeByvalTextBox Text1, "VB Forums"
End Sub
Private Sub ChangeByvalTextBox(ByVal TBox As TextBox, ByRef newText As String)
TBox.Text = newText
End Sub
Thanks I have been thinking that Byval was assumed
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Jan 27th, 2009, 06:08 PM
#7
Re: HELP me, a silly question. A little problem with my function
I think that was the case in VB5 (apart from Object data types, which haven't changed).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|