C# Windows Form application ... accessing controls from different class

Hello,

I am working on Forms application if C# Visual studio and have the following problem.

First i have a Class :
public partial class Form1 : Form
    {
        public static Form1 frm;
       
        Thread StartB = new Thread(() => Program.Start(frm));

      
        public Form1()
        {
            InitializeComponent();

            textUsername.Text = Properties.Settings.Default.UserName;
            textPassword.Text = Properties.Settings.Default.Password;
                     
        }
		
		 private void Login_Click(object sender, EventArgs e)
        {
            Start.Start();
        }
	}

Open in new window


Now i have a seperate class:
 public static void Start(Form1 frm)
        {
            frm.Logger.Items.Add("Logging info test"); 
		}

Open in new window


So first problem i had was GUI freezing when i called my Start method. I have solved it by executing it in another thread.
Now i want to populate my ListBox with "log information". What does that mean is i want to log what is happening in my application.

Each method that will execute will be logged in my ListBox control.

Question is why doesnt this work?

Thank you!
andy gehoxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Maybe I'm having a lack of coffee but I can't make sense of what your current code is doing or what it should be doing (according to the text of your question).
andy gehoxAuthor Commented:
Hmmm let me explain a bit btter then.

I have a form application ... with ListBox on it.

Now i have few other classes that are doing some background jobs.

What i want is to display "log" information on ListBox.

So basically i do not know hot to access ListBox outside of Form1 class ....
AndyAinscowFreelance programmer / ConsultantCommented:
I understand what you want.  However I can't see any link between that and the code you gave - nor do I understand just what that code should do.  (Maybe that is why your current code is not doing what you want).
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

AndyAinscowFreelance programmer / ConsultantCommented:
New project with two forms:
Form1code:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2(this);
            frm.ShowDialog();
        }

        public void DisplayMessage(String s)
        {
            this.listBox1.Items.Add(s);
        }
    }

Open in new window


and
Form2
    public partial class Form2 : Form
    {
        private Form1 frmMain = null;

        public Form2(Form1 frm)
        {
            frmMain = frm;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmMain.DisplayMessage("Button 1 clicked");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            frmMain.DisplayMessage("now for Button 2");

        }

        private void button3_Click(object sender, EventArgs e)
        {
            frmMain.DisplayMessage("Button 3 has been clicked");
        }
    }

Open in new window


That is showing how one can pass information between forms.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
I think something like this is what you were after?...
    public partial class Form1 : Form
    {

        Thread StartB;

        public Form1()
        {
            InitializeComponent();

            textUsername.Text = Properties.Settings.Default.UserName;
            textPassword.Text = Properties.Settings.Default.Password;
        }

        private void Login_Click(object sender, EventArgs e)
        {
            if (StartB == null)
            { 
                StartB = new Thread(() => Program.Start(this));
                StartB.Start();
            }
        }

    }

Open in new window


In your other class that is running the threaded code:
        public static void Start(Form1 frm)
        {
            for (int i = 1; i <= 12; i++ )
            {
                System.Threading.Thread.Sleep(500); // .... some simulated "work" ...

                // update the form somehow; this must be done from the main UI thread
                // one way to do this is to Invoke() it via our "frm" reference:
                frm.Invoke((MethodInvoker)delegate {
                    frm.Logger.Items.Add(i.ToString("00") + ": Logging info test @" + DateTime.Now.ToString());
                });
            }
        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
andy gehoxAuthor Commented:
Exelent responses i have solved my problem.

Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.