[Closed] .Net – dataGridView Question
Hi guys, i need to select a row using right click mouse button, so i looked up at MSDN for some help and i found i need to use the hitTest method ( http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.hittest%28v=vs.85%29.aspx ), i try to apply this in maxScript but knock me out!, so i come to you, looking for a kind soul who help me understand this. Thanks in advance.
This is an axample of what i was doing...
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 = 100
local myCol2 = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
myCol2.headerText = "Col2"
myCol2.width = 100
local myCol3 = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
myCol3.headerText = "Col3"
myCol3.width = 100
myDGV.columns.add myCol1
myDGV.columns.add myCol2
myDGV.columns.add myCol3
myDGV.width = 343
myDGV.height = 200
for i=0 to 4 do
(
myDGV.rows.add ()
myDGV.rows.item [i].cells.item [0].value = "Pleaaaaseee"
myDGV.rows.item [i].cells.item [1].value = "hellllllppppp"
myDGV.rows.item [i].cells.item [2].value = "meeeeee!!!"
)
) -- on open
on myDGV click s e do
(
if e.button == e.button.left then format "Great, you press the LMB! ...at pos: %
" [e.x, e.y]
if e.button == e.button.middle then format "Excellent, you pressed the MMB! ...at pos: %
" [e.x, e.y]
if e.button == e.button.right then format "Yahooooo, you pressed the RMB! ...at pos: %
" [e.x, e.y]
) -- on click
)
createDialog roll_Test
clearListener ()
on myDGV click s e do if e.Button == e.Button.Right do
(
hit = s.HitTest e.x e.y
if hit.RowIndex > -1 do s.rows.item[hit.RowIndex].selected = on
)
Is it possible to filter the input for the cells? . In this case, i want to set the valueType property of a column to Single, if i write “125,6” when i finish editing the cell the number i wrote is fine, but if i write “125.6” when i finish editing the input value changes to “1256” so perhaps i sould filter the input allowing only 1,2,3,4,5,6,7,8,9,0, and comma characters. is that possible?.
myDGV.columns.item [0].valueType = dotNetClass "System.Single"
fn TextKeyDown s e =
(
format "%
" (e.KeyCode.ToString())
e.SuppressKeyPress = (e.KeyCode == e.KeyCode.OemPeriod) -- which is "." ... the "." on NumPad calls "Decimal"
)
on myDGV EditingControlShowing s e do
(
dotNet.addEventHandler e.Control "KeyDown" TextKeyDown
)
A quick simple brute force way to do this could be to filter the string after it’s been input
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 = 100
local myCol2 = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
myCol2.headerText = "Col2"
myCol2.width = 100
local myCol3 = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
myCol3.headerText = "Col3"
myCol3.width = 100
myDGV.columns.add myCol1
myDGV.columns.add myCol2
myDGV.columns.add myCol3
myDGV.width = 343
myDGV.height = 200
/*
for i=0 to 4 do
(
myDGV.rows.add ()
myDGV.rows.item [i].cells.item [0].value = "Pleaaaaseee"
myDGV.rows.item [i].cells.item [1].value = "hellllllppppp"
myDGV.rows.item [i].cells.item [2].value = "meeeeee!!!"
)*/
) -- on open
on myDGV click s e do if e.Button == e.Button.Right do
(
hit = s.HitTest e.x e.y
if hit.RowIndex > -1 do s.rows.item[hit.RowIndex].selected = on
)
on myDGV CellValueChanged s e do(
if((e.RowIndex != -1) and (e.ColumnIndex != -1))do(
remainder = filterString s.rows.item[e.RowIndex].Cells.Item[e.ColumnIndex].Value "123456789,"
if(remainder.count > 0)do(
messagebox("Invalid Entry")
s.rows.item[e.RowIndex].Cells.Item[e.ColumnIndex].Value = ""
)
)
)
)
createDialog roll_Test
clearListener ()
Thanks guys, i really appreciate you help, both examples could be very usefull.
I tried to capture the keyCode for Return/Enter key but it seems not to work ( http://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx#Y6556 ). I´m trying to use the endEdit () method to exit a Cell when Enter is pressed, but i can´t get it, is Enter key disabled while a cell is being edited? or perhaps keyCode will not be usefull for this. I hope you can help me with this, thanks in advance guys!
I was looking for how to catch Enter key to endEdit a cell, and i think i miss the event handler, i found this at MSDN ( http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.keychar%28v=VS.80%29.aspx ), so i think i must use the keyPress event handler to use the keyChar property in order to check whether the ENTER key was pressed.
The event handler would be:
on myDGV EditingControlShowing s e do
(
dotNet.addEventHandler e.control "keyPress" onEnterPressed
)
The problem for me is i don´t know how to implement this example in maxScript, i don´t know how to get the char value of the Enter/Return key. Could somebody give me a hand on this?. Thanks in advance for the patience and kindness
Example 1:
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
}
Example 2:
if (e.KeyChar == (char)13)
{
messagebox.show ("ENTER was Pressed")
}
in the common case it’s:
(e.keyChar == (dotnetclass "Convert").ToChar (dotnetclass "Keys").Enter)
but you can store the value in local variable for every next use…
Thanks Denis, i tried this, but doesn´t seem to work, am i doing something wrong?, i need to catch enter to end the edition of a cell for the first example of this post. Could you give me a hand on this?, thanks in advance.
fn enterPressed s e =
(
if e.keyChar == ((dotnetclass "Convert").ToChar (dotnetclass "Keys").Enter) then
(
print "Enter was pressed"
)
)
on myDGV editingControlShowing s e do
(
dotNet.addEventHandler e.control "KeyPress" enterPressed
)
The editing control has to accepts Return. By default it’s probable OFF. Set AcceptsReturn property to TRUE on EditingControlShowing event.