Inhalt
Aktueller Ordner:
duesseldorfer-schuelerinventar-csharp-client/Duesk/FormsProfileEditForm.cs
using Duesk.Api;
using Duesk.Api.Models;
namespace Duesk.Forms;
public partial class ProfileEditForm : Form
{
private Profile? _profile;
private TextBox txtName;
private ComboBox cboGroup;
private TextBox txtNewGroup;
private TabControl tabControl;
private Panel panelSe;
private Panel panelFe;
private Button btnSave;
private Button btnCancel;
private List<RadioButton[]> _seRadioGroups = new List<RadioButton[]>();
private List<RadioButton[]> _feRadioGroups = new List<RadioButton[]>();
private List<Group> _groups = new List<Group>();
public ProfileEditForm(Profile? profile)
{
_profile = profile;
InitializeComponent();
LoadGroups();
if (profile != null) LoadProfileData();
}
private void InitializeComponent()
{
this.Text = _profile == null ? "Neues Profil" : "Profil bearbeiten";
this.Size = new Size(950, 750);
this.StartPosition = FormStartPosition.CenterParent;
// Hauptpanel
var mainPanel = new TableLayoutPanel
{
Dock = DockStyle.Fill,
Padding = new Padding(10),
RowCount = 3
};
// Info-Panel
var infoPanel = new GroupBox { Text = "Profil-Informationen", Dock = DockStyle.Fill };
var infoLayout = new TableLayoutPanel { ColumnCount = 2, Dock = DockStyle.Fill, Padding = new Padding(10) };
infoLayout.Controls.Add(new Label { Text = "Name:", TextAlign = ContentAlignment.MiddleRight }, 0, 0);
txtName = new TextBox { Dock = DockStyle.Fill };
infoLayout.Controls.Add(txtName, 1, 0);
infoLayout.Controls.Add(new Label { Text = "Gruppe:", TextAlign = ContentAlignment.MiddleRight }, 0, 1);
cboGroup = new ComboBox { DropDownStyle = ComboBoxStyle.DropDownList, Dock = DockStyle.Fill };
infoLayout.Controls.Add(cboGroup, 1, 1);
infoLayout.Controls.Add(new Label { Text = "Neue Gruppe:", TextAlign = ContentAlignment.MiddleRight }, 0, 2);
txtNewGroup = new TextBox { Dock = DockStyle.Fill };
infoLayout.Controls.Add(txtNewGroup, 1, 2);
infoPanel.Controls.Add(infoLayout);
mainPanel.Controls.Add(infoPanel, 0, 0);
// TabControl für SE und FE
tabControl = new TabControl { Dock = DockStyle.Fill };
// Selbsteinschätzung
panelSe = CreateItemPanel("Selbsteinschätzung", _seRadioGroups);
tabControl.TabPages.Add(panelSe);
// Fremdeinschätzung
panelFe = CreateItemPanel("Fremdeinschätzung", _feRadioGroups);
tabControl.TabPages.Add(panelFe);
mainPanel.Controls.Add(tabControl, 0, 1);
// Button Panel
var buttonPanel = new FlowLayoutPanel { Dock = DockStyle.Fill, FlowDirection = FlowDirection.RightToLeft, Padding = new Padding(0, 10, 0, 0) };
btnSave = new Button { Text = "Speichern", Size = new Size(100, 30), BackColor = Color.FromArgb(76, 175, 80), ForeColor = Color.White, FlatStyle = FlatStyle.Flat };
btnSave.Click += BtnSave_Click;
btnCancel = new Button { Text = "Abbrechen", Size = new Size(100, 30) };
btnCancel.Click += (s, e) => DialogResult = DialogResult.Cancel;
buttonPanel.Controls.Add(btnCancel);
buttonPanel.Controls.Add(btnSave);
mainPanel.Controls.Add(buttonPanel, 0, 2);
this.Controls.Add(mainPanel);
}
private Panel CreateItemPanel(string title, List<RadioButton[]> radioGroups)
{
var panel = new Panel { Text = title };
var scrollPanel = new Panel { AutoScroll = true, Dock = DockStyle.Fill };
int yPos = 10;
for (int i = 1; i <= 36; i++)
{
var groupBox = new GroupBox
{
Text = $"{i}. {Norms.Items[i - 1]}",
Location = new Point(10, yPos),
Size = new Size(900, 60)
};
var radioButtons = new RadioButton[4];
int xPos = 10;
for (int val = 4; val >= 1; val--)
{
string label = val switch
{
4 => "trifft voll zu (4)",
3 => "trifft zu (3)",
2 => "trifft teilweise zu (2)",
_ => "trifft nicht zu (1)"
};
var rb = new RadioButton
{
Text = label,
Location = new Point(xPos, 20),
AutoSize = true,
Tag = val
};
if (val == 2) rb.Checked = true;
groupBox.Controls.Add(rb);
radioButtons[4 - val] = rb;
xPos += rb.Width + 10;
}
scrollPanel.Controls.Add(groupBox);
radioGroups.Add(radioButtons);
yPos += 70;
}
panel.Controls.Add(scrollPanel);
return panel;
}
private async void LoadGroups()
{
try
{
_groups = await ApiClient.GetGroupsAsync() ?? new List<Group>();
cboGroup.Items.Clear();
foreach (var group in _groups)
{
cboGroup.Items.Add(group.Name);
}
if (cboGroup.Items.Count > 0) cboGroup.SelectedIndex = 0;
}
catch { }
}
private void LoadProfileData()
{
if (_profile == null) return;
txtName.Text = _profile.Name;
// Gruppe auswählen
if (!string.IsNullOrEmpty(_profile.GroupName))
{
int index = cboGroup.FindStringExact(_profile.GroupName);
if (index >= 0) cboGroup.SelectedIndex = index;
}
// SE Werte laden
for (int i = 1; i <= 36; i++)
{
int value = _profile.GetItem(i);
var radios = _seRadioGroups[i - 1];
foreach (var rb in radios)
{
if ((int)rb.Tag == value)
{
rb.Checked = true;
break;
}
}
}
// FE Werte laden
for (int i = 1; i <= 36; i++)
{
int value = _profile.GetFeItem(i);
var radios = _feRadioGroups[i - 1];
foreach (var rb in radios)
{
if ((int)rb.Tag == value)
{
rb.Checked = true;
break;
}
}
}
}
private async void BtnSave_Click(object? sender, EventArgs e)
{
string name = txtName.Text.Trim();
if (string.IsNullOrEmpty(name))
{
MessageBox.Show("Bitte Name eingeben!", "Hinweis",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
var profile = _profile ?? new Profile();
profile.Name = name;
// Gruppe bestimmen
string newGroup = txtNewGroup.Text.Trim();
if (!string.IsNullOrEmpty(newGroup))
{
var success = await ApiClient.CreateGroupAsync(newGroup);
if (success)
{
await LoadGroupsAsync();
// Gruppe auswählen
for (int i = 0; i < _groups.Count; i++)
{
if (_groups[i].Name == newGroup)
{
profile.GroupId = _groups[i].GroupId.ToString();
profile.GroupName = newGroup;
break;
}
}
}
}
else if (cboGroup.SelectedItem != null)
{
string selectedGroup = cboGroup.SelectedItem.ToString()!;
for (int i = 0; i < _groups.Count; i++)
{
if (_groups[i].Name == selectedGroup)
{
profile.GroupId = _groups[i].GroupId.ToString();
profile.GroupName = selectedGroup;
break;
}
}
}
// SE Werte speichern
for (int i = 1; i <= 36; i++)
{
int value = 2;
foreach (var rb in _seRadioGroups[i - 1])
{
if (rb.Checked)
{
value = (int)rb.Tag;
break;
}
}
profile.SetItem(i, value);
}
// FE Werte speichern
for (int i = 1; i <= 36; i++)
{
int value = 2;
foreach (var rb in _feRadioGroups[i - 1])
{
if (rb.Checked)
{
value = (int)rb.Tag;
break;
}
}
profile.SetFeItem(i, value);
}
Cursor = Cursors.WaitCursor;
btnSave.Enabled = false;
bool success;
if (_profile == null)
success = await ApiClient.CreateProfileAsync(profile);
else
success = await ApiClient.UpdateProfileAsync(profile);
Cursor = Cursors.Default;
btnSave.Enabled = true;
if (success)
{
DialogResult = DialogResult.OK;
Close();
}
else
{
MessageBox.Show("Fehler beim Speichern des Profils!", "Fehler",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private async Task LoadGroupsAsync()
{
_groups = await ApiClient.GetGroupsAsync() ?? new List<Group>();
cboGroup.Items.Clear();
foreach (var group in _groups)
{
cboGroup.Items.Add(group.Name);
}
if (cboGroup.Items.Count > 0) cboGroup.SelectedIndex = 0;
}
}