Notifications
Clear all

[Closed] New line in TextBox with enter key

My TextBox will respond to the Enter key to do some action but I cannot get the enter key to actually start a new line in a multiline TextBox nor a RichTextBox (all dotNet).

I’ve read all topics on CG about it and none made mine work.

Do I need to do something special to allow starting a new line with the enter key (it’s so simple)?

Davy

11 Replies
 lo1

I remember some weird issue where you also have to set acceptsTab to true for acceptsReturn to work

I tried them all. With TextBox AcceptsTab and Return, no luck.
RichTextBox doesn’t even have AcceptsReturn since its suppose to work by default. But tried the AcceptsTab, no luck either.

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

probably you missed something. i don’t see a problem:


 try(form.close()) catch()
 form = dotnetobject "MaxCustomControls.MaxForm"
 form.size = dotnetobject "System.Drawing.Size" 200 200
 	
 tb = dotnetobject "TextBox"
 tb.Dock = tb.Dock.Fill
 tb.MultiLine = true
 tb.AcceptsTab = true
 	
 fn onKeyUp s e =
 (
 	case e.KeyCode of
 	(
 		(e.KeyCode.Enter): print "ENTER pressed" 
 	)
 )	
 dotnet.addEventHandler tb "KeyUp" onKeyUp
 
 form.controls.add tb	
 form.showmodeless()
 

And why does this not work as well?


TBRF=newRolloutFloater "TextBox" 200 200 100 100

rollout TBR "TextBox"
(
	dotNetControl TBC "System.Windows.Forms.TextBox" height:100

	on TBR open do
	(
		TBC.Multiline=true
		TBC.AcceptsReturn=true
		TBC.AcceptsTab=true
	)
)
addRollout TBR TBRF

 lo1

Oh, I remember now. It just does not work, you have to implement it yourself.

TBRF=newRolloutFloater "TextBox" 200 200 100 100

rollout TBR "TextBox"
(
	dotNetControl TBC "System.Windows.Forms.TextBox" height:100

	on TBR open do
	(
		TBC.Multiline=true
		TBC.AcceptsReturn=true
		TBC.AcceptsTab=true
	)
	
	on TBC keyUp s e do
	(
		if e.KeyCode == e.KeyCode.Enter do 
		(
			local sel = s.selectionstart
			s.text = (dotNetobject "system.string" s.text).insert s.selectionStart (dotNetClass "System.Environment").newline
			s.selectionStart=sel+2
			s.selectionLength=0
			s.scrollToCaret()
			s.refresh()
		)
	)
)
addRollout TBR TBRF
2 Replies
(@denist)
Joined: 11 months ago

Posts: 0

...
	on TBC keyUp s e do
	(
		if e.KeyCode == e.KeyCode.Enter do s.selectedtext = (dotNetClass "System.Environment").newline
	)
...

 lo1
(@lo1)
Joined: 11 months ago

Posts: 0

As usual, Denis with the +1

I lost about 2h today because of that
I actually started implementing it like that but stopped thinking this was insane for something obvious and came in here to ask my question.

Thanks a lot

Works great, thanks a lot

could be better:


  try(destroydialog tb_dialog) catch()
 rollout tb_dialog "Text BOX" width:200 height:100
 (
 	dotnetcontrol tb "TextBox" width:192 height:92 pos:[4,4]
 
 	on tb keyUp s e do
 	(
 		if e.KeyCode == e.KeyCode.Enter do s.Paste (dotNetClass "System.Environment").NewLine
 	)
 	on tb_dialog open do
 	(
 		tb.Multiline = on
 		tb.AcceptsTab = on
 	)
 )
 createdialog tb_dialog

now it’s undoable

PS. (dotNetClass “System.Environment”).NewLine
lo, I like it !