Inhalt

Aktueller Ordner: duesseldorfer-schuelerinventar-csharp-client/Duesk/Forms
⬅ Übergeordnet

LoginForm.cs

using Duesk.Api;
using Duesk.Api.Models;

namespace Duesk.Forms;

public partial class LoginForm : Form
{
    private TextBox txtUsername;
    private TextBox txtPassword;
    private Button btnLogin;
    private Button btnCancel;
    private Label lblTitle;
    private Label lblInfo;
    
    public LoginForm()
    {
        InitializeComponent();
    }
    
    private void InitializeComponent()
    {
        this.Text = "DÜSK - Anmeldung";
        this.Size = new Size(400, 250);
        this.StartPosition = FormStartPosition.CenterScreen;
        this.FormBorderStyle = FormBorderStyle.FixedDialog;
        this.MaximizeBox = false;
        
        lblTitle = new Label
        {
            Text = "DÜSK - Düsseldorfer Schülerinventar",
            Font = new Font("Segoe UI", 14, FontStyle.Bold),
            TextAlign = ContentAlignment.MiddleCenter,
            Location = new Point(20, 20),
            Size = new Size(345, 30)
        };
        
        var lblUsername = new Label
        {
            Text = "Benutzername:",
            Location = new Point(50, 70),
            Size = new Size(80, 25)
        };
        
        txtUsername = new TextBox
        {
            Text = "gast",
            Location = new Point(140, 70),
            Size = new Size(180, 25)
        };
        
        var lblPassword = new Label
        {
            Text = "Passwort:",
            Location = new Point(50, 105),
            Size = new Size(80, 25)
        };
        
        txtPassword = new TextBox
        {
            Text = "gast",
            Location = new Point(140, 105),
            Size = new Size(180, 25),
            PasswordChar = '*'
        };
        
        btnLogin = new Button
        {
            Text = "Anmelden",
            Location = new Point(140, 145),
            Size = new Size(85, 30),
            BackColor = Color.FromArgb(102, 153, 204),
            ForeColor = Color.White,
            FlatStyle = FlatStyle.Flat
        };
        btnLogin.Click += BtnLogin_Click;
        
        btnCancel = new Button
        {
            Text = "Abbrechen",
            Location = new Point(235, 145),
            Size = new Size(85, 30)
        };
        btnCancel.Click += (s, e) => Application.Exit();
        
        lblInfo = new Label
        {
            Text = "Server: paul-koop.org | Benutzung mit gast/gast möglich",
            Font = new Font("Segoe UI", 9, FontStyle.Italic),
            ForeColor = Color.Gray,
            Location = new Point(20, 190),
            Size = new Size(345, 20),
            TextAlign = ContentAlignment.MiddleCenter
        };
        
        this.Controls.AddRange(new Control[] {
            lblTitle, lblUsername, txtUsername, lblPassword, txtPassword,
            btnLogin, btnCancel, lblInfo
        });
    }
    
    private async void BtnLogin_Click(object? sender, EventArgs e)
    {
        btnLogin.Enabled = false;
        Cursor = Cursors.WaitCursor;
        
        try
        {
            var response = await ApiClient.LoginAsync(txtUsername.Text.Trim(), txtPassword.Text.Trim());
            
            if (response != null && (response.Success || !string.IsNullOrEmpty(response.UserId)))
            {
                SessionManager.Instance.SetSession(response.UserId!, response.Session!);
                OpenMainForm();
            }
            else
            {
                MessageBox.Show($"Anmeldung fehlgeschlagen: {response?.Error ?? "Unbekannter Fehler"}",
                    "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnLogin.Enabled = true;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show($"Verbindungsfehler: {ex.Message}", "Fehler",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            btnLogin.Enabled = true;
        }
        finally
        {
            Cursor = Cursors.Default;
        }
    }
    
    private void OpenMainForm()
    {
        var mainForm = new MainForm();
        mainForm.Show();
        this.Hide();
    }
}