C# 如何在不使form1中的方法成为静态的情况下,将form1中的方法调用到新类?
也不需要在新类中创建一个新的form1实例。 在表格1中,我有一个公开的方法:C# 如何在不使form1中的方法成为静态的情况下,将form1中的方法调用到新类?,c#,.net,winforms,C#,.net,Winforms,也不需要在新类中创建一个新的form1实例。 在表格1中,我有一个公开的方法: public void Test() { } 那么这个班, using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Timers; using System.Windows.For
public void Test()
{
}
那么这个班,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Timers;
using System.Windows.Forms;
namespace ScrollLabelTest
{
class SaveOldHtml
{
private static Form frm1 = null;
private static int count;
private static System.Timers.Timer _timer = new System.Timers.Timer();
private static string page;
private static List<string> newText = new List<string>();
public SaveOldHtml(string DirectoryToSave,int count, string contents)
{
System.IO.File.WriteAllText(DirectoryToSave + "Page" + count.ToString("D6")
+ ".html", contents);
}
public SaveOldHtml(string DirectoryToSave, List<string> newTextList, int count)
{
using (StreamWriter myStream = new StreamWriter(DirectoryToSave + "newTextList" + count.ToString("D6")
+ ".txt"))
{
for (int i = 0; i < newTextList.Count; i++)
{
myStream.WriteLine(newTextList[i]);
}
}
}
public static void Start(Form1 form)
{
frm1 = form;
_timer.Elapsed += _timer_Elapsed;
_timer.Interval = 10000;
count = 5;
LoadOldHtmlFiles();
_timer.Start();
}
但是frm1不具有表1中方法的属性。
我需要在form1的这个类中创建新实例?或者有没有其他方法不创建实例也不创建静态方法?您正在将构造函数中的参数存储为基本类型表单,而不是实际类型表单1 改变
private static Form frm1 = null;
到
和frm1.Test将可用。为什么不想创建表单的实例?你的表单已经打开了吗?不清楚你想要实现什么。无论如何,我认为让其他类调用形式的公共方法不是一个好主意。
private static Form1 frm1 = null;