When using a basic form application in C# I am having trouble accessing the variabels within it.
So with in the form class I have
public partial class pingerform : Form
{
..
..
private System.Windows.Forms.TextBox textBox2;
public string textBox2Text
{
get { return textBox2.Text; }
set { textBox2.Text = value; }
}
And then in the main application I have
Application.Run(new pingerform());
...
...
pingerform.textBox2Text.text() = str;
but I am told that there is no object reference.
Error 1
An object reference is required for the non-static field,
method, or property
‘pingerform.textBox2Text.get’ C:\Users\aaron.street\Documents\Visual
Studio 11\Projects\PingDrop\PingDrop\Program.cs 54 21 PingDrop
So I thought I would make the pinger form class static but it wont let me do this?
Error 1
Cannot create an instance of the static class
‘PingDrop.pingerform’ C:\Users\aaron.street\Documents\Visual Studio
11\Projects\PingDrop\PingDrop\Program.cs 21 29 PingDrop
How can I access the forms properties with out creating a specific instance of the form,
I have a background thread running that I want to update a text filed with in the form at regular intervals?
Cheers
Aaron
You have no choice but to create new instance and either pass it as parameter to the thread, or store it as member of your main Program class.
Example for the second option:
And don’t forget to change the textbox to be
public.Note: this is simple working solution to the simple case of one background thread accessing the textbox. If you have more threads accessing it, this will break. For best practice methods that require some more work, please read this.