Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我试图将结果写入文本框时,我得到了关于控件的错误。调用必须是。。。。我的代码怎么了?我也是c语言的新手#_C# - Fatal编程技术网

C# 当我试图将结果写入文本框时,我得到了关于控件的错误。调用必须是。。。。我的代码怎么了?我也是c语言的新手#

C# 当我试图将结果写入文本框时,我得到了关于控件的错误。调用必须是。。。。我的代码怎么了?我也是c语言的新手#,c#,C#,如果InvokeRequired为true,则无法访问文本框之类的UI元素,因为当前代码运行的线程与定义UI元素的线程不同。在这种情况下,调用Invoke方法,将委托再次传递给同一个方法和委托期望的参数 namespace SmartDeviceProject4 { public partial class Form1 : Form { Barcode2 brkd = new Barcode2(); public Form1() {

如果InvokeRequired为true,则无法访问文本框之类的UI元素,因为当前代码运行的线程与定义UI元素的线程不同。在这种情况下,调用Invoke方法,将委托再次传递给同一个方法和委托期望的参数

namespace SmartDeviceProject4
{
    public partial class Form1 : Form
    {
        Barcode2 brkd = new Barcode2();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            brkd.ScanBufferStart();
            brkd.OnScan += new Barcode2.OnScanHandler(brkd_OnScan);
        }

        void brkd_OnScan(ScanDataCollection scancollection)
        {
            if (textBox1.InvokeRequired)
            {
                textBox1.Text = scancollection.GetFirst.Text;//Occurs error here
            }
            else
            {
                textBox1.Text = "invoke not required";
            }
            brkd.ScanBufferStop();
        }
    }
}

您需要提供更多的上下文。例如完整的错误消息
  void brkd_OnScan(ScanDataCollection scancollection)
  {
        if (textBox1.InvokeRequired)
        {
            // Don't try to use UI properties here, but reinvoke this same method 
            // asking the framework to switch to the thread owning the control....
            textBox1.Invoke(new Barcode2.OnScanHandler(brkd_OnScan), scancollection);
        }
        else
        {
            // Now this code runs in the same thread of the UI element and it is possible
            // to change the properties of the textbox.
            textBox1.Text = scancollection.GetFirst.Text;
        }
        brkd.ScanBufferStop();
  }