I want to access Form1 elements from another class file (chartscopier.cs for example), but I can't change textbox1 text from chartscopier.cs.
How can I do this?
Here is my code:
Form1.cs
namespace TEST
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var CopyCharts = new System.Threading.Timer(chartscopier.CopyGraph, null, 0, 60000);
}
}
}
chartscopier.cs
namespace TEST
{
class chartscopier
{
//var Timer = new Timer(CopyGraph, null, 0, 60000);
public static void CopyGraph(object data)
{
Stopwatch strTimer = new Stopwatch();
WebClient WC = new WebClient();
IConfigSource BaseConfig = new IniConfigSource(@"D:\NEWROBOT\CONFIG.ini");
string LogDir = BaseConfig.Configs["GENERAL"].Get("Log Dir");
string ImgDir = BaseConfig.Configs["GENERAL"].Get("IMG Dir");
string[] Clients = BaseConfig.Configs["CLIENTS"].GetKeys();
foreach (string Client in Clients)
{
strTimer.Start();
//Console.WriteLine(Client);
IConfigSource ClientConfig = new IniConfigSource(@"D:\NEWROBOT\" + Client + ".ini");
string[] Services = ClientConfig.Configs["SERVICES"].GetKeys();
foreach (string Service in Services)
{
string url = BaseConfig.Configs["CLIENTS"].Get(Client);
string param = ClientConfig.Configs["SERVICES"].Get(Service);
string html = WC.DownloadString(url + param);
// Cargar doc en HPACK
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
// LINQ para generar imagenes
var img = doc.DocumentNode.SelectSingleNode("//img[1]");
var src = img.Attributes["src"].Value;
string uIMG = url + src;
WC.DownloadFile(uIMG, @ImgDir + Client + "\\" + Service + ".png");
}
strTimer.Stop();
TimeSpan ts = strTimer.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
strTimer.Reset();
// WANT TO WRITE HERE Form1.TextBox1.text = "RunTime " + elapsedTime;
}
}
}
}
You need to pass the form to that function so you have access to it, then write a public function on the form to modify/get the text box. Make sure the two functions are checking if invoke is required.
You need to pass a reference you want
chartscopier.CopyGraph
to manipulate directly to the method so it can be within scope. YourCopyGraph(object data)
signature should be more likeCopyGraph(object data, TextBox aTextBox)
Then you can call it from your Form1's instance like
I think, a better way is to handle through events.
Create a class to represent data that you want to pass.
In "chartscopier" create an event to report progress.
Then in Form1, create a handler for that event.
This example will show you how to access form elements from other class:
And in other class..