[Closed] .Net – dataGridView Question
Denis, i tried to add “e.control.acceptsReturn = true” to the editingControlShowing event and again, it didn´t work, i couldn´t catch enter key. This is what i tried…
fn enterPressed s e =
(
if e.keyChar == ((dotnetclass "Convert").ToChar (dotnetclass "Keys").Enter) then
(
print "Enter was pressed"
)
)
on myDGV editingControlShowing s e do
(
e.control.acceptsReturn = true
dotNet.addEventHandler e.control "KeyPress" enterPressed
)
Instead, i try the following example and actually catches Enter key, but the problem is it doesn´t catch enter if i don´t hit another key first. It happens only the first time, after this, if i hit Enter on another cell, it works perfectly. Do you know how could i fix it?. Thank you Denis, i really appreciate your help.
fn keyReleased s e =
(
if (e.keycode == (dotnetclass "Keys").enter) then
(
print "Enter pressed"
e.handled = true
)
)
on myDGV editingControlShowing s e do
(
dotNet.addEventHandler e.control "keyUp" keyReleased
)
it seems like you have some other problem… we can catch the ENTER key, but what do you want to do next? what is the main idea?
Well Denis, this is what i need, while i´m editing a cell, if enter is pressed, i need to stop the edit mode and validate the cell value, perhaps i did things wrong, could you help me with this example?. Thanks in advance, and really want to thanks for your patience.
try (destroyDialog roll_Test) catch ()
rollout roll_Test "Test" width:370 height:210
(
dotNetControl myDGV "System.Windows.Forms.DataGridView"
on roll_Test open do
(
local myCol1 = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
myCol1.headerText = "Col1"
myCol1.width = 300
myDGV.columns.add myCol1
myDGV.width = 343
myDGV.height = 200
myDGV.rowCount =5
myDGV.columns.item [0].valueType = dotNetClass "System.Single"
) -- on open
fn filterInput s e = ( e.suppressKeyPress = (e.keyCode == e.keyCode.OemPeriod) or (e.KeyCode == e.KeyCode.Decimal))
fn onEnterPressed s e =
(
-- need to catch enter
-- if enter is pressed while editing the cell, then i need to stop the edit mode and validate the cell value
)
on myDGV Click s e do (s.beginEdit on)
on myDGV editingControlShowing s e do -- When the cell is being edited
(
dotNet.addEventHandler e.control "keyDown" filterInput
dotNet.addEventHandler e.control "keyUp" onEnterPressed -- i need this event to handle when enter key pressed (keyUp/keyDown/keyPress)
)
) -- rollout
createDialog roll_Test
the problem is :
when dotnet controls put into max rollout the rollout suppresses some messages. in you case it’s DLGC_WANTALLKEYS.
if you put Grid View into Form (MAX or .NET) it will work fine. But if you need to stay with max rollout there is a solution that I showed for Tree View: [ http://forums.cgsociety.org/showpost.php?p=6785100&postcount=1 ]( http://forums.cgsociety.org/showpost.php?p=6785100&postcount=1)
or
fn CreateNativeWindowOps forceRecompile:on = if forceRecompile do
(
source = ""
source += "using System;
"
source += "using System.Windows.Forms;
"
source += "public class WindowHook : NativeWindow
"
source += "{
"
source += " private const int WM_GETDLGCODE = 0x0087;
"
source += " private const int DLGC_WANTALLKEYS = 0x0004;
"
source += " public WindowHook() { }
"
source += " protected override void WndProc(ref Message m)
"
source += " {
"
source += " switch (m.Msg)
"
source += " {
"
source += " case WM_GETDLGCODE:
"
source += " m.Result = (IntPtr)DLGC_WANTALLKEYS;
"
source += " break;
"
source += " default:
"
source += " base.WndProc(ref m);
"
source += " break;
"
source += " }
"
source += " }
"
source += "}
"
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
compilerParams.ReferencedAssemblies.AddRange #("System.dll", "System.Windows.Forms.dll")
compilerParams.GenerateInMemory = true
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
global NativeWindowOps = compilerResults.CompiledAssembly
)
CreateNativeWindowOps()
try (destroyDialog roll_Test) catch ()
rollout roll_Test "Test" width:370 height:210
(
local hook = dotnetobject "WindowHook"
dotNetControl gv "DataGridView"
on roll_Test open do
(
local dc1 = dotNetObject "DataGridViewTextBoxColumn"
dc1.headerText = ""
dc1.width = 300
gv.columns.add dc1
gv.width = 343
gv.height = 200
gv.rowCount = 5
)
fn onKeyPress s e =
(
format "key press: %
" (bit.charasint e.keyChar)
)
fn onKeyDown s e =
(
format "key down: %
" (e.keyCode.ToString())
)
on gv EditingControlShowing s e do -- When the cell is being edited
(
hook.AssignHandle (dotnetobject "IntPtr" e.Control.Handle)
dotNet.removeAllEventHandlers e.Control
dotNet.addEventHandler e.Control "KeyPress" onKeyPress
dotNet.addEventHandler e.Control "KeyDown" onKeyDown
)
on gv CellEndEdit s e do
(
format "cell end edit... : %
" s.rows.item[e.RowIndex].cells.item[e.ColumnIndex].value
hook.ReleaseHandle()
)
) -- rollout
createDialog roll_Test
Woooowww, thank you very much Denis, that works great!!, i´ll ask a friend about the cSharp part you put in there. Now i´ll try use a .Net form or MaxForm. These are my first steps into dotNet, i find it very interesting, but sometimes it´s a bit hard to “translate” the syntax to use in max. So, i apologize for bothering you with countless questions, i really appreciate your help, Thanks again!!
I was trying to make the same example but this time without using rollouts but maxforms, i saw that endEdit () when enter is pressed is default action. I need to ask you a last question, how do i implement the keyDown event on the editingControlShowing without use a rollout?, i couldn´t find examples of how to use that eventHandler without using rollout. Thanks and sorry for bothering you again.
function TestMaxForm =
(
-- theForm
theForm = dotNetObject "MaxCustomCOntrols.MaxForm"
theForm.text = "dataGridView in a MaxForm"
theForm.width = 266
theForm.height = 200
-- The dataGridView
myDGV = dotNetObject "System.Windows.Forms.DataGridView"
myCol1 = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
myCol1.headerText = "Column 1"
myDGV.location = dotNetObject "System.Drawing.Point" 0 0
myCol1.width = 300
myDGV.columns.add myCol1
myDGV.width = 250
myDGV.height = 200
myDGV.rowCount = 5
myDGV.columns.item [0].valueType = dotNetClass "System.Single"
fn beginEdit s e = (s.beginEdit on )
fn filterInput s e = ( e.suppressKeyPress = (e.keyCode == e.keyCode.OemPeriod) or (e.KeyCode == e.KeyCode.Decimal))
dotNet.addEventHandler myDGV "click" beginEdit
theForm.controls.add myDGV
theForm.Show ()
/* HOW DO I IMPLEMENT THE FOLLOWING EVENT IN THE CONTEXT OF A MAXFORM ?
on myDGV editingControlShowing s e do -- When the cell is being edited
(
dotNet.addEventHandler e.control "keyDown" filterInput
)
*/
)
TestMaxForm ()
how could i access to a Maxform for asking properties/Methods/Events? Using rollouts when the rollout was opened i could access it this way…
showProperties roll_Test.myDVG.currentCell
is it possible using forms?
function TestMaxForm =
(
-- theForm
theForm = dotNetObject "MaxCustomCOntrols.MaxForm"
theForm.text = "dataGridView in a MaxForm"
theForm.width = 266
theForm.height = 200
-- The dataGridView
myDGV = dotNetObject "System.Windows.Forms.DataGridView"
myCol1 = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
myCol1.headerText = "Column 1"
myDGV.location = dotNetObject "System.Drawing.Point" 0 0
myCol1.width = 300
myDGV.columns.add myCol1
myDGV.width = 250
myDGV.height = 200
myDGV.rowCount = 5
myDGV.columns.item [0].valueType = dotNetClass "System.Single"
fn beginEdit s e = (s.beginEdit on )
dotNet.addEventHandler myDGV "Click" beginEdit
fn editingControlShowing s e =
(
fn filterInput s e = (e.suppressKeyPress = (e.keyCode == e.keyCode.OemPeriod) or (e.KeyCode == e.KeyCode.Decimal))
fn onKeyPress s e =
(
format "key press:% %
" s (bit.charasint e.keyChar)
)
dotNet.removeAllEventHandlers e.control
dotNet.addEventHandler e.control "KeyDown" filterInput
dotNet.addEventHandler e.control "KeyPress" onKeyPress
)
dotNet.addEventHandler myDGV "EditingControlShowing" editingControlShowing
theForm.controls.add myDGV
theForm.Showmodeless()
theForm
)
theForm = TestMaxForm()
Thank you very much Denis, for everything, it works great!! and you gave me some excellent examples, thanks again.