Results 1 to 7 of 7

Thread: HELP me, a silly question. A little problem with my function

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    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 .....

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    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

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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!

  6. #6
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: HELP me, a silly question. A little problem with my function

    Quote 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

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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
  •  



Click Here to Expand Forum to Full Width