简体   繁体   中英

Why is my event handler only entered once?

I have my form's KeyPreview set to true.

I'm trying to move to the next cell down if a DataGridView cell already has a value. I've got this code:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
    if (this.ActiveControl == dataGridViewPlatypus)
    {
        var currentCell = dataGridViewPlatypus.CurrentCell;
        if (currentCell.Value.ToString().Length == 1) 
        {
            ;//Now what?
            SendKeys.Send("{DOWN}");
        }
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

With a breakpoint set on the inner "if" I get there the first time I enter a value in a cell (presumably it is either length 0 or length 1, but looking at the value of currentCell at that point, it's "formatted text" value == "" and I don't see any other likely properties for discerning what the cell contents are).

The "if" fails, and it drops to the "return base." line.

When I enter a second value in the cell (what is visible in the cell changes from "2" to "22" for example), I don't even reach the breakpoint. Why???

Note: This is a variation on a question I asked here: How can I programmatically move from one cell in a datagridview to another?

Right now you're testing if the text representation of the value in that box has a length of exactly one character.

If I understand your question correctly you should try currentCell.Value.ToString().Length > 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM