Results 1 to 12 of 12

Thread: [RESOLVED] prevent typing html

  1. #1

    Thread Starter
    Banned
    Join Date
    Aug 2022
    Posts
    97

    Resolved [RESOLVED] prevent typing html

    richtextbox we enter our strings.
    prevent typing html tags into richtextbox but allow the pre-program button to add it.

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,854

    Re: prevent typing html

    Have a look at the KeyPress event and disallow for example the input of "<" and ">"

  3. #3

    Thread Starter
    Banned
    Join Date
    Aug 2022
    Posts
    97

    Re: prevent typing html

    Quote Originally Posted by Arnoutdv View Post
    Have a look at the KeyPress event and disallow for example the input of "<" and ">"
    but keypress is used by pressing the enter button. there is also a send button so how will i assign the code to the button side
    world be better to add code in change state ?

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,854

    Re: prevent typing html

    I don't understand what you are saying.
    In the KeyPress event of the RichTextBox you have to prevent the typing of "<" and ">", not in the KeyPress event of the other controls.

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,382

    Re: prevent typing html

    Quote Originally Posted by Arnoutdv View Post
    I don't understand what you are saying.
    In the KeyPress event of the RichTextBox you have to prevent the typing of "<" and ">", not in the KeyPress event of the other controls.
    Haven't understood OP's answer, too.

    BUT: Arnout's idea is "dangerous". What if the user wants to enter something like "... If var<6 Then..."? Arnout's idea would prevent it.

    Bottom line from me: let user enter whatever he wants, and when he tries to "send" it (save, hit OK, whatever), parse the content and remove any html-tags
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6

    Thread Starter
    Banned
    Join Date
    Aug 2022
    Posts
    97

    Re: prevent typing html

    "send" it (save, hit OK, whatever), parse the content and remove any html-tags
    good thought



    Code:
    Const SpecialCharacters As String = "!,@,#,$,%,^,&,*,(,),{,[,],},?,<,>,|,;,:,/,\,*"  'modify as needed
    Code:
    Private Sub txtNick_Change()
    Dim myString As String
    Dim newString As String
    Dim char As Variant
    myString = txtNick
    newString = myString
    For Each char In Split(SpecialCharacters, ",")
        txtNick = Replace(txtNick, char, "")
    Next
    txtNick.SelStart = Len(txtNick.Text)
    End Sub
    this works good.

    now as we type this code above fires and works.
    but i have html code in a button when i press it,than it adds code to the txtNick so how do i bypass the code and allow the button code to work

    maybe i can use boolean 0 and 1 cheap way

    Code:
    Private Sub txtSend_Change()
    If htmll = True Then
    Dim myString As String
    Dim newString As String
    Dim char As Variant
    myString = txtNick
    newString = myString
    For Each char In Split(SpecialCharacters, ",")
        txtSend = Replace(txtSend, char, "")
    Next
    htmll = False
    End If
    Last edited by tuffan; Sep 13th, 2022 at 05:33 AM.

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,382

    Re: prevent typing html

    you could use a global/module-level Boolean "FromButton" which you set to True in the Button
    Code:
    Private FromButton As Boolean
    
    
    Private Sub Button_Click()
       FromButton = True
       SendHTMLToRichTextBox
       FromButton = False
    End Sub
    
    Private Sub txtNick_Change()
    Dim myString As String
    Dim newString As String
    Dim char As Variant
      If Not FromButton Then
        myString = txtNick
        newString = myString
        For Each char In Split(SpecialCharacters, ",")
            txtNick = Replace(txtNick, char, "")
        Next
        txtNick.SelStart = Len(txtNick.Text)
      End If
    End Sub
    btw: I wouldn't split SpecialCharacters with each "change"-Event. Do it once and be done with it.
    And Replacing in a loop.... i think i remember writing something like a "ReplaceAny"-Function, but for the life of me i can't remember where it is
    The only thing i remember was using the StrSpn/StrCSpn-API's
    Last edited by Zvoni; Sep 13th, 2022 at 05:39 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8

    Thread Starter
    Banned
    Join Date
    Aug 2022
    Posts
    97

    Re: prevent typing html

    am running the same code but on a heavy form.
    heavy form that has max controls limit reached.


    Code:
    Private Sub txtSend_Change()
    Dim myString As String
    Dim newString As String
    Dim char As Variant
    myString = txtSend
    newString = myString
    For Each char In Split(SpecialCharacters, ",")
        txtSend = Replace(txtSend, char, "")
    Next
    
    
    txtSend.SelStart = Len(txtSend.Text)

    the entire vb6 source code project closes instant.

    but when i used the same code on another form light wight it was ok

  9. #9

    Thread Starter
    Banned
    Join Date
    Aug 2022
    Posts
    97

    Re: prevent typing html

    Quote Originally Posted by Zvoni View Post
    you could use a global/module-level Boolean "FromButton" which you set to True in the Button
    Code:
    Private FromButton As Boolean
    
    
    Private Sub Button_Click()
       FromButton = True
       SendHTMLToRichTextBox
       FromButton = False
    End Sub
    
    Private Sub txtNick_Change()
    Dim myString As String
    Dim newString As String
    Dim char As Variant
      If Not FromButton Then
        myString = txtNick
        newString = myString
        For Each char In Split(SpecialCharacters, ",")
            txtNick = Replace(txtNick, char, "")
        Next
        txtNick.SelStart = Len(txtNick.Text)
      End If
    End Sub
    btw: I wouldn't split SpecialCharacters with each "change"-Event. Do it once and be done with it.
    And Replacing in a loop.... i think i remember writing something like a "ReplaceAny"-Function, but for the life of me i can't remember where it is
    The only thing i remember was using the StrSpn/StrCSpn-API's
    thanks.
    however my other form i tried using it on that has max control limit reach crashes soon as i try enter any words.
    when i say crash i mean the whole project exits vb6 exits

    cause
    Code:
     txtNick = Replace(txtNick, char, "")
    tried added doevents no luck

    rather than replace cant i just stop them from typing by using if xxx then exit sub
    Code:
    if instr(txtNick,char) then
    msgbox "no illigals allowed"
    exit sub
    end if
    this fires even with normal words so not working right
    Last edited by tuffan; Sep 13th, 2022 at 05:57 AM.

  10. #10
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,382

    Re: prevent typing html

    Just saw this:
    Code:
     txtNick = Replace(txtNick, char, "")
    Maybe a stupid question: Does this trigger the Change-Event (recursively?)?

    After reading the thread again, i'd rather go for the KeyDown-Event instead of Change if you only want to prevent the user from entering illegal characters

    Think about it: in your Change-Event you "prevent" the user from entering illegal html-characters, but you have a button which "inserts" text containing html-tags.
    AFTER using this Button, the moment the User continues typing into the textbox, those html-tags from the Button get removed, too
    Last edited by Zvoni; Sep 13th, 2022 at 06:07 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  11. #11

    Thread Starter
    Banned
    Join Date
    Aug 2022
    Posts
    97

    Re: prevent typing html

    ok it was richtextbox playing it replaced with text1 instead ok now

  12. #12

    Thread Starter
    Banned
    Join Date
    Aug 2022
    Posts
    97

    Re: [RESOLVED] prevent typing html

    Zvoni thank you

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