当我使用不同的函数执行完全相同的操作时,为什么我的文本框不会更新?

时间:2022-10-07 20:05:16

My TextBox won't update! I am using it as a Log to update what other things are doing...

我的TextBox不会更新!我用它作为日志来更新其他事情正在做什么...

Form 1 code:

表格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();
    }

}

My Logging Class code:

我的记录类代码:

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);
        }
    }
}

I am just completely stumped....does anyone know how I can fix the logAdd function so I can get my textbox log to finally update?

我只是完全难过....有谁知道我如何修复logAdd函数所以我可以让我的文本框日志最终更新?

EDIT:

I finally got it to work, here was the fix:

我终于得到了它的工作,这是修复:

Form1 Code

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();
    }

}

Logging Code:

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);
        }
    }
}

Well...I can tell that is a really bad way to do it...but until I find a true fix as long as it works im happy. Thanks for the help guys!

好吧......我可以说这是一个非常糟糕的方法......但是直到我找到一个真正的解决方案,只要它工作我很开心。谢谢你的帮助!

4 个解决方案

#1


What thread are you calling Logging.updateLog() from? If it's not the UI thread, I could see that the TextBox wouldn't get updated in that case. If you try calling logAdd() from synchronize_Click() (assuming that this is called from the UI thread based on the name), does it work then?

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

#2


From the code you posted your static Logger class is creating a new instance of the Main form. which is different than the one your running on the UI.

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

Overall this isn't a very good design pattern. write a comment if you want me to elaborate and give a sample.

总的来说,这不是一个非常好的设计模式。如果你想让我详细说明并提供样品,请写评论。

#3


the synchronize_click is run on the instance of the main form that is running. The logadd function is run on a seperate main form that you create in the logging code. For this to work, you need to pass your existing main form into the log code so that it updates the text box you can see, not the extra text box you don't.

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

#4


I had a problem similar once before, it was solved by calling .Invalidate() on the text box after writing to it.

我之前遇到过一个类似的问题,它是通过在写入文本框后调用.Invalidate()来解决的。

You could also check your message is actually getting to the textbox with:

您还可以检查您的邮件实际上是否已进入文本框:

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

Hope that helps you.

希望对你有所帮助。

#1


What thread are you calling Logging.updateLog() from? If it's not the UI thread, I could see that the TextBox wouldn't get updated in that case. If you try calling logAdd() from synchronize_Click() (assuming that this is called from the UI thread based on the name), does it work then?

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

#2


From the code you posted your static Logger class is creating a new instance of the Main form. which is different than the one your running on the UI.

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

Overall this isn't a very good design pattern. write a comment if you want me to elaborate and give a sample.

总的来说,这不是一个非常好的设计模式。如果你想让我详细说明并提供样品,请写评论。

#3


the synchronize_click is run on the instance of the main form that is running. The logadd function is run on a seperate main form that you create in the logging code. For this to work, you need to pass your existing main form into the log code so that it updates the text box you can see, not the extra text box you don't.

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

#4


I had a problem similar once before, it was solved by calling .Invalidate() on the text box after writing to it.

我之前遇到过一个类似的问题,它是通过在写入文本框后调用.Invalidate()来解决的。

You could also check your message is actually getting to the textbox with:

您还可以检查您的邮件实际上是否已进入文本框:

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

Hope that helps you.

希望对你有所帮助。