Results 1 to 19 of 19

Thread: Cool Textbox Class

Threaded View

  1. #12
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    Someone brought up the API way to change the numbers only thing (I read all that once and didn't want to read it again, so I apologize for not giving a name), but this is how it's done. It's not as good for all cases though, as this way invalidates everything but numbers (it allows backspace and delete, but not periods, which some people may want for decimals). I took these out of a class I had myself which is somewhat like Nucleus's, but is more for giving a textbox some of the props that exist for edit controls, but aren't exposed with VB.
    VB Code:
    1. 'this takes a reference to a textbox just like Nucleus's class did
    2. 'blnClassBound tells whether or not a textbox reference has been set yet
    3. 'in my clsTextBoxEx.SourceTextBox property set procedure
    4. 'txtSource is the class side textbox var
    5.  
    6. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    7. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    8. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    9.  
    10. Private Const EM_SETREADONLY As Long = &HCF
    11. Private Const ES_NUMBER As Long = &H2000
    12. Private Const ES_READONLY As Long = &H800
    13. Private Const GWL_STYLE As Long = -16
    14.  
    15. Public Property Get NumbersOnly() As Boolean
    16.    Dim lngStyle As Long
    17.    If Not blnClassBound Then Exit Property
    18.    lngStyle = GetWindowLong(txtSource.hWnd, GWL_STYLE)
    19.    NumbersOnly = CBool(lngStyle And ES_NUMBER)
    20. End Property
    21.  
    22. Public Property Let NumbersOnly(ByVal blnNewNumbersOnly As Boolean)
    23.    Dim lngStyle As Long
    24.    If Not blnClassBound Then Exit Property
    25.    lngStyle = GetWindowLong(txtSource.hWnd, GWL_STYLE)
    26.    lngStyle = IIf(blnNewNumbersOnly, lngStyle Or ES_NUMBER, lngStyle And (Not ES_NUMBER))
    27.    SetWindowLong txtSource.hWnd, GWL_STYLE, lngStyle
    28. End Property
    29.  
    30. 'I had this in there before I even knew a textbox had a .Locked property
    31. 'but I'll include it anyway.  someone may find a use for it
    32. Public Property Get ReadOnly() As Boolean
    33.    Dim lngStyle As Long
    34.    If Not blnClassBound Then Exit Property
    35.    lngStyle = GetWindowLong(txtSource.hWnd, GWL_STYLE)
    36.    ReadOnly = CBool(lngStyle And ES_READONLY)
    37. End Property
    38.  
    39. 'SetWindowLong doesn't seem to make a textbox read only if you manually
    40. 'change the ES_READONLY flag for some reason, but SendMessage does get it
    41. Public Property Let ReadOnly(ByVal blnNewReadOnly As Boolean)
    42.    If Not blnClassBound Then Exit Property
    43.    SendMessage txtSource.hWnd, EM_SETREADONLY, blnNewReadOnly, 0
    44. End Property
    I had a .GetLine(Index) prop too, but I remember getting a snag somewhere and ditched it. In that case, it was before I even knew of Split() (those were bad times indeed ), but I had an API way. I'm going to work on that again.

    I remembered a while later I forgot one of the useful ones I intended to show in the first place...
    VB Code:
    1. Private Const EM_GETMODIFY As Long = &HB8
    2. Private Const EM_SETMODIFY As Long = &HB9
    3.  
    4. Public Property Get Edited() As Boolean
    5.    If Not blnClassBound Then Exit Property
    6.    Edited = SendMessage(txtSource.hWnd, EM_GETMODIFY, 0, 0)
    7. End Property
    8.  
    9. Public Property Let Edited(ByVal blnNewEdited As Boolean)
    10.    If Not blnClassBound Then Exit Property
    11.    SendMessage txtSource.hWnd, EM_SETMODIFY, blnNewEdited, 0
    12. End Property
    13.  
    14. Private Sub txtSource_Change()
    15.    'this makes sure the edited flag is set, as I noticed there were times
    16.    'when changing text wouldn't make it apparent to the class for some reason
    17.    Me.Edited = True
    18. End Sub
    Last edited by Kaverin; Aug 21st, 2001 at 11:00 PM.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

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