Setting Focus to a .NET UserControl…?

后端 未结 7 702
不思量自难忘°
不思量自难忘° 2020-12-18 23:16

I\'m creating a custom control derived from UserControl that I would like to set focus to.

The custom control contains a ComboBox control and I draw some strings bes

7条回答
  •  甜味超标
    2020-12-19 00:14

    Hans is right, a UserControl will do everything it can to hand off focus to a child control, expect in this one sneaky case:

    • All the children have Enabled == false

    What this means is that with a little prep-work, the UserControl will begrudgingly accept focus. Try something to this effect:

    var controls = this.Controls.Cast().ToList();
    controls.ForEach(control => control.Enabled = false);
    this.ActiveControl = null; //the UserControl will try to remember its ActiveControl
    this.Focus();
    controls.ForEach(control => control.Enabled = true);
    

提交回复
热议问题