Prevent / Remove EndofLines in Text Dragged to TextField or TextArea

I have a TextField and two TextAreas with the following code which prevents pasting any text with endoflines and directly typing endoflines.

How do I prevent any text dragged into these controls from containing endoflines?

MouseDown:

  Dim S as String
  Dim C as New Clipboard
  
  S = C.Text
  S = ReplaceAll(S, Chr(13), " ")
  S = ReplaceAll(S, Chr(10), " ")
  C.Text = S

KeyDown:

  If Asc(Key) = 13 or Asc(Key) = 10 then
    Return True
  End If

put similar code in the DROPOBJECT event which you will need to activate as well

also

c.text=replaceEndoflines(s," ")

This worked.

Open:

Me.AcceptTextDrop

DropObject:

  Dim C as New Clipboard
  Dim S as String
  
  S = Obj.Text
  S = ReplaceLineEndings(S, " ")
  C.Text = S
  Me.Paste

You can save a lot of code by using the TextChange event. Every time it fires, either trim the newlines or revert to a previous version of the field that you’ve saved. If the latter, and there are no newlines, save the contents, SelStart and SelLength so you can restore them later if needed.

TextChange will cover you no matter how the text is updated, be it typing, pasting, or drag-and-dropping.

TextField & TextAreas TextChange Event:

  Dim S as String
  S = Me.Text
  S = ReplaceLineEndings(S, " ")
  Me.Text = S

TextArea KeyDown Event:

  If Asc(Key) = 3 or Asc(Key) = 10 or Asc(Key) = 13 then
    Return True
  End If
Dim S as String
  S = Me.Text
  S = ReplaceLineEndings(S, " ")
  Me.Text = S

won’t this cause a duplication of the event call?
Since the TextChange event is in fact changing the text?

wouldn’t a safer method be something like

if instr(me.text,chrb(&H0A)+instr(me.text,chrb(&H0D)>0 then Me.Text =  ReplaceLineEndings(me.text, " ")

it will still call it twice if there are CF/LF, but won’t invoke the update overhead

I think Xojo prevents recursion of that event internally, but your advice is still sound. Otherwise, every keystroke would cause the user to lose their place in the field.

What would be an advantage, if any, of using chrb in the answer verses chr?

In this case, they yield the same result, but ChrB will return a single byte whereas Chr will return a unicode character that may be up to four bytes internally.

and according to the solution to another topic… CHRB() can break an encoding, where CHR() might not

In this specific example, none.

ChrB should only be used when you are dealing with “raw bytes” held in a string (ie/ read from a binary stream, or socket). Any time you have"textual data" you should use Chr. Using ChrB on data that already has a proper encoding will set the encoding to NIL and you can cause yourself to have weird effects as mentioned in:

https://blog.xojo.com/2013/08/20/why-are-there-diamonds-in-my-user-interface/

Thanks for the replies.

I am saving to a custom xml document format with UTF-8 encoding. All text is generated by the user in listboxes, textareas, and textfields would be internally stored as UTF-8 as well.

if instr(me.text,chr(10)+instr(me.text,chr(13)>0 then Me.Text =  ReplaceLineEndings(me.text, " ")

A minor point…

In terms of optimization, using separate If conditions would be faster. The way that’s written, it’s forced to look for a CR even if it already found a LF.

if me.Text.InStr( Chr( 10 ) ) <> 0 or me.Text.InStr( Chr( 13 ) ) <> 0 then ...

In practice, I don’t know if you’d be able to measure the difference.

I’ve updated the code to also account for SelStart and optimization. The user is prevented from typing any endoflines in a textfield or textarea. Textareas can still display multiple lines of text such as a large paragraph but not include any endoflines. If any text is pasted or dragged into these controls any endoflines are replaced with spaces.

TextChange

  If Instr(Me.Text, EndOfLine) > 0 then
    Dim S as String
    Dim I as Integer
    S = Me.Text
    I = Me.SelStart
    S = ReplaceLineEndings(S, " ")
    Me.Text = S
    Me.SelStart = I
  End If

KeyDown

  If Asc(Key) = 3 or Asc(Key) = 10 or Asc(Key) = 13 then
    Return True
  End If