Kann ich nach der Rückkehr von Showdialog auf das Formular zugreifen? [geschlossen]
Posted: 16 May 2025, 18:04
Ich verwende einen Werks-Methoden-Ansatz in einer Form wie folgt: < /p>
Ich frage mich, ob es sich um einen guten und gültigen Ansatz handelt, um auf die Formulare zuzugreifen. MyForm -Form, also würde ich darauf zugreifen, dass im Ui-Thread nach Showdialog zurückgegeben wird. Ist das problematisch?
Code: Select all
// MyForm.cs
private MyForm() { }
private string Foo()
{
// this would be called after ShowDialog returned
string s = "";
foreach (Label l in MyLabels)
s += l.Text;
}
// this would be called after ShowDialog returned
private int Bar => label1.Text.Length;
public static bool ShowMyForm(out string s, out int i)
{
using (MyForm f = new MyForm())
{
bool ok = f.ShowDialog() == DialogResult.OK;
// accessing Controls of f after ShowDialog returned
s = f.Foo();
i = f.Bar;
return ok; // User clicked OK or Cancel
}
}
// Other Form
void CallForm()
{
if (MyForm.ShowMyForm(out string s, out int i)
{
if (s == ...) { }
if (i == ...) { }
}
}