将消息记录到其他类的文本框中

时间:2014-02-18 21:35:57

标签: c# class composition

我有一个多行文本框,我想将消息记录到其他类

但是我在课堂创作中得到了无限循环。

从表单类外部更新表单控件的正确方法是什么?

namespace Program
{

    public partial class MainForm : Form
    {
        engine connEngine = new engine();

        public MainForm()
        {
            InitializeComponent();

        }

        private void Start_Click(object sender, EventArgs e)
        {
            msgLog("Connection Start Clicked");

            if (connEngine.StartConnection(txtIPAddress.Text, 8002) == false)
            {
                msgLog("Connection Start Failed"); 
            }
            else
            {
                msgLog("Connection Start Success");
            }

        }

        public void msgLog(string message)
        {
            txtMessageLog.AppendText(message + Environment.NewLine);
        }


        private void MainForm_Load(object sender, EventArgs e)
        {
            msgLog("Form Load Success");
        }

    }

    class engine
    {
        MainForm _mainForm = new MainForm();

        public bool StartConnection(string txtIPAddress, int p)
        {
            _mainForm.msgLog("StartConnection started");
            //do something
            return true;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您的问题是您在主窗体中创建引擎的实例,并在引擎中创建 MainForm 的实例 - 这会创建循环。请考虑将其作为参数传递给引擎的构造函数,而不是在引擎中创建 MainForm 的新实例。这样的事情(未经测试):

class engine
{
private MainForm _mainForm = null;

engine(MainForm formInstance)
{
   _mainForm = formInstance;
}

public bool StartConnection(string txtIPAddress, int p)
{
    _mainForm.msgLog("StartConnection started");
    //do something
    return true;
}
}

你可以像这样使用它:

    public partial class MainForm : Form
    {
       private engine connEngine = null;

       public MainForm()
       {
          InitializeComponent();

          connEngine = new engine(this);
       }

...

请原谅我的格式......

答案 1 :(得分:1)

Piotr解决方案很好。但是,我更喜欢在想要记录的类中使用Action委托。所以引擎类仍然独立于来自MainForm的任何其他内容,并且所有内容都保持松散耦合。

public partial class MainForm : Form
{
    engine connEngine = new engine();

    public MainForm()
    {
        InitializeComponent();

        connEngine.log = s => { msgLog(s); };
    }

    private void Start_Click(object sender, EventArgs e)
    {
        msgLog("Connection Start Clicked");

        if (connEngine.StartConnection(txtIPAddress.Text, 8002) == false)
        {
            msgLog("Connection Start Failed"); 
        }
        else
        {
            msgLog("Connection Start Success");
        }

    }

    void msgLog(string message)
    {
        txtMessageLog.AppendText(message + Environment.NewLine);
    }


    private void MainForm_Load(object sender, EventArgs e)
    {
        msgLog("Form Load Success");
    }

}

class engine
{
    internal Action<string> log;

    public bool StartConnection(string txtIPAddress, int p)
    {
        log("StartConnection started");
        //do something
        return true;
    }
}

答案 2 :(得分:0)

你这里有一点递归。 MainForm实现了engine

public partial class MainForm : Form
{
    engine connEngine = new engine();

engine实现MainForm

class engine
{
    MainForm _mainForm = new MainForm();

你最终得到的是一个无限循环的实例化。