Using a delegate to populate a listbox
        Posted  
        
            by Leroy Jenkins
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Leroy Jenkins
        
        
        
        Published on 2010-04-14T04:02:07Z
        Indexed on 
            2010/04/14
            4:03 UTC
        
        
        Read the original article
        Hit count: 318
        
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?
© Stack Overflow or respective owner