PDA

View Full Version : dotNet: textBox on return pressed event?


PEN
10-08-2008, 08:24 PM
How would I go about using the enter button to call an event in the textBox object? This would be the same as the on editText entered do event. Any one?

martroyx
10-08-2008, 08:41 PM
I Pen,
That exactly what I'm looking for too. I think it have something to do with the controls.invoke methods but I don't really know how it work...

http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx


fn Textbox_KeyPress s e=
(
if e.KeyChar == "\r" then
(
s.control.invoke (ref to the handler here)
)
)


Regard,
Martin Dufour

magicm
10-08-2008, 10:29 PM
Here's an example, hope it helps

Cheers,
Martijn

PEN
10-09-2008, 12:11 AM
Thanks Martijn, I finaly found something on it in another post as well. That is a good example that you have there.

martroyx
10-09-2008, 12:19 AM
That a very good example, it may come handy when I'll be working on my dotneteditor :applause:

but here's what I really want :


try (Form1.close()) catch ()
(
Global Form1 = (Dotnetobject "System.Windows.Forms.Form")
Global PboxMap = (Dotnetobject "System.Windows.Forms.PictureBox")
Global PButton = (Dotnetobject "System.Windows.Forms.Button")
--//--
--//-- let say I want to manualy fire this handler when I click the PButton
--//-- and also, I don't want to call the function itself.
--//--
fn PboxMap_Click s e =
(
s.backcolor = (s.backcolor.fromargb 255 (random 100 255) (random 100 255) (random 100 255))
)
--//--
fn PButton_Click s e =
(
--((PboxMap.getType()).GetEvent "Click")
--PboxMap_Click PboxMap ""
)
--//--
fn Form1_Closed s e =
(
Form1 = undefined
PboxMap = undefined
PButton = undefined
gc light:true
)
--//-- Initialisation
PboxMap.backcolor = PboxMap.backcolor.navy
PboxMap.dock = PboxMap.dock.fill
--//--
PButton.backcolor = PButton.backcolor.fromargb 100 100 100 100
PButton.forecolor = PButton.forecolor.white
PButton.dock = PButton.dock.fill
PButton.text = "Fire PboxMap Click Handler"
PButton.font = (dotNetobject "System.Drawing.Font" PButton.font.name 14)
--//--
Form1.Text = "Test Form"
Form1.ShowIcon = true
Form1.ShowInTaskbar = false
Form1.TopMost = true
Form1.StartPosition = Form1.StartPosition.CenterScreen
Form1.ClientSize = dotNetObject "System.Drawing.Size" 200 100
--//--
Form1.Controls.Add PboxMap
PboxMap.Controls.Add PButton
--//--
dotnet.addeventhandler Form1 "closed" Form1_Closed
dotnet.addeventhandler PButton "Click" PButton_Click
dotnet.addeventhandler PboxMap "Click" PboxMap_Click
--//--
Form1.Show()
)


Thank for your help,
Martin

PEN
10-09-2008, 12:59 AM
Still no go. I'm writting a scripted modifier and the enter key isn't firing the keyDown event at all. The first print statement isn't being fired so enter/return isn't being seen. Also I can't get tab to work although I don't need it. Any ideas any one?


fn initTextBox tb=
(
tb.multiLine=true
tb.AcceptsReturn=true
tb.text=characterName
)

on characterNameDn keyDown arg do
(
print #keyDown
print arg.KeyCode
if arg.KeyCode==arg.KeyCode.enter then
(
print #yup
addPrefixToCharacter senderArg.text replacePrefix:true
)
)

martroyx
10-09-2008, 01:08 AM
Hi Paul,
I used the keypress and test for "\r" because of this, maybe your could give it a try ...


fn Textbox_KeyPress s e=
(
if e.KeyChar == "\r" then
(
---
)
)


Martin Dufour

martroyx
10-09-2008, 01:13 AM
maybe this could help too...


fn textbox_MouseClick e s = (enableAccelerators = false)

magicm
10-09-2008, 07:56 AM
That a very good example, it may come handy when I'll be working on my dotneteditor :applause:

but here's what I really want :

...let say I want to manualy fire this handler when I click the PButton
and also, I don't want to call the function itself.
Since they are both the same type of event (Click) and thus share the same event argument, you could simpy use PboxMap_Click s e to pass it on.

Another way could be to have each event call a function instead:
fn changeColor NetObj =
(
NetObj.backcolor = (NetObj.BackColor.FromArgb 255 (random 100 255) (random 100 255) (random 100 255))
)

fn PboxMap_Click s e = changeColor s
fn PButton_Click s e = changeColor s
Cheers,
Martijn

magicm
10-09-2008, 07:59 AM
Still no go. I'm writting a scripted modifier and the enter key isn't firing the keyDown event at all. The first print statement isn't being fired so enter/return isn't being seen. Also I can't get tab to work although I don't need it. Any ideas any one?
I forgot to add to my initial reply that the AcceptsTab property needs to be set to true as well for this to work. Not sure why..

Martijn

martroyx
10-09-2008, 08:56 AM
Since they are both the same type of event (Click) and thus share the same event argument, you could simpy use PboxMap_Click s e to pass it on.


That's what I mean by ...I don't want to call the function itself...:wise:
Well, maybe it's not possible in maxscript but from what I understand of the
controls.invoke method you can do something like this:


Theevent = (gather the event from the object ???) as a "system.delegate" witch is a ref to the event.
PboxMap.invoke Theevent


not that I need this, just wanted to know :-)

Martin Dufour

Kameleon
10-09-2008, 10:36 AM
Hi guys, I actually have a working script here, and I've found how to put Tab working (finally!!!). I'm using two events just to show the order of event calling, first being KeyDown then KeyPress, you can actually restrict user input characters in the KeyPress event by doing e.Handled=true. Anyway, here it is, I hope it helps in some way. Cheers!

(
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()
)

Edit: Oh and AcceptsTab and AcceptsReturn are only valid when working with multi-line textboxes.

magicm
10-09-2008, 11:05 AM
That's what I mean by ...I don't want to call the function itself...:wise:
Well, maybe it's not possible in maxscript but from what I understand of the
controls.invoke method you can do something like this:


Theevent = (gather the event from the object ???) as a "system.delegate" witch is a ref to the event.
PboxMap.invoke Theevent


(PboxMap.GetType()).GetEvent("Click").GetRaiseMethod() should return a MethodInfo object. This object in turn has an Invoke method (MethodBase.Invoke Method (Object, Object[])). However, a call to GetRaiseMethod() will always return undefined in mxs, simply because the method is not created (there's more info on this on the MSDN website (http://msdn.microsoft.com/en-us/library/a89hcwhh.aspx).

As far as delegates go, I don't think they can be created from within maxscript.

I am curious though as to what you are trying to do? Do you have a specific reason why you wouldn't want to call the event handler directly?

Cheers,
Martijn

PEN
10-09-2008, 12:41 PM
Thanks again I will try that a bit later today to see if I can get it working.

martroyx
10-09-2008, 06:28 PM
GetRaiseMethod() will always return undefined in mxs, simply because the method is not created


I don't know what this mean but guest it's why it return undefined :)


I am curious though as to what you are trying to do? Do you have a specific reason why you wouldn't want to call the event handler directly?


No specific reason, but I just can't figure how to build this "system.delegate" thing...
I'll be sure go get back to you if I found something about this !

Regard,
Martin Dufour

CGTalk Moderation
10-09-2008, 06:28 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.