2

I want Change mouse cursor position to focused control. I change focuses by keyboard(Enter key). How can I do this?

Siamak Ferdos
  • 3,181
  • 5
  • 29
  • 56

1 Answers1

4

Here you go:

void goToActive()
{
    Control ctl = this.ActiveControl;
    this.Cursor = new Cursor(Cursor.Current.Handle);
    if (ctl != null) Cursor.Position = ctl.PointToScreen(new Point(3,3));
}

To catch your navigation key from everywhere override ProcessCmdKey as show here..:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Enter) { goToActive(); return true;}
    return base.ProcessCmdKey(ref msg, keyData);
}

Update: If you'd rather not follow the Enter-Key but the Enter events of your controls, here is how to do that:

We register all controls in the Form.Shown event:

private void Form1_Shown(object sender, EventArgs e)
{
    registerAllControls(this);
}

This regisers all controls recursively. You may want to exlude some based on your needs, maybe checking the name, type or Tag..:

void registerAllControls(Control ctl)
{
    ctl.Enter += ControlReceivedFocus;
    foreach (Control ct in ctl.Controls)
    {
        registerAllControls(ct);
    }
}

We call the modified goToActive function only when we are not here already..:

void ControlReceivedFocus(object sender, EventArgs e)
{
    if (!((sender as Control).ClientRectangle
        .Contains(PointToClient(MousePosition))))
    {
        goToActive(sender);
    }
}

I have modified the function to include the calling control, to make things a little easier..:

void goToActive(object sender)
{
    Control ctl = sender as Control;
    this.Cursor = new Cursor(Cursor.Current.Handle);
    Cursor.Position = ctl.PointToScreen(new Point(3, 3));
    if (sender is TextBox) Cursor = Cursors.IBeam; 
    else Cursor = Cursors.Default;
}

Note that the Cursor has a tendency to pick up wrong shapes; I set it to Default or, for TextBoxes to IBeam.

I have tested it, it works, but, as noted, I'd rather not have my cursor track my focus.. Make it an option, not a feature!

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
  • How can I detect focus changing on a form to use this code? – Siamak Ferdos Jun 06 '15 at 10:50
  • By overriding [ProcessCmdKey](http://stackoverflow.com/questions/5951496/how-do-i-capture-keys-f1-regardless-of-the-focused-control-on-a-form).. – TaW Jun 06 '15 at 10:54
  • But it's on key pressing event and not focusing! Some where focus is changing when a condition in a `input` was true. Like in a `textBox` of year if characters be 4 digits it will be focus to next input automaticly – Siamak Ferdos Jun 06 '15 at 11:06
  • OK, you can look at [this](http://stackoverflow.com/questions/1159020/windows-forms-detect-the-change-of-the-focused-control) answer. Not accepted but very good imo.. (Although it is lacking the recursion, needed for nested controls!!) Or, of course, to get full control you could code the Enter events of all controls you want to participate.. - Oh, btw, are you sure your users will appreciate this in the first place? I doubt I would like to have the mouse jump into my fields..? – TaW Jun 06 '15 at 11:10