从另一个类文件访问C#表单文本框
我想从另一个类文件(例如chartscopier.cs)访问Form1元素,但我无法更改chartscopier.cs中的textbox1文本。
我怎样才能做到这一点?
这是我的代码:
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; } } } }
我认为,更好的方法是处理事件。
创建一个类来表示要传递的数据。
public class ChartCopyProgressEventArgs : EventArgs { public TimeSpan ElapsedTime { get; set; } //you can add more prop.s here }
在“chartcopier”中创建一个报告进度的事件。
class chartscopier { public static event EventHandler Changed; public static void CopyGraph(object data) { ... if (Changed != null) { var args = new ChartCopyProgressEventArgs(); args.ElapsedTime = elapsedTime; Changed(null, args); } } }
然后在Form1中,为该事件创建一个处理程序。
public Form1() { InitializeComponent(); chartscopier.Changed += UpdateStatus; ... } private void UpdateStatus(object sender, ChartCopyProgressEventArgs e) { var ts = e.ElapsedTime; var elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); var str = "RunTime " + elapsedTime; if (TextBox1.InvokeRequired) { TextBox1.Invoke(() => {TextBox1.text = str;}); } else { TextBox1.text = str; } }
您需要将表单传递给该函数,以便您可以访问它,然后在表单上编写一个公共函数来修改/获取文本框。 确保这两个函数正在检查是否需要调用。
您需要传递一个引用,您希望chartscopier.CopyGraph
直接操作该方法,以便它可以在范围内。 你的CopyGraph(object data)
签名应该更像CopyGraph(object data, TextBox aTextBox)
然后你可以从你的Form1的实例中调用它
chartscopier.CopyGraph(data,this.textBox1)
此示例将向您展示如何从其他类访问表单元素:
public partial class Form1 : Form { private void button1_Click(object sender, EventArgs e) { FormCopier fc = new FormCopier(); fc.PopulateTest(this.textBox1); } }
而在其他课程..
class FormCopier { public void PopulateTest(TextBox t) { t.Text = "Demo"; t.Refresh(); } }