在我开始之前,我想说我知道这个论坛上有类似的帖子。不幸的是,就我而言,它们不起作用。我确定这是我的错。我希望你能帮助我。
所以这是我的情况:
我写了 windows 窗体,它像终端一样工作。我有向这个终端添加文本的功能。我想从自定义类内部调用这个函数。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace terminal
{
public partial class Form1 : Form
{
Test testClass = new Test() ;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
terminal(textBox1.Text);
}
public void terminal(string text)
{
richTextBox1.AppendText(text);
richTextBox1.AppendText("\n");
}
}
public class Test
{
Form1.terminal("sample tekst");
}
}
而这东西是行不通的。当我开始调试时,我可以看到内存在增加,但没有出现任何表格。我的问题是我无法访问函数 addToLog();来自类 LoadSomeFiles。 Visual Studio 下划线整行
Form1.terminal("sample tekst");
红色的。如果我写了这种奇怪的东西,我很抱歉。我是新手。
源文件:Program
谢谢你的帮助!
最佳答案
只需制作您需要的对象的实例。在本例中为 Form1 对象。
然后您可以像以前一样访问它。
这就是我的意思:
Form1 form1 = new Form1();
form1.terminal("sample text");
希望这对你有帮助。
关于c# - otside 类放置在 Form 中的 Visual Studio 调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39143877/