I'm working on a C# program, and right now I have one Form
and a couple of 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 TextBox
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?
I'm relatively new to c# and brand new to stackoverflow. Anyway, regarding the question on how to access controls on a form from a class: I just used the ControlCollection (Controls) class of the form.
//Add a new form called frmEditData to project.
//Draw a textbox on it named txtTest; set the text to
//something in design as a test.
Form frmED = new frmEditData();
MessageBox.Show(frmED.Controls["txtTest"].Text);
Worked for me, maybe it will be of assistance in both questions.
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"
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
JUST YOU CAN SEND FORM TO CLASS LIKE THIS
Class1 excell = new Class1 (); //you must declare this in form as you want to control
excel.get_data_from_excel(this); // And create instance for class and sen this form to another class
INSIDE CLASS AS YOU CREATE CLASS1
class Class1
{
public void get_data_from_excel (Form1 form) //you getting the form here and you can control as you want
{
form.ComboBox1.text = "try it"; //you can chance Form1 UI elements inside the class now
}
}
IMPORTANT : But you must not forgat you have declare modifier form properties as PUBLIC and you can access other wise you can not see the control in form from class
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"
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();
}
}
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";
}
}
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();
}
}
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";
}
}
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";
}
}
JUST YOU CAN SEND FORM TO CLASS LIKE THIS
Class1 excell = new Class1 (); //you must declare this in form as you want to control
excel.get_data_from_excel(this); // And create instance for class and sen this form to another class
INSIDE CLASS AS YOU CREATE CLASS1
class Class1
{
public void get_data_from_excel (Form1 form) //you getting the form here and you can control as you want
{
form.ComboBox1.text = "try it"; //you can chance Form1 UI elements inside the class now
}
}
IMPORTANT : But you must not forgat you have declare modifier form properties as PUBLIC and you can access other wise you can not see the control in form from class
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
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
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"
I'm relatively new to c# and brand new to stackoverflow. Anyway, regarding the question on how to access controls on a form from a class: I just used the ControlCollection (Controls) class of the form.
//Add a new form called frmEditData to project.
//Draw a textbox on it named txtTest; set the text to
//something in design as a test.
Form frmED = new frmEditData();
MessageBox.Show(frmED.Controls["txtTest"].Text);
Worked for me, maybe it will be of assistance in both questions.
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";
}
}
Source: Stackoverflow.com