0

我的文本框不会更新!我将它用作日志来更新其他正在做的事情......

表格 1 代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.OleDb;
using System.Collections.Specialized;
using System.Text;

delegate void logAdd(string message);

namespace LCR_ShepherdStaffupdater_1._0
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent(); 
        }

        public void logAdd(string message)
        {
            this.Log.Items.Add(message); // Doesnt add to textbox

            this.Log.Items.Add("This won't update the textbox!!! Why?"); // Doesnt add to textbox
        }

        private void exitProgramToolStripMenuItem_Click(object sender, EventArgs e) 
        {
            Application.Exit(); 
        }
        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form aboutBox = new AboutBox1(); 
            aboutBox.ShowDialog(); 
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void settingsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            settingsForm.settings.ShowDialog();
        }

        private void synchronize_Click(object sender, EventArgs e)
        {
            this.Log.Items.Add("This WILL update the textbox. It is exactly the same as the other one..."); // This will add to the textbox
            DatabaseHandling.createDataSet();
        }

    }

    public class settingsForm 
    {
        public static Form settings = new Settings();
    }

}

我的日志记录类代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LCR_ShepherdStaffupdater_1._0
{
    public class Logging
    {
        static Main mainClass = new Main();
        static logAdd logAddDelegate;

        public static void updateLog(string message)
        {
            logAddDelegate = mainClass.logAdd;
            logAddDelegate(message);
        }
    }
}

我完全被难住了......有谁知道我可以如何修复 logAdd 函数,以便我可以让我的文本框日志最终更新?

编辑:

我终于让它工作了,这里是修复:

Form1 代码

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.OleDb;
using System.Collections.Specialized;
using System.Text;

delegate void logAdd(string message);

namespace LCR_ShepherdStaffupdater_1._0
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent(); 
        }

        public void add(string message)
        {
            this.Log.Items.Add(message);
        }
        public void logAdd(string message)
        {
            Log.Invoke(new logAdd(add), new object[] { message });
        }

        private void exitProgramToolStripMenuItem_Click(object sender, EventArgs e) 
        {
            Application.Exit(); 
        }
        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form aboutBox = new AboutBox1(); 
            aboutBox.ShowDialog(); 
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void settingsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            settingsForm.settings.ShowDialog();
        }

        private void synchronize_Click(object sender, EventArgs e)
        {
            logAdd("this works");
        }

    }

    public class settingsForm 
    {
        public static Form settings = new Settings();
    }

}

记录代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LCR_ShepherdStaffupdater_1._0
{
    public class Logging
    {
        static Main mainClass = new Main();
        static logAdd logAddDelegate;

        public static void updateLog(string message)
        {
            logAddDelegate = mainClass.logAdd;
            logAddDelegate(message);
        }
    }
}

嗯......我可以说这是一种非常糟糕的方法......但直到我找到一个真正的修复,只要它工作我很高兴。谢谢你们的帮助!

4

4 回答 4

4

从您发布的代码中,您的静态 Logger 类正在创建 Main 表单的新实例。这与您在 UI 上运行的不同。

总的来说,这不是一个很好的设计模式。如果您希望我详细说明并提供示例,请写评论。

于 2009-02-04T15:49:00.290 回答
1

你从哪个线程调用Logging.updateLog()?如果不是 UI 线程,我可以看到 TextBox 在这种情况下不会更新。如果您尝试调用logAdd()from synchronize_Click()(假设这是根据名称从 UI 线程调用的),那么它可以工作吗?

于 2009-02-04T15:47:42.507 回答
1

synchronize_click 在正在运行的主窗体的实例上运行。logadd 函数在您在日志记录代码中创建的单独主窗体上运行。为此,您需要将现有的主表单传递到日志代码中,以便它更新您可以看到的文本框,而不是您看不到的额外文本框。

于 2009-02-04T15:59:53.510 回答
0

我以前也遇到过类似的问题,.Invalidate()写完后在文本框上调用就解决了。

您还可以通过以下方式检查您的消息是否实际到达文本框:

debug.WriteLine( this.Log.Items( this.Log.Items.Count-1));

希望对您有所帮助。

于 2009-02-04T15:47:10.917 回答