tags:

views:

15810

answers:

9

I'm working on a program in C#, and right now I have one form and a couple classes. I would like to be able to access some of the form controls(such as a textbox) from my class. When I try to change the text in the text box from my class I get the following error:

An object reference is required for the non-static field, method, or property 'Project.Form1.txtLog'

How can I access methods and controls that are in Form1.cs from one of my classes?

+2  A: 
  1. you have to have a reference to the form object in order to access its elements
  2. the elements have to be declared public in order for another class to access them
  3. don't do this - your class has to know too much about how your form is implemented; do not expose form controls outside of the form class
  4. instead, make public properties on your form to get/set the values you are interested in
  5. post more details of what you want and why, it sounds like you may be heading off in a direction that is not consistent with good encapsulation practices
Steven A. Lowe
+2  A: 

You need access to the object.... you can't simply ask the form class....

eg...

you would of done some thing like

Form1.txtLog.Text = "blah"

instead of

Form1 blah = new Form1();
blah.txtLog.Text = "hello"
Keith Nicholas
+12  A: 

You are trying to access the class as opposed to the object. That statement can be confusing to beginners, but you are effectively trying to open your house door by picking up the door on your house plans.

If you actually wanted to access the form components directly from a class (which you don't) you would use the variable that instantiates your form.

Depending on which way you want to go you'd be better of either sending the text of a control or whatever to a method in your classes eg

public void DoSomethingWithText(string formText)
{
   // do something text in here
}

or exposing properties on your form class and setting the form text in there - eg

string SomeProperty
{
   get 
   {
      return textBox1.Text;
   }
   set
   {
      textBox1.Text = value;
   }
}
KiwiBastard
A: 

You need to make the members in the for the form class either public or, if the service class is in the same assembly, internal. Windows controls' visibility can be controlled through their Modifiers properties.

Note that it's generally considered a bad practice to explicitly tie a service class to a UI class. Rather you should create good interfaces between the service class and the form class. That said, for learning or just generally messing around, the earth won't spin off its axis if you expose form members for service classes.

rp

rp
+3  A: 

Another solution would be to pass the textbox (or control you want to modify) into the method that will manipulate it as a parameter.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        TestClass test = new TestClass();
        test.ModifyText(textBox1);
    }
}

public class TestClass
{
    public void ModifyText(TextBox textBox)
    {
        textBox.Text = "New text";
    }
}
Timothy Carter
A: 
    public System.Windows.Forms.TabPage xLog;
    public System.Windows.Forms.TextBox xLogBox;

I've a Form and a class and in designer.cs I put some controls public.

I can access to form in class constructor, but not outside of it

public class myDevice
{
    public myDevice(TcpListener server, ref System.Windows.Forms.TextBox logBox)
    {
        logBox.Text += " connection begins "; // HERE WORKS ! 
    }

    logBox. ? // there is not logBox !
    private void addTime()
    {
        logBox. ?  // there is not logBox
    }
}

So how can I access to my Form or how can I send all my Form as reference to my class ?

yshuditelu 's answer is not completely true, because my class is not written in Form1.cs and I do not want to write my class in Form1.cs

Cmptrb
A: 

Oh man, I think the guy is trying to do the same thing as I try:

public partial class frmGraph : Form {
  public void DoSomethingToTheGraph() {
   //do something
  }
}

and from another class

frmGraph f = new frmGraph();
f.DoSomethingToTheGraph();

How we can achieve this ?

Ovidiu
A: 

Hi Guys,

If the form starts up first, in the form Load handler we can instantiate a copy of our class. We can have properties that reference whichever controls we want to reference. Pass the reference to the form 'this' to the constructor for the class.

public partial class Form1 : Form
{
    public ListView Lv
    {
        get { return lvProcesses; }
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Utilities ut = new Utilities(this);
    }
}

In your class, the reference from the form is passed into the constructor and stored as a private member. This form reference can be used to access the form's properties.

class Utilities
{
    private Form1 _mainForm;
    public Utilities(Form1 mainForm)
    {
        _mainForm = mainForm;
        _mainForm.Lv.Items.Clear();
    }
}
Ojhnny777
A: 

I have a Question:

is it possible to set the property of a textbox from string to integer? what I mean is that, for example, I have a textbox for Employee Number and it takes only integer. then I want to access it from another form so I should make a property for it? then how do I get that the property would return an integer already, not a string?

madz