从另一个类文件中访问C#表单文本框 [英] Accesing C# Form textbox from another class file
问题描述
我搜索了很多次的解决方案,但每个代码我试过,是一个史诗失败...
这里是我的代码,我只是想要访问Form1元素,从另一个类文件(例如chartcopier.cs),因为我说我已经尝试了这么多的事情,让这个工作...但我不能改变textbox1文本从chartscopier.cs
感谢您的建议
Form1.cs
< blockquote>
命名空间TEST
{
public partial class Form1:Form
{
public Form1 ()
{
InitializeComponent();
var CopyCharts = new System.Threading.Timer(chartscopier.CopyGraph,null,0,60000);
}
}
}
chartscopier.cs
命名空间TEST
{
class chartscopier
{
// var Timer = new Timer(CopyGraph,null,0,60000);
public static void CopyGraph(object data)
{
秒表strTimer = new Stopwatch();
WebClient WC = new WebClient();
IConfigSource BaseConfig = new IniConfigSource(@D:\NEWROBOT\CONFIG.ini);
string LogDir = BasicConfig.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();
//想要写这里Form1.TextBox1.text =RunTime+ elapsedTime;
}
}
}
}
解决方案我认为,一个更好的方法是通过事件处理。
创建一个类来表示要传递的数据。 p>
public class ChartCopyProgressEventArgs:EventArgs
{
public TimeSpan ElapsedTime {get;组; }
//您可以在此处添加更多prop.s
}
chartscopier创建一个事件来报告进度。
class chartscopier
{
public static event EventHandler< ChartCopyProgressEventArgs>更改;
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;
}
}
I've search many times solutions to this but every code I've tried, was an epic fail...
So here is my code, I just want to access Form1 elements, from another class file (chartcopier.cs for example), as I said I've tried so many things to get this working... But I can't change textbox1 text from chartscopier.cs
Thanks in advice
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; } } } }
解决方案I think, a better way is to handle through events.
Create a class to represent data that you want to pass.
public class ChartCopyProgressEventArgs : EventArgs { public TimeSpan ElapsedTime { get; set; } //you can add more prop.s here }
In "chartscopier" create an event to report progress.
class chartscopier { public static event EventHandler<ChartCopyProgressEventArgs> Changed; public static void CopyGraph(object data) { ... if (Changed != null) { var args = new ChartCopyProgressEventArgs(); args.ElapsedTime = elapsedTime; Changed(null, args); } } }
Then in Form1, create a handler for that event.
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; } }
这篇关于从另一个类文件中访问C#表单文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!