简体   繁体   English

从另一种形式调用函数C#

[英]Calling a function from another form c#

i have a main form with a function that changes the text of a text box thats on the main form, the code is below: 我有一个带有功能的主窗体,该函数可以更改在主窗体上的文本框的文本,代码如下:

main form function: 主要形式功能:

    public void consoleLog(string message)
    {
        txtConsoleLog.Text += Environment.NewLine;
        txtConsoleLog.Text += message;
        txtConsoleLog.SelectionStart = txtConsoleLog.TextLength;
        txtConsoleLog.ScrollToCaret();
        txtConsoleLog.Refresh();
    }

So now i open a new form called frm connect when i click a button like this: 因此,当我单击如下所示的按钮时,我打开了一个名为frm connect的新表单:

    private void connectToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form frmConnect = new FrmConnect(this);
        frmConnect.Show();
    }

this is the frmConnect below 这是下面的frmConnect

public partial class FrmConnect : Form
{
    private Form mainForm;

    public FrmConnect(Form frmMain)
    {
        this.mainForm = frmMain;
        InitializeComponent();
    }

    private void btnConnect_Click(object sender, EventArgs e)
    {
        FrmMain.Connected = true;
        mainForm.consoleLog("Connected");
    }
}

So when i click a button i want to call the function but its saying it doesnt contain a definition for it, also im trying to change the 'Connected' variable thats on the main form which works by just referencing the FrmMain but am i able to do that using mainForm.Connected = true? 因此,当我单击一个按钮时,我想调用该函数,但它说它不包含其定义,我也试图通过仅引用FrmMain来更改主窗体上的“ Connected”变量,但我能够使用mainForm.Connected = true做到这一点?

If i change the function to public static, it will work by referencing FrmMain but then i get errors with the txtConsoleLog as i cant reference an object in a non static method or something like that, any help is appriciated 如果我将函数更改为public static,它将通过引用FrmMain起作用,但是由于我无法以非静态方法或类似的方式引用对象,因此txtConsoleLog会出现错误。

When you pass the form into your constructor, and store it as the private member variable, in both places you declare it of the base type Form . 当您将表单传递给构造函数并将其存储为私有成员变量时,在两个地方都将其声明为基本类型Form In order to use a method on the type that you defined, your parameter and variable should be of type FrmMain . 为了在您定义的类型上使用方法,您的参数和变量应为FrmMain类型。

public partial class FrmConnect : Form
{
    private FrmMain mainForm;

    public FrmConnect(FrmMain frmMain)
    {
        this.mainForm = frmMain;
        InitializeComponent();
    }

    private void btnConnect_Click(object sender, EventArgs e)
    {
        FrmMain.Connected = true;
        mainForm.consoleLog("Connected");
    }
}

You should change 你应该改变

private Form mainForm;

public FrmConnect(Form frmMain)
{

To

private FrmMain mainForm;

public FrmConnect(FrmMain frmMain)
{

which will later give you access to all of the public properties on FrmMain in your other methods in the FrmConnect class. 稍后,您将可以通过FrmConnect类中的其他方法访问FrmMain上的所有公共属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM