简体   繁体   English

如何制作一种可以从C#中的任何线程更改任何文本框的文本属性的方法?

[英]how to make a method which can change text property of any text box from any thread in C#?

I am not a really good C# programmer. 我不是一个非常好的C#程序员。 I am used to C but not C#. 我习惯于C,但不习惯C#。

I know that from a thread other than UI thread we cannot change the property of textbox and labels. 我知道,从UI线程以外的线程中,我们无法更改文本框和标签的属性。

So after trying few different ways I am using this method all the time: 因此,在尝试了几种不同的方法之后,我一直都在使用此方法:

 item.Invoke(new EventHandler(delegate
            {
                item.Text = text;
            }));

Then after writing some code I noticed it is boring and inefficient to do this every single time, so I made a method: 然后,在编写一些代码之后,我注意到每次执行此操作都很无聊且效率低下,所以我做了一个方法:

static void change_text_from_different_thread(TextBox item, string text)
        {
            item.Invoke(new EventHandler(delegate
            {
                item.Text = text;
            }));

        }

then I tried to use this in my thread the problem is that I can't send the TextBox from the thread. 然后我尝试在线程中使用它,问题是无法从线程发送TextBox。

let's say this is my thread: 假设这是我的主题:

{

change_text_from_different_thread(textbox1,"Hi");

}

then I get this error: 然后我得到这个错误:

An object reference is required for the non-static field, method, or property 非静态字段,方法或属性需要对象引用

So could you tell me how I can pass the textbox? 那你能告诉我如何传递文本框吗? BTW there is a chance that my invoke method is wrong or very dangerous, but I could not find a easier way. 顺便说一句,我的invoke方法可能是错误的或非常危险的,但是我找不到更简单的方法。 If it is really bad, please let me know. 如果真的不好,请告诉我。 If it is okay, also let me know. 如果可以的话,也请通知我。 If by using another method things get better and easier please do let me know as well. 如果通过其他方法使事情变得更好和更轻松,请也让我知道。

It is my first time asking question, sorry if I have missed some thing. 这是我第一次问问题,对不起,如果我错过了一些事情。 Thank you very much. 非常感谢你。

This is my method: 这是我的方法:

static void gyro_self_check_thread_function()
{
//lab lab
}

this is how I made a thread and start it: 这是我创建线程并启动它的方式:

private void Gyro_self_check_button_Click(object sender, EventArgs e)
        {

            Gyro_self_check_status_label.Text = "Status: Checking....";
            gyro_self_testing_groupBox.Update();
            _serialPort.Write(Protocal_Values.gyro_self_checking_request, 0, 2);


            Thread gyro_self_check_thread = new Thread(gyro_self_check_thread_function);
            gyro_self_check_thread.Start();

            //Thread gyro_self_check_thread = new Thread(gyro_self_check_thread);
            //gyro_self_check_thread.Start();



        }

Looks like your thread method is also static and textbox1 is a non-static field, so it cannot be accessed from static method. 看起来您的线程方法也是静态的,而textbox1是一个非静态字段,因此无法从静态方法访问它。

If this is the case, then you can make your thread method non-static and it should compile. 如果是这种情况,则可以使线程方法成为非静态方法,并且应该编译。

As another posted commented, this is probably an issue with static vs. non-static access. 正如另一篇评论所述,这可能是静态访问与非静态访问的问题。 To alleviate this problem, you might be able to write this as an extension method on TextBox: 为了缓解此问题,您可以将其作为扩展方法写在TextBox上:

static class ExtensionMethods
{
     static void change_text_from_different_thread(this TextBox item, string text)
     {
         item.Invoke(new EventHandler(delegate
             {
                 item.Text = text;
             }));

     }
}

Then you call it like so: 然后,您可以这样称呼它:

textbox1.change_text_from_different_thread("Hi");

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

相关问题 如何将任何可以显示文本的对象传递给C#方法 - How to pass any object that can display text to a C# method 如何使RichText框中的任何文本作为超链接 - how to make any text in richtext box as hyperlink .net(C#)中是否有“非常丰富”的文本框? (WPF) - Is there any “very rich” text box in .net (C#)? (WPF) 如何使用C#代码将文本插入任何网站? - how to insert any text to any website using C# code? 我如何能够在富文本框或任何类型的视觉输出中显示此信息。 C# - How would i be able to present this information in a rich text box or any type of visual outputs. C# 如何在C#中的富文本框中使某些文本变为粗体 - How to make some text bold in a rich text box in C# 在C#中,如何更改“文本”框中新添加的文本的颜色 - In C#, how to change colour of newly added text in Text box 如何在富文本框中更改所选的文本背景颜色wpf c# - How can I change the selected text background color in Rich text box wpf c# 如何从 C# 中的文本中搜索短语的任何顺序及其任何单词 - How to search any order of a phrase and any word of it from text in C# 将文本文件的编码从ANSI更改为UTF8,而不会影响C#中文件的任何字符! - Change encoding of a text file from ANSI to UTF8 without affecting any chars of the file in C#!
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM