9
Reply

Accessing form control from another class

andy mcinnes

andy mcinnes

Apr 18 2012 7:14 AM
4.3k
Hi,

I am still rather newb with c# and I am just going through the painful task of translating my apps from vb.net to c#.net so I can integrate them all in to one easy to use app.

The problem I have never had before but seem to be having is accessing form controls from my classes.

I try to use a structure where by all database calls are in one class, all validation and calculations are in another class and then only the form controls are on the form code.

So an example of an issue I am having is that I read textbox values from the form and also do things like update the status strip progress bar etc.

I have even tried both with and without delegates and I simply can't manage it.

Usually if I want to access a form control I would reference a new form instance:

Form1 frm = new Form1();

and use it like this:

frm1.ToolStripProgressBar1.Value = 100;

If I do this on the form it works fine:

this.ToolStripProgressBar1.Value = 100;

But from another class just doesn't seem to work.

My guess is that I reference "= new Form1();"

Does this refer to a new instance of the form I am trying to access controls on and if so how do I access the current instance of a form?

I had been reading a little about IEnumerable<control> but I'm not sure if this is the right direction to go or how to implement it exactly?

Thanks in advance for any help!

Andy

Answers (9)