[Closed] dotnet TextBox and enter key
I have a single line dotnet textbox control in a regular max rollout, now I want to capture if enter was pressed. I need to use the dotnet textbox because I can place the caret a a specific place. But how to capture if enter was pressed, I have found some solutions on this forum but they seem to be dealing with multiline textBoxes. I need to fire a eventhandler when enter was pressed.
Any ideas and suggestions are welcome!
Thanks,
-Johan
Hey Johan, have you seen this?
(
local hForm = dotNetObject "MaxCustomControls.MaxForm"
hForm.Size = dotNetObject "System.Drawing.Size" 640 480
hForm.FormBorderStyle = (dotnetclass "System.Windows.Forms.FormBorderStyle").FixedToolWindow
hForm.Text = ""
hForm.ShowInTaskbar = False
local txtbox = dotNetObject "System.Windows.Forms.TextBox"
txtbox.location=dotNetObject "System.Drawing.Point" 10 10
local txtbox2 = dotNetObject "System.Windows.Forms.TextBox"
txtbox2.location=dotNetObject "System.Drawing.Point" 10 40
local cmbbox = dotNetObject "System.Windows.Forms.ComboBox"
cmbbox.location=dotNetObject "System.Drawing.Point" 10 80
local dnKeys=dotnetclass "System.Windows.Forms.Keys"
hform.controls.add(txtbox)
hform.controls.add(txtbox2)
hform.controls.add(cmbbox)
local theKey=false
fn txtbox_KeyDown sender eb =
(
if (eb.Keycode==dnKeys.Tab) OR (eb.Keycode==dnKeys.Return) then theKey=eb.KeyCode else theKey=false
)
fn txtbox_KeyPress sender eb =
(
if theKey!=false then
(
case theKey of
(
(dnKeys.Tab): hform.selectnextcontrol hform.ActiveControl true true false false
(dnKeys.Return): print "Return"
)
)
)
dotnet.addEventHandler txtbox "KeyDown" txtbox_KeyDown
dotnet.addEventHandler txtbox "KeyPress" txtbox_KeyPress
dotnet.addEventHandler txtbox2 "KeyDown" txtbox_KeyDown
dotnet.addEventHandler txtbox2 "KeyPress" txtbox_KeyPress
hForm.ShowModeless()
)
I’m using a MaxForm but I’m guessing that this should work on a rollout, was this what you’re looking for?
Cheers,
Artur Leão
I forgot. In Rollout/Dialog the TextBox’s KeyUp event captures the Enter key.
try(destroydialog textb) catch()
rollout textb ""
(
dotnetcontrol tb "TextBox"
on tb KeyUp s e do if e.KeyCode == e.KeyCode.Enter do print "Enter!"
)
createdialog textb height:40
Thanks all!
I went for the maxform solution, but I only needed the keyDown event handler like Denis mentioned. Weird thing is that with a regular max rollout the enter key is never catched even with enableAccelerators = false… So a dotnet maxform with just the keyDown event did work.
The only reason I still like rollouts so much that now the whole script takes 50 lines while a regular rollout solution for this problem only takes 30 lines.
Also is there a way to assign a variable to a maxform like
myForm.myVariable = dotNetMXSValue someStruct
or do I always have to assign it to the Tag property? In other words can I add public variables to a form via maxscript? Simply declaring it doesn’t work for me anyway.
Thanks,
-Johan
Also is there any disposing I should take care of when closing a maxform. I seem to be getting some garbage collection errors, but I’m not sure if it’s related to this script…
Thanks,
-Johan
Denis, do you have found any “weird” issues with garbage collection and dotnet controls and or dotnet maxforms? Otherwise I’ll be converting my script back to straight max rollouts.
Thanks,
-Johan
I don’t see any “weird” issues with GC for dotnet objects and forms. But I always try to dispose all disposable objects before closing forms. Also I always dispose all images and image lists.
But I prefer to stay with max dialogs/rollouts as long as they fit my needs. And I have the same reasons as yours – rollouts are shorter, clearer, more stable. I like to use dotnetcontrols instead of dotnetobjects because I don’t need to care about their disposing. Max does it for me.