public class TicketDetailviewController : ViewController
{
public TicketDetailviewController() : base()
{
TargetObjectType = typeof(ITicket);
TargetViewType = DevExpress.ExpressApp.ViewType.DetailView;
TargetViewNesting = Nesting.Root;
}
protected override void OnActivated()
{
base.OnActivated();
if (View.CurrentObject is ITicket ticket)
{
View.Caption = ticket.Subject;
}
}
protected override void OnViewControlsCreated()
{
base.OnViewControlsCreated();
if (View.ViewEditMode != ViewEditMode.Edit) return;
if (View.CurrentObject is ITicket ticket)
{
// Standaard word de eerste editor geselecteerd (met alle tekst die er in staat) waardoor je meteen begint met het verwijderen van het onderwerp als je gaat typen.
// Dus de focus verplaatsen naar de volgende editor (deze heeft meteen ook niet alles selected)
if (!string.IsNullOrWhiteSpace(ticket.Subject))
{
((Control)View.Control).HandleCreated += TicketDetailviewController_HandleCreated;
}
}
}
private void TicketDetailviewController_HandleCreated(object sender, EventArgs e)
{
if (View.Control is ContainerControl control)
{
control.HandleCreated -= TicketDetailviewController_HandleCreated;
control.BeginInvoke(new Action(() =>
{
var activeControl = control.ActiveControl;
var nextFocusableControl = GetNextFocusableControl(control);
nextFocusableControl.Focus();
if (nextFocusableControl is TextEdit editor)
{
editor.SelectionLength = 0;
}
}));
}
}
private static Control GetNextFocusableControl(ContainerControl parentControl)
{
var start = parentControl.ActiveControl ?? parentControl;
var current = parentControl.GetNextControl(start, true);
while (current != null && (!current.CanFocus || !current.Enabled || !current.Visible))
{
current = parentControl.GetNextControl(current, true);
}
return current;
}
} 1006300cookie-checkXAF / Windows forms control focus (prevent deleting text on keydown)