Mike

Mike

  • NA
  • 1
  • 0

Best Way To Access Form Controls From Classes

Mar 5 2006 10:43 AM

Here is a demonstration of my problem. I am able to do this, but I am just wondering the BEST way to do this because the way I am doing it does not seem like the correct way.

How I am doing it now: I am passing the instance of Form1 through each constructor with a private variable in each class "private Form1 m_frm"

In the second section shows how I am currently doing it now.

public class Form1 : System.Windows.Forms.Form
{
    public listBox1;
 
    public Form1()
    {
    }  
}

public class myClass
{
    public myClass()
    {
    }

    public void myClassFunction()
    {
        mySubClass subc = new mySubClass();
        subc.myFunction();
    }

    private class mySubClass
    {
        public mySubClass()
        {
        }
  
        public void myFunction()
        {
            // i need to do this 
            Form1.listBox1.Items.Add("blah");
        }
    }
}

---------------------------------------------------------------------------------

public class Form1 : System.Windows.Forms.Form
{
    public listBox1;
 
    public Form1()
    {
        myClass mc = new myClass(this);
    }  
}

public class myClass
{
    private Form1 m_frm;

    public myClass(Form1 frm)
    {
        m_frm = frm;        
    }

    public void myClassFunction()
    {
        mySubClass subc = new mySubClass(m_frm);
        subc.myFunction();
    }

    private class mySubClass
    {
        private Form1 frm;

        public mySubClass(Form1 frm)
        {
            m_frm = frm;
        }
  
        public void myFunction()
        {
              m_frm.listBox1.Items.Add("blah");
        }
    }
}


Thanks for the help, there HAS to be a better way