Files
flowframes/CodeLegacy/Forms/CustomForm.cs
2024-11-26 11:45:21 +01:00

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);
}
}
}