Hallo,
ich habe mir das jetzt von
www.bitel.net/dghm11...ion_von_2_Forms.html
mehrmals durchgelesen aber ich habe immer noch einen Fehler und ich weiß nicht warum.
Also ich habe 2 Formen Form1.cs und SubForm.cs und es nur eine Variablenübergabe geschehen.
Form1 hat folgenden Code:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class MainForm : Form { public MainForm() { InitializeComponent(); }
void button_Click(object sender, EventArgs e) { SubForm subForm = new SubForm(); subForm.UpdateText += UpdateLabelText; subForm.ShowDialog(this); }
void UpdateLabelText(object sender, SubForm.TextEventArgs ed) { MessageBox.Show(ed.Text); }
} } |
Die SubForm.cs hat folgenden Code:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46:
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class SubForm : Form { public SubForm() { InitializeComponent(); }
public class TextEventArgs : EventArgs { public TextEventArgs(string text) { Text = text; }
public string Text { get; set; } }
public event EventHandler UpdateText; void button_Click(object sender, EventArgs e) { OnUpdateText(new TextEventArgs("Test")); }
protected virtual void OnUpdateText(TextEventArgs e) { EventHandler ev = UpdateText; if (ev != null) ev(this, e); } }
} |
Beim klick auf den Button in der Form1.cs, wird eine Funktion in der Form1.cs aufgerufen das sich eine MessageBox öffnet.
Allerdings wird bei
C#-Quelltext
1:
| subForm.UpdateText += UpdateLabelText; |
in der Form1 wird der fehler gemeldet:
Fehler 1 Keine Überladung für "UpdateLabelText" stimmt mit dem Delegaten "System.EventHandler" überein. *********\WindowsFormsApplication1\Form1.cs 24 39 WindowsFormsApplication1
Was bedeutet das?