Results 1 to 23 of 23

Thread: create new property for text box

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    19

    create new property for text box

    Hi, I have a simple text box, plus (tested and working) code that restricts only numbers, one decimal point and an initial negative sign if used.

    What I want now is to set limits to the scope of the final number entered, to be put in the change() part of the text box. I would like to do this by writing a couple of properties (lets call them maxvalue and minvalue) and adding them to the text box, so they then appear in the normal properties window for the text box.

    Example: lets say for this particular textbox I want the maximum number entered to be 17 and the minimum to be -5 and set those values in the new-improved properties window for the text box. The propertyget will catch the error if I enter (say) 18 or -6.

    I know this can be done by creating a dedicated control but that's a bit overkill for what I want here!
    And obviously I know I can just trap the above errors using a procedure in the change event but it would be quite nifty to be able to create these two additional properties for an existing 'normal' text box so I can take care of things at design time.
    Thanks for any help that may be forthcoming.

  2. #2
    Addicted Member
    Join Date
    Jul 2021
    Posts
    201

    Re: create new property for text box

    You can use the Tag property as a "general purpose additional data".
    Another tip (from experience...): When you convert the text value to a number, use error trapping and check for overflow!

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,212

    Re: create new property for text box

    VB6 standard controls, like a Textbox, do not support custom properties. ChatGPT also suggested the use of the Tag Property....but not sure how you might create both a 'maxvalue' and 'minvalue' using Tag.
    Sam I am (as well as Confused at times).

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,186

    Re: create new property for text box

    It sounds to me like it all needs to be stuffed into a custom User Control module. That's precisely what they're for, creating a control that appears in our ToolBox that's based on other controls but with additional features.

    They can, optionally, even be compiled into OCX files if we'd like, or alternatively just pull the CTL source code into our existing project (either way).

    These custom User Controls can have all the properties, events, and methods your heart desires. I even have instances where I've subclassed within them, but that's not at all necessary for what it sounds like you're trying to do.

    ------------------

    As just another note, this creation of custom UCs is actually a more true form of "subclassing" than what we've come to call "subclassing" in VB6 circles. We (in VB6 circles) tend to call it the "insertion of a callback in the message pump of a particular hWnd". However, the rest of the world calls it "the subordinating (i.e., sublassing) of some class to a "super-class" to give that super-class increased functionality. Given that our existing intrinsic controls are clearly classes, the creation of a custom UC is clearly a "form of subclassing".

    However, I confuse our VB6 nomenclature by bringing this up, so just forget I said it.
    Last edited by Elroy; Apr 12th, 2024 at 09:00 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,537

    Cool Re: create new property for text box

    You use a Long value for the Tag, the first 16 bits are the "minvalue" and the next 16 bits are the "maxvalue".

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    19

    Re: create new property for text box

    Hi, this sounds like what I want. I just have no idea as to actually write it!
    OK, so it's a variable of type long (that I understand). So far so OK, but I thought that variables could only hold one value at a time. I have never had any overt dealing with bits, so how do I get both the minimum value and maximum value into that one tag variable?
    Failing that, I can just put the check routine into each box's lost focus event.

  7. #7
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,537

    Talking Re: create new property for text box

    Bit arithmetic is related to powers of two.

    Code:
    LongValue = MinValue + MaxValue * 65536
    conversely:

    Code:
    MinValue = LongValue And &HFFFF&
    MaxValue = LongValue \ 65536

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,186

    Re: create new property for text box

    If you're sticking two values into the Tag property, I don't understand why you wouldn't just stick them both in there, as it's a string regardless of how you handle it.

    Code:
    Option Explicit
    
    Private Sub Form_Load()
    
        Dim Min As Long
        Dim Max As Long
        Min = 123
        Max = 456
        
        Text1.Tag = CStr(Min) & "|" & CStr(Max)
        
        Min = 0
        Max = 0
        
        Min = CLng(Split(Text1.Tag, "|")(0))
        Max = CLng(Split(Text1.Tag, "|")(1))
        
        Debug.Print Min, Max ' Shows original 123 and 456 values.
        
    
    End Sub
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,537

    Cool Re: create new property for text box

    Code:
    Min = Val(Text1.Tag)
    Max = Mid$(Text1.Tag, Instr(Text1.Tag, "|") + 1)
    But I'd rather use two Integers packed in a Long any day...

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,212

    Re: create new property for text box

    Now I DO see how (but not sure how you might create both a 'maxvalue' and 'minvalue' using Tag.)

    Thx to both Elroy and Van---But, I do like Elroy's approach better (was not sure WHAT type 'Tag' was; but I SHOULD have...oh well)
    Sam I am (as well as Confused at times).

  11. #11
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,186

    Re: create new property for text box

    Quote Originally Posted by SamOscarBrown View Post
    Thx to both Elroy and Van---But, I do like Elroy's approach better (was not sure WHAT type 'Tag' was; but I SHOULD have...oh well)
    It would have been nice if some of these things were a Variant, but such is not the case.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  12. #12
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,119

    Re: create new property for text box

    Quote Originally Posted by Elroy View Post
    It sounds to me like it all needs to be stuffed into a custom User Control module. That's precisely what they're for, creating a control that appears in our ToolBox that's based on other controls but with additional features.
    To do that more easily, he can use this control interface replication and encapsulation helper.

    I already did it. Attached is the file.
    Now it is necessary to add the new functionality (new properties).
    Attached Files Attached Files

  13. #13
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,186

    Re: create new property for text box

    Quote Originally Posted by Eduardo- View Post
    To do that more easily, he can use this control interface replication and encapsulation helper.
    IDK, I've created so many custom UCs that it comes pretty easy to me these days. It seems that, for someone who wants to be a serious VB6 programmer, it's worth learning how to do this.

    And personally, I've never used any of the wizards. I just start with a new-bank UC and start adding controls and my code.

    Basically, it's just raising the events we need, and saving and fetching any properties we wish to save. I suppose we might also have some custom methods, but that's at the developer's discretion.

    Also, I typically don't raise every single event of the sub-controls on the UC, just raising the ones I know I'll use. That simplifies things substantially.

    Maybe I'll work on a short tutorial on how to get these things going, and post it in the CodeBank. Seems like it might be useful.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  14. #14
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,119

    Re: create new property for text box

    Quote Originally Posted by Elroy View Post
    IDK, I've created so many custom UCs that it comes pretty easy to me these days. It seems that, for someone who wants to be a serious VB6 programmer, it's worth learning how to do this.

    And personally, I've never used any of the wizards. I just start with a new-bank UC and start adding controls and my code.

    Basically, it's just raising the events we need, and saving and fetching any properties we wish to save. I suppose we might also have some custom methods, but that's at the developer's discretion.

    Also, I typically don't raise every single event of the sub-controls on the UC, just raising the ones I know I'll use. That simplifies things substantially.

    Maybe I'll work on a short tutorial on how to get these things going, and post it in the CodeBank. Seems like it might be useful.
    I don't think he wants to learn a lot about making controls. I think he is just looking for something easy and quick to get what he needs now (a TextBox control with a few more properties).

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    19

    Re: create new property for text box

    Exactly, Eduardo. That's why I thought an entirely new control was overkill for this particular issue.

  16. #16
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,119

    Re: create new property for text box

    Quote Originally Posted by starman1500 View Post
    Exactly, Eduardo. That's why I thought an entirely new control was overkill for this particular issue.
    Helping is getting difficult lately (at least here in this forum). people don't read messages.

  17. #17
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,186

    Re: create new property for text box

    Quote Originally Posted by Eduardo- View Post
    ... a TextBox control with a few more properties.
    I'll bow out after this, but, to my eyes, that's exactly what a custom UC could provide with a fairly minimal amount of work. And, we wouldn't need to play any tricks with converting Longs to Strings (and vice-versa) to do it. It's just part of the machinery of a UC (creating properties of any "variable type" we want, and preserving/storing those types). I'm unclear on exactly he wants, but I'm now motivated to just quickly knock something out.

    Here's what I knocked out. I believe this meets most of his specifications.

    To use it, start a new UC, place a single TextBox on it named "txt". I named this new UC "TextNum". Then paste the following code into this new UC's code:

    Code:
    
    Option Explicit
    '
    Event Change()
    '
    Private mdMin As Double
    Private mdMax As Double
    '
    
    
    Private Sub txt_Change()
        RaiseEvent Change
    End Sub
    
    
    Private Sub txt_KeyPress(KeyAscii As Integer)
        '
        ' Make sure any - is first (and only) character.
        If KeyAscii = 45 Then ' A dash.
            If txt.SelStart <> 0& Or InStr(txt.Text, "-") <> 0& Then KeyAscii = 0
            Exit Sub
        End If
        '
        ' Make sure there's only one decimal point.
        If KeyAscii = 46 Then ' A period.
            If InStr(txt.Text, ".") <> 0& Then KeyAscii = 0
            Exit Sub
        End If
        '
        ' From here down, make sure it's a number.
        If KeyAscii < 48 Or KeyAscii > 57 Then
            KeyAscii = 0
            Exit Sub
        End If
        '
        ' Check our min and max.  If out of range, disallow.
        Dim d As Double
        d = Val(Left$(txt.Text, txt.SelStart) & Chr$(KeyAscii) & Mid$(txt.Text, txt.SelStart + txt.SelLength + 1&))
        If d < mdMin Or d > mdMax Then
            KeyAscii = 0
            Exit Sub
        End If
        '
        ' If we fell through, it's a good character.
    End Sub
    
    
    
    Public Property Get Min() As Double
        Min = mdMin
    End Property
    Public Property Let Min(dNewMin As Double)
        mdMin = dNewMin
        PropertyChanged
    End Property
    
    Public Property Get Max() As Double
        Max = mdMax
    End Property
    Public Property Let Max(ByVal dNewMax As Double)
        mdMax = dNewMax
        PropertyChanged
    End Property
    
    Public Property Get Text() As String
        Text = txt.Text
    End Property
    Public Property Let Text(ByVal sNewText As String)
        txt.Text = sNewText
        PropertyChanged
    End Property
    
    Public Property Get Font() As StdFont
        Set Font = txt.Font
    End Property
    Public Property Set Font(ByVal NewFont As StdFont)
        Set txt.Font = NewFont
        PropertyChanged
    End Property
    
    Public Property Get ForeColor() As OLE_COLOR
        ForeColor = txt.ForeColor
    End Property
    Public Property Let ForeColor(NewColor As OLE_COLOR)
        txt.ForeColor = NewColor
        PropertyChanged
    End Property
    
    Public Property Get BackColor() As OLE_COLOR
        BackColor = txt.BackColor
    End Property
    Public Property Let BackColor(NewColor As OLE_COLOR)
        txt.BackColor = NewColor
        PropertyChanged
    End Property
    
    
    
    Private Sub UserControl_InitProperties()
        mdMin = -1.7976931348623E+308
        mdMax = 1.7976931348623E+308
        txt.ForeColor = &H80000008
        txt.BackColor = &H80000005
    End Sub
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        mdMin = PropBag.ReadProperty("Min", -1.7976931348623E+308)
        mdMax = PropBag.ReadProperty("Max", 1.7976931348623E+308)
        txt.Text = PropBag.ReadProperty("Text", vbNullString)
        Set txt.Font = PropBag.ReadProperty("Font", txt.Parent.Font)
        txt.ForeColor = PropBag.ReadProperty("ForeColor", &H80000008)
        txt.BackColor = PropBag.ReadProperty("BackColor", &H80000005)
    End Sub
    
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        PropBag.WriteProperty "Min", mdMin
        PropBag.WriteProperty "Max", mdMax
        PropBag.WriteProperty "Text", txt.Text
        PropBag.WriteProperty "Font", txt.Font
        PropBag.WriteProperty "ForeColor", txt.ForeColor
        PropBag.WriteProperty "BackColor", txt.BackColor
    End Sub
    
    
    
    Private Sub UserControl_Resize()
        txt.Left = 0
        txt.Top = 0
        txt.Width = UserControl.Width
        txt.Height = UserControl.Height
    End Sub
    
    
    Using the IDE's menu, you could also set that "Text" property to be the default, the way it is for a standard TextBox.

    Once that's done, you can then use this new UC as a control on your forms. And, when you call up the Properties Window for it, it'll have your requested Min and Max properties (without any stuffing into the Tag property). In fact, you can still use the Tag property of this UC for other purposes.

    Name:  MinMax.png
Views: 65
Size:  43.6 KB
    Last edited by Elroy; Apr 13th, 2024 at 11:45 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  18. #18
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,119

    Re: create new property for text box

    Hello. I think that your code does not cover the validation for what is pasted into the textbox.

    I hope you are still motivated

  19. #19
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,186

    Re: create new property for text box

    Quote Originally Posted by Eduardo- View Post
    Hello. I think that your code does not cover the validation for what is pasted into the textbox.

    I hope you are still motivated
    hahaha, nahhh, it should be fairly straightforward for starman to further tweak that txt_KeyPress event if he wants it to work differently.

    I'm going to make a CodeBank post now, using The Trick's work, that shows how to fairly easily create standard DLLs with VB6. Then, someday, I need to get back onto my binary tree project.

    ----------------

    Ahhh, yeah, I actually did think about copy-pasting. But that validation actually isn't what was requested in the OP. Title: create new property for text box. That's where my head has been at when reading the posts of this thread. That's why I included the Property Window image.
    Last edited by Elroy; Apr 13th, 2024 at 12:13 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  20. #20
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,119

    Re: create new property for text box

    OK, you are no longer motivated

    Quote Originally Posted by Elroy View Post
    to further tweak that txt_KeyPress event
    For pasting? No. (and the user can use the mouse to paste).

  21. #21
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,186

    Re: create new property for text box

    Quote Originally Posted by Eduardo- View Post
    For pasting? No. (and the user can use the mouse to paste).
    Yeah, you're correct. It would probably take work in the txt_Change event (and possibly txt_KeyDown). The easiest way might be to just always save the last txt.Text value in a module String, check that the new txt.Text is valid and, if not, put back the last txt.Text value. That wouldn't take much at all. Actually, using that approach, all that txt_KeyPress stuff could be deleted.

    But again, not my project. I was just really trying to show that UCs aren't really that difficult once the key concepts of them are learned. (Creating events, exposing whatever properties you want [maybe creating new ones], saving the properties in the PropertyBag so they get saved to the FRM file, resizing our sub-controls when the actual UC is resized).
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  22. #22
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,186

    Re: create new property for text box

    Ok, here's new UC that uses the txt_Change event for validating. That should catch everything.

    Code:
    
    Option Explicit '
    '
    Event Change()
    '
    Private mdMin As Double
    Private mdMax As Double
    Private msPriorText As String
    '
    
    
    Private Sub txt_Change()
        Static bRecursing As Boolean
        If bRecursing Then Exit Sub
        '
        ' Do validations.
        '
        ' No dashes after first character.
        If InStr(2&, txt.Text) <> 0 Then GoTo Reset
        '
        ' At most, one period.
        If UBound(Split(txt.Text, ".")) > 1& Then GoTo Reset
        '
        ' Only numbers, dash, and period.
        Dim i As Long
        Const ALLOWED As String = "0123456789.-"
        For i = 1& To Len(txt.Text)
            If InStr(ALLOWED, Mid$(txt.Text, i, 1&)) = 0& Then GoTo Reset
        Next
        '
        ' Check Min/Max range.
        Dim d As Double
        d = Val(txt.Text)
        If d < mdMin Or d > mdMax Then GoTo Reset
        '
        ' If we got to here, all is copacetic.
        msPriorText = txt.Text
        '
        ' Only raise the event if we've made a valid change.
        RaiseEvent Change
        Exit Sub
        '
    Reset:
        bRecursing = True
        txt.Text = msPriorText
        bRecursing = False
    End Sub
    
    
    
    Public Property Get Min() As Double
        Min = mdMin
    End Property
    Public Property Let Min(dNewMin As Double)
        mdMin = dNewMin
        PropertyChanged
    End Property
    
    Public Property Get Max() As Double
        Max = mdMax
    End Property
    Public Property Let Max(ByVal dNewMax As Double)
        mdMax = dNewMax
        PropertyChanged
    End Property
    
    Public Property Get Text() As String
        Text = txt.Text
    End Property
    Public Property Let Text(ByVal sNewText As String)
        txt.Text = sNewText
        PropertyChanged
    End Property
    
    Public Property Get Font() As StdFont
        Set Font = txt.Font
    End Property
    Public Property Set Font(ByVal NewFont As StdFont)
        Set txt.Font = NewFont
        PropertyChanged
    End Property
    
    Public Property Get ForeColor() As OLE_COLOR
        ForeColor = txt.ForeColor
    End Property
    Public Property Let ForeColor(NewColor As OLE_COLOR)
        txt.ForeColor = NewColor
        PropertyChanged
    End Property
    
    Public Property Get BackColor() As OLE_COLOR
        BackColor = txt.BackColor
    End Property
    Public Property Let BackColor(NewColor As OLE_COLOR)
        txt.BackColor = NewColor
        PropertyChanged
    End Property
    
    
    
    Private Sub UserControl_InitProperties()
        mdMin = -1.7976931348623E+308
        mdMax = 1.7976931348623E+308
        txt.ForeColor = &H80000008
        txt.BackColor = &H80000005
    End Sub
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        mdMin = PropBag.ReadProperty("Min", -1.7976931348623E+308)
        mdMax = PropBag.ReadProperty("Max", 1.7976931348623E+308)
        txt.Text = PropBag.ReadProperty("Text", vbNullString)
        Set txt.Font = PropBag.ReadProperty("Font", txt.Parent.Font)
        txt.ForeColor = PropBag.ReadProperty("ForeColor", &H80000008)
        txt.BackColor = PropBag.ReadProperty("BackColor", &H80000005)
        '
        msPriorText = txt.Text
    End Sub
    
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        PropBag.WriteProperty "Min", mdMin
        PropBag.WriteProperty "Max", mdMax
        PropBag.WriteProperty "Text", txt.Text
        PropBag.WriteProperty "Font", txt.Font
        PropBag.WriteProperty "ForeColor", txt.ForeColor
        PropBag.WriteProperty "BackColor", txt.BackColor
    End Sub
    
    
    
    Private Sub UserControl_Resize()
        txt.Left = 0
        txt.Top = 0
        txt.Width = UserControl.Width
        txt.Height = UserControl.Height
    End Sub
    
    
    I suppose there could still be a bit of tweaking to restore SelStart and SelLength when validation fails, but hey ho.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    19

    Re: create new property for text box

    Many thanks for that. It will also be a useful exercise to go through it and see how a person with brains does it! ;-)
    I can adapt a few of the values like max and min (max will probably be about 18 and min -4, in case of a supernova)... the app is for recording star magnitudes. Basically the smaller the number the brighter the star.
    Also thanks everyone who made suggestions. Good stuff.

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