mirror of
https://github.com/n00mkrad/flowframes.git
synced 2025-12-16 16:37:48 +01:00
83 lines
2.5 KiB
C#
83 lines
2.5 KiB
C#
using Flowframes.Ui;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Flowframes.Forms
|
|
{
|
|
public class CustomForm : Form
|
|
{
|
|
public Control FocusedControl { get { return this.GetControls().Where(c => c.Focused).FirstOrDefault(); } }
|
|
public List<Control> AllControls => GetAllControls(this);
|
|
|
|
public bool AllowTextboxTab { get; set; } = true;
|
|
public bool AllowEscClose { get; set; } = true;
|
|
|
|
private List<Control> _tabOrderedControls;
|
|
|
|
private static List<Control> GetAllControls(Control parent)
|
|
{
|
|
var controls = new List<Control>();
|
|
|
|
foreach (Control ctrl in parent.Controls)
|
|
{
|
|
controls.Add(ctrl);
|
|
|
|
if (ctrl.HasChildren)
|
|
{
|
|
controls.AddRange(GetAllControls(ctrl));
|
|
}
|
|
}
|
|
|
|
return controls;
|
|
}
|
|
|
|
public void TabOrderInit(List<Control> tabOrderedControls, int defaultFocusIndex = 0)
|
|
{
|
|
_tabOrderedControls = tabOrderedControls;
|
|
this.GetControls().ForEach(control => control.TabStop = false);
|
|
|
|
if (defaultFocusIndex >= 0 && tabOrderedControls != null && tabOrderedControls.Count > 0)
|
|
tabOrderedControls[defaultFocusIndex].Focus();
|
|
}
|
|
|
|
public void TabOrderNext()
|
|
{
|
|
if (_tabOrderedControls == null || _tabOrderedControls.Count <= 0)
|
|
return;
|
|
|
|
var focused = FocusedControl;
|
|
|
|
if (_tabOrderedControls.Contains(focused))
|
|
{
|
|
int index = _tabOrderedControls.IndexOf(focused);
|
|
Control next = null;
|
|
|
|
while (_tabOrderedControls.Where(x => x.Visible && x.Enabled).Any() && (next == null || !next.Visible || !next.Enabled))
|
|
{
|
|
index++;
|
|
next = _tabOrderedControls.ElementAt(index >= _tabOrderedControls.Count ? 0 : index);
|
|
}
|
|
|
|
if (next != null)
|
|
next.Focus();
|
|
|
|
return;
|
|
}
|
|
|
|
_tabOrderedControls.First().Focus();
|
|
}
|
|
|
|
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
|
{
|
|
if (keyData == Keys.Escape && AllowEscClose)
|
|
Close();
|
|
|
|
if (keyData == Keys.Tab && !(FocusedControl is TextBox && AllowTextboxTab))
|
|
TabOrderNext();
|
|
|
|
return base.ProcessCmdKey(ref msg, keyData);
|
|
}
|
|
}
|
|
}
|