I need to make some controls static, for example:
private static System.Windows.Forms.TextBox infoBox;
infoBox = new System.Windows.Forms.TextBox();
so I will able to use it in other class:
string myInfo = CourseWork.Form1.infoBox.Text;
But when I use visual designer, visual studio changes my code to:
private System.Windows.Forms.TextBox infoBox; // it removes static
this.infoBox = new System.Windows.Forms.TextBox(); // and add .this
And then I’ve got next error:
An object reference is required for the non-static field,
method, or property 'CourseWork.Form1.infobox'
Is it possible to avoid this ? or maybe I’m doing something wrong ?
I believe the design is flawed. The infoBox belongs to the form, so objects outside the form should not be trying to access it.
It sounds like you need to add an accessor method to your form class, something like
GetText(), to provide visibility to your other objects without breaking the Law of Demeter.