LevelOS-Core/TLL/FormHelper.cs

73 lines
3.7 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
namespace UI
{
/// <summary>
/// Abre qualquer UserControl dentro de um Form genérico,
/// sem precisar criar uma classe Form para cada tela.
/// </summary>
public static class FormHelper
{
// ══════════════════════════════════════════════════════════════════════
// ABRE UM USERCONTROL GENÉRICO
// Uso: FormHelper.Show<AgendaCadastroPanel>("Agenda");
// ══════════════════════════════════════════════════════════════════════
public static void Show<TPanel>(
string titulo,
int width = 1150,
int height = 780,
Action<TPanel>? configure = null) // opcional: configurar o painel antes de abrir
where TPanel : UserControl, new()
{
using var form = BuildForm(titulo, width, height);
var panel = new TPanel();
configure?.Invoke(panel);
form.Controls.Add(panel);
form.ShowDialog();
}
// ══════════════════════════════════════════════════════════════════════
// ABRE UM USERCONTROL JÁ INSTANCIADO (útil quando precisa passar dados)
// Uso: FormHelper.Show(new AgendaCadastroPanel(), "Agenda");
// ══════════════════════════════════════════════════════════════════════
public static void Show(
UserControl panel,
string titulo,
int width = 1150,
int height = 780)
{
using var form = BuildForm(titulo, width, height);
form.Controls.Add(panel);
form.ShowDialog();
}
// ══════════════════════════════════════════════════════════════════════
// ABRE UM FORM QUALQUER (mantém compatibilidade com o showForms<T>)
// Uso: FormHelper.ShowForm<AgendaForm>();
// ══════════════════════════════════════════════════════════════════════
public static void ShowForm<TForm>()
where TForm : Form, new()
{
using var form = new TForm();
form.StartPosition = FormStartPosition.CenterScreen;
form.ShowDialog();
}
// ── BUILDER INTERNO ───────────────────────────────────────────────────
private static Form BuildForm(string titulo, int width, int height)
{
return new Form
{
Text = titulo,
Size = new Size(width, height),
StartPosition = FormStartPosition.CenterScreen,
BackColor = Color.White,
MinimumSize = new Size(800, 500),
Icon = System.Drawing.Icon.ExtractAssociatedIcon(
System.Reflection.Assembly.GetEntryAssembly()!.Location)
};
}
}
}