Non-breaking space during input

Solved
GrandJean -  
 GrandJean -
Hello everyone,

I want to be able to insert a non-breaking space while typing in a textbox.
I found the code chr(160) but I can't figure out how to integrate it while typing.
This method interests me because I would also like to be able to integrate a bullet point, for example chr(149), while typing.

I haven't found any solutions on the forums and my various attempts...

In short, I'm asking for your help and thank you in advance

Jean

Configuration: Windows XP / Chrome 32.0.1700.107

6 answers

  1. f894009 Posted messages 17417 Registration date   Status Member Last intervention   1 717
     
    Hello,

    it all depends on where you want to put these characters, if before the input:
    for example:
    x = Chr(149) & Chr(160) & TextBox1
    0
  2. Zoul67 Posted messages 2001 Status Member 149
     
    Hello,

    In VB or VBA?
    It's surprising to specifically need a non-breaking space...
    If you want to automatically replace a character with another during input (or prevent the entry of certain characters), use the ASCII codes in the procedure:

    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)


    Cheers!
    0
  3. GrandJean
     
    Thank you both for responding so quickly.

    It's specifically while typing that I want to be able to use these characters. If my sentence ends with a ?, I need a non-breaking space to prevent the ? from being alone on a new line when the content is used in another form.

    I have tried with keypress, but without success. I can't find the connection between chr() and keyAscii. Sorry if I'm not expressing myself well.

    I hope you have a lead.
    0
  4. GrandJean
     
    Sorry, I forgot to specify: in Excel VBA
    0
  5. Zoul67 Posted messages 2001 Status Member 149
     
    Un exemple pour ajouter un espace insécable avant le ? :
    (adapt the code to your needs; this one prevents unwanted character input)
    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Select Case KeyAscii Case 63 TextBox1.Value = TextBox1.Value & Chr(160) Case 97 To 122 ' Lowercase alpha characters Case 65 To 90 ' Uppercase alpha characters Case 8 ' Backspace Case Else KeyAscii = 0 End Select End Sub
    0
  6. GrandJean
     
    Great.

    I tried your solution and it works very well. Thank you very much.
    However, while it works well when creating a new text, in correction mode it doesn't work: if I go back to the text and want to insert a ?, using the select case, my ? ends up as the last character.
    So, I replaced the select case with:
    TB_1 = Replace(TB_1, " ?", Chr(160) & "?") 'for the non-breaking space
    TB_1 = Replace(TB_1, "*", Chr(149)) 'for the bullet

    Now, I can place my character where needed. The cursor, however, still positions itself at the end of the text. But that's not a big deal.
    I would have preferred a keyboard shortcut (like ctrl / Enter for going to a new line). I haven't found one.

    Thanks again
    Jean
    0
    1. GrandJean
       
      To solve the cursor position, I used SelStart, which gives:
      PosCurseur = Me.TB_1.SelStart
      TB_1 = Replace(TB_1, " ?", Chr(160) & " ?")
      TB_1 = Replace(TB_1, "*", Chr(149))
      Me.TB_1.SelStart = PosCurseur
      0