MXS dotNet exception

MXS dotNet exception

istan
Advisor Advisor
797 Views
5 Replies
Message 1 of 6

MXS dotNet exception

istan
Advisor
Advisor

Why I'm getting an exception here, when <ESC> is pressed ?

fn foo = (
  local theFrm = dotNetObject "System.Windows.Forms.Form"
  -- doesn't matter - same effect:
  --local theFrm = dotNetObject "MaxCustomControls.MaxForm"

  theFrm.width = 200
  theFrm.height = 100
  theFrm.text = "DotNet Form"
	
  local Input = dotNetObject "System.Windows.Forms.TextBox" 
  Input.location = dotNetObject "System.Drawing.Point" 0 20
  Input.width = 50 
  Input.height = 30
  Input.Text = "foo"
  theFrm.controls.add Input

  -- this assignment causes the exception?!
  dotNet.addEventHandler Input "KeyDown" ( fn foo s e = () )
	
  local txt = dotNetObject "System.Windows.Forms.Label"
  txt.location = dotNetObject "System.Drawing.Point" 0 2
  txt.text = "Press the <ESC> key.."
  txt.width = 200
  theFrm.controls.add txt

  try thefrm.ShowDialog() catch()
)

foo true

 

0 Likes
798 Views
5 Replies
Replies (5)
Message 2 of 6

denisT.MaxDoctor
Advisor
Advisor

When working with a .net TextBox form control, as a rule, you need to do a few basic things:

# disable / enable accelerators
# decide what to do with Return, Escape and Tab
# deal with the beep sound on these keys

 

this example shows how to do this:

 

fn GotFocus s e =
(
	s.text = ""
	enableAccelerators = off
)
fn LostFocus s e =
(
	s.text = "Type here"
	enableAccelerators = on
)
fn KeyDown s e =
(
	if ((e.KeyCode == e.KeyCode.Enter) or (e.KeyCode == e.KeyCode.Tab) or (e.KeyCode == e.KeyCode.Escape)) do
	(
		e.Handled = e.SuppressKeyPress = true
	)
)
fn KeyUp s e =
(
	case e.KeyCode of
	(
		 (e.KeyCode.Enter): print "enter"
		(e.KeyCode.Escape): print "escape"
	)
)

form = dotnetobject "maxcustomcontrols.maxform"
form.width = 200
form.height = 100
form.text = "DotNet Form"

tb = dotNetObject "System.Windows.Forms.TextBox" 
tb.location = dotNetObject "System.Drawing.Point" 20 20
tb.width = 140 
tb.height = 30
tb.AcceptsReturn = on

form.controls.add tb

dotnet.addeventhandler tb "GotFocus" GotFocus
dotnet.addeventhandler tb "LostFocus" LostFocus
dotnet.addeventhandler tb "KeyDown" KeyDown
dotnet.addeventhandler tb "KeyUp" KeyUp


form.showmodeless()
0 Likes
Message 3 of 6

istan
Advisor
Advisor

Sorry to say, but your code also spits out an exception, when I press <Escape>.

btw: Max2020 but also happens in 2017..

 

 

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
   at _CxxThrowException(Void* , _s__ThrowInfo* )
   at MXS_dotNet.DotNetObjectWrapper.ProcessEvent(DotNetObjectWrapper* , String eventName, Object delegateArgs)
   at MXS_dotNet.DotNetObjectManaged.delegate_proxy_type.ProcessEvent(String eventName, Object delegateArgs)
   at System.Windows.Forms.Control.OnKeyDown(KeyEventArgs e)
   at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
   at System.Windows.Forms.Control.WmKeyChar(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.TextBox.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

 

 

0 Likes
Message 4 of 6

denisT.MaxDoctor
Advisor
Advisor

Oh! My bad. I have not tested this in versions 2016+

 

I am sure that this is Autodesk's fault. TextBox always worked for me until 2016+. You can also see that ComboBox, for example, works fine, although it uses the same “edit control”.

 

   

0 Likes
Message 5 of 6

istan
Advisor
Advisor

I'll open a ticket..

0 Likes
Message 6 of 6

denisT.MaxDoctor
Advisor
Advisor

Now I am absolutely sure that this is an Autodesk's bug.

The simplest example to see is:

 

fn KeyPress s e =
(
)

form = dotnetobject "maxcustomcontrols.maxform"
form.text = "DotNet Form"
form.KeyPreview = on

dotnet.addeventhandler form "KeyPress" KeyPress
form.showmodeless()

 

This is easy to see if KeyPreview is set to TRUE.

 

I can solve the problem by overriding ProcessKeyMessage, but this should no doubt be fixed by Autodesk

 

0 Likes