Using a delegate to populate a listbox
- by Leroy Jenkins
Ive been playing around with delegates trying to learn and I ran into one small problem Im hoping you can help me with.
class myClass
{
OtherClass otherClass = new OtherClass(); // Needs Parameter
otherClass.SendSomeText(myString);
}
class OtherClass
{
public delegate void TextToBox(string s);
TextToBox textToBox;
public OtherClass(TextToBox ttb) // ***Problem***
{
textToBox = ttb;
}
public void SendSomeText(string foo)
{
textToBox(foo);
}
}
the form:
public partial class MainForm : Form
{
OtherClass otherClass;
public MainForm()
{
InitializeComponent();
otherClass = new OtherClass(this.TextToBox);
}
public void TextToBox(string aString)
{
listBox1.Items.Add(aString);
}
}
Obviously this doesnt compile because the OtherClass constructor is looking for TextToBox as a parameter. How would you recommend getting around the issue so I can get an object from myClass into the textbox in the form?