SpecialGridKey processed twice

Apr 2, 2009 at 12:48 PM
Hi all !
Can you please help me?..
I use SourceGrid4_11.

Method GridVirtual.ProcessSpecialGridKey(KeyEventArgs e) is called twice if  (KeyEventArgs)e.Handled wasn't set to true.
That's why, if  I catch Validating event (which fires when user press Tab after cell value editing) and cancel (in some conditions) validation with explanation message to user, this message shows twice too.

How can I solve this problem?
Thanks.
Coordinator
Apr 2, 2009 at 8:21 PM
I am not sure what is exactly happening.  Have you tried upgrading to version 4.20 ?

If the problem still persists, could you give a sample code to reproduce this error, please?

Thanks
Apr 3, 2009 at 9:08 AM
Edited Apr 3, 2009 at 9:19 AM

Hi. Thanks for your reply.

Unfortunately, I haven't tried to use v4.20 because, as I know, it's written for VS 2008, but my application is in VS2005.

I guess, this problem doesn't  depend of my code, because, if  I press Enter key on cell, which editor is disabled and cell is not in Editing state, method  GridVirtual.OnKeyDown(KeyEventArgs e)(and, accordingly, GridVirtual.ProcessSpecialGridKey(..)) is called twice anyway. Even if Editor is usual TextBox and doesn't have any my event handlers, this problem occurs.

For example:
In your's code (GridVirtual.ProcessCmdKey(..), line 1139):

 

 

if

 

 

(focusCellContext.Cell != null && focusCellContext.IsEditing())

 

{

focusCellContext.EndEdit(

 

false);

 

e.Handled =

 

true;

 

}

If  focusCellContext.IsEditing() is false, e.Handled is not set to true, and this method is called in the second time.

Can you, please, give me an advice, how to avoid this problem?

 

Apr 3, 2009 at 12:24 PM

GridVirtual.ProcessCmdKey()  calls GridVirtual.OnKeyDown():

 

 

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
....
OnKeyDown(args);

if (args.Handled)
return true;
else
return base.ProcessCmdKey(ref msg, keyData);
....
}

and then method OnKeyDown(...) is called automatically. But This method contains code:
...
if
(e.Handled == false)
ProcessSpecialGridKey(e);
...

that's why, if in first time method ProcessSpecialGridKey(..) hadn't set e.Handled to true, it would be called again.

That's what I've saw. Am I right?..
Please, If you can, give me an advice how to avoid this issue?...

 

Apr 8, 2009 at 1:36 PM
You can always build the Sourcegrid project using SharpDevelop, which is open source to get the latest version, then use the dlls in VS2005 for your project.  I have seen posts saying that it is developed using SharpDevelop so it will definatly build the current version.
Apr 8, 2009 at 2:29 PM
SharpDevelop 3.0 will load solution without problems and compile it for .Net 2.0 so you can use these dlls as Wavey said in vs2005 projects without problems.
Apr 8, 2009 at 7:22 PM
Hi. Thanks for advices.
I'll try to use v4_20 as you've said. But, as for me, it's better to use source code, but not only dll....

I've downloaded Sourcegrid v4_20 and saw it's code, and, if I'm not mistaking, GridVirtual has the same code, as the version 4_11 - methods ProcessCmdKey, OnKeyDown and ProcessSpesialGridKey hadn't changed...

Have you tried to check key processing? Does this problem really occurs?

Thanks.
Coordinator
Apr 15, 2009 at 5:15 AM
Hi,


Sorry for late reply. Better later than never.
I can't reproduce the error.  Could you give any code how to reproduce this error?

And yes, you are reight - those methods did not change from 4.11 to 4.20. But if what you found  is really a bug, then we will fix it :)
Apr 16, 2009 at 1:20 PM
Edited Apr 16, 2009 at 1:22 PM
 Hi. Thanks for reply.
 This problem can be reproduced even in the simple example like this:

I create a usual grid and then set the cell:

SourceGrid.Cells.
Cell bomCell = new SourceGrid.Cells.Cell();
bomCell.View = new SourceGrid.Cells.Views.Cell();
_tableGrid[currentRow, ColumnBOM] = bomCell;

(there's the full description of this cell,  I haven't set any editor for it.)
When my application shows me this grid, I select this cell (bomCell) and press Enter key. I've set a breakpoint on line 1555 in GridVirtual.cs. And I've hit this breakpoint twice.

That's the simplest example.

But my problem is the next:
I create a grid and the first column should contain the unique keys. When user adds the new line into the grid, unique key cell is automatically initialized by the first item of special list of keys. For example, keys are the letters of alphabet.
User have selected "A" in the first column of the first row. Then he adds a new line and the first cell of new line is initialized by "A" too, but cell is in Editing state. User should select another value of ComboBox in this cell. I catch Control.Validating event of cell.Editor's control and check the entered value.
If user selected "A" again,  I see it is incorrect because this key, "A", is the key of the first row. I show a message to user and then cancel validation. But if user try to leave edited cell by pressing Escape key, message to user shows twice, because I can't let the validation...
That's my problem... Unfortunately, I can't post the whole code, it's really volumetric....

Coordinator
Apr 17, 2009 at 5:15 AM
GridVirtual.cs:1555 in trunk is OnMouseDown method,   probably it's not where you put your breakpoint.   What method exactly are you setting breakpoint?
And could you show what event are you subcribing to to show your message box? 
Apr 17, 2009 at 8:10 AM
Hi.
I use source code v4_11, and in this version line 1555 is in trunk of OnKeyDown method..... This method is really called twice...

in my code I create an Editor:
.....................
_alphabetEditor =
new ComboBox(typeof(string), AlphabetKeys, true);
_alphabetEditor .Control.Validating += new System.ComponentModel.CancelEventHandler(Control_Validating);
.................

void
Control_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
.....................
    if
(KeyRepeated(.....))
    {
        System.Windows.Forms.MessageBox.Show(_errorMessageStr);
        e.Cancel =
true;
        return;
    }
......

Event _alphabetEditor .Control.Validating occurs in the second time because of method OnKeyDown called twice when user press Escape and my handler cancel validation....

 

Coordinator
Apr 19, 2009 at 4:07 PM
Hi, i've tried reproducing your error, but no sucess.


I've modified sample 17, and replaced the main for loop with the following:

for (int r = 1; r < grid1.RowsCount; r++)
            {
                grid1[r,0] = new SourceGrid.Cells.RowHeader("Header " + r.ToString());
                for (int c = 1; c < grid1.ColumnsCount; c++)
                {
                    if (rnd.NextDouble() > 0.20)
                    {
                        grid1[r,c] = new SourceGrid.Cells.Cell(r*c, typeof(int));
                        
                        SourceGrid.Cells.Editors.TextBox textbox = new SourceGrid.Cells.Editors.TextBox(typeof(int));
                        grid1[r,c].Editor = textbox;
                        textbox.Control.Validating += delegate(object csender, CancelEventArgs ce)
                        {
                            System.Windows.Forms.MessageBox.Show("should appear twice, but does not");
                            ce.Cancel = true;
                        };
                        
                    }
                    else
                        grid1[r,c] = null;
                }
            }


When i start ssample 17, and press esacpe, only single message box is shown.    Could you somehow modify any sample and provide the code?
Apr 21, 2009 at 9:48 AM
Edited Apr 21, 2009 at 2:32 PM
Hi.
I've found this problem occurs in ComboBox. I've changed your example and pasted it into the trunk of example 17:

for (int r = 1; r < grid1.RowsCount; r++)
            {
                grid1[r,0] = new SourceGrid.Cells.RowHeader("Header " + r.ToString());
                for (int c = 1; c < grid1.ColumnsCount; c++)
                {
                    if (rnd.NextDouble() > 0.20)
                    {
                        grid1[r,c] = new SourceGrid.Cells.Cell(r*c, typeof(int));
                        
                        SourceGrid.Cells.Editors.ComboBox textbox = new SourceGrid.Cells.Editors.ComboBox(typeof(string));
                        grid1[r,c].Editor = textbox;
                        textbox.Control.Validating += delegate(object csender, CancelEventArgs ce)
                        {
                            System.Windows.Forms.MessageBox.Show("should appear twice, but does not");
                            ce.Cancel = true;
                        };
                        
                    }
                    else
                        grid1[r,c] = null;
                }
            }


...and I've got the second message...

What about the textBox - this error doesn't have a plase, it's true, but, as I've wrote at the beginning of this discussion, Enter key is processed twice on the cell which doesn't have an editor (I've checked it on your's example 19)...
Coordinator
Apr 21, 2009 at 6:24 PM
Thanks! I've got the double message box with your sample.   I will try to debug to see what's happening.




May 5, 2009 at 7:16 AM
Hi again.
Have you tried to check key processing?
Please, let me know if you find how to avoid this problem...
Thanks.
Coordinator
May 13, 2009 at 1:22 PM

No, i still cant reproduce it.

Could you reach me on skype?  My skype is "darius.damalakas"    When you add me as a contact, please write that you want help for SourceGrid

Then we could find some time  to work together and see how to reproduce this.

May 14, 2009 at 8:38 AM

Hi.

But, 2 posts ago you've wrote you've got the double message box.... Have you tried to debug key processing?....

And I've wrote in my post of Apr 3 at 3:24 PM where is the problem, IMHO.

Unfortunately, I don't have skype on my workplace... But, if it's really necessary, I'll install it and try to reach you...

Thanks.<abbr />

Coordinator
May 15, 2009 at 10:53 AM

Yes, i actually did write two posts before that i got message box twice. But i tried to reproduce it wednesday, and i had no luck!   So i want to be sure that i do everything correctly to get this problem.