textBox von FormA aus FormB ändern

Steffen C

Grünschnabel
Hallo,
ich habe ein grosses Problem mit meinen C# Klassen.
Ich habe eine FormA, von dort aus Betätige ich den Buttonoeffnen.
Dann sollte sich FormB öffnen.

Bei FormB betätige ich den ButtonSpeichern. Anschliessed sollte in
der TextBox von FormA "Test" erscheinen.
Nur Leider klappt das nicht. Die TextBox von FormA bleibt immer leer.
Weiss jemand Rat ?


public class FormA : System.Windows.Forms.Form
{
public Buttonoeffnen()
{
FormB FB = new FormB();
FB.Show();
}

public TextBox(string von FormB)
{
this.textBox.Text=vonFormB;
}
}


public class FormB : System.Windows.Forms.Form
{
public ButtonSpeichern()
{
FormA FA = new FormA();
FA.TextBox("Test");
}
}
 
Hi,

du musst deinem Öffnen-Befehl der FormB die Instanz der FormA übergeben anstatt innerhalb von FormB eine neue FormA zu erstellen, so wie du es gemacht hast.
Code:
public class FormA : System.Windows.Forms.Form
{
public Buttonoeffnen()
{
FormB FB = new FormB(this);
FB.Show();
}

public void TextBox(string von FormB)
{
this.textBox.Text=vonFormB;
}
}


public class FormB : System.Windows.Forms.Form
{
FormA FA;
public FormB(FormA formA)
{
this.FA = formA;
}
public void ButtonSpeichern()
{
this.FA.TextBox("Test");
}
}

Ist nicht getestet, müsste aber gehen ..

MfG
 
Zurück