【问题标题】:Input to a textbox from a different class C#从不同的类 C# 输入到文本框
【发布时间】:2013-03-17 12:38:46
【问题描述】:

我不想为这个任务提供完整的代码,但我的主要问题是从 Program.cs 中的类中获取文本以显示在 Form.cs 的文本框中。我几乎尝试了所有方法,但似乎仍然无法正常工作。我真的只想将 Hello 类中的文本显示到表单类的文本框中。这是我目前拥有的

Program.cs - 主要代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;

namespaceWindows
{

static class Program
{

    public static Form1 form;
    [STAThread]

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Thread thread1 = new Thread(new ThreadStart(open));
        thread1.Start();
        Thread thread2 = new Thread(new ThreadStart(open));
        thread2.Start();

    }

    static void openForms()
    {

        form = new Form1();
        form.ShowDialog();
        Program1 p = new Program1();


    }
   }

public class Program1
{
   private HELLO h;
   public Program1()
    {


        h = new HELLO();
    }
}

public class HELLO
{
  public HELLO()
  {
   Program.form.ThreadSafe("Hello ");
  }

 }
}

Form1.cs - 文本框在哪里

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Windows
{
public partial class Form1 : Form
{
    string input;
    public delegate void SetTextCallback(string text);

    public Form1()
    {
        InitializeComponent();
    }



    public void ThreadSafe(string text)
    {

        if (this.screenTextBox.InvokeRequired)
        {
            // It's on a different thread, so use Invoke.
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text + " (Invoke)" });
        }
        else
        {
            // It's on the same thread, no need for Invoke
            this.screenTextBox.Text = text + " (No Invoke)";

        }
    }



    // This method is passed in to the SetTextCallBack delegate
    // to set the Text property of textBox1.
    private void SetText(string text)
    {
        this.screenTextBox.Text = text;
    } 

  }

我知道代码可能没有意义,但这主要是因为我必须编辑它,因为我无法在此处发布我的分配代码。谁能看到我的问题是什么?为什么打开时表单中不显示文字,我在网上尝试了多种解决方案,仍然没有。

【问题讨论】:

    标签: c# multithreading forms class textbox


    【解决方案1】:

    当您调用form.ShowDialog(); 时,应用程序主线程将停在这里,Program1 p = new Program1();在表单关闭事件调用之前不会运行。

    程序.cs

    static class Program
        {
    
            [STAThread]
    
            static void Main()
            {
    
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Thread thread1 = new Thread(new ThreadStart(openForms));
                thread1.SetApartmentState(ApartmentState.STA);
                thread1.Start();
            }
            static Form1 frm;
            static void openForms()
            {
                 frm = new Form1();
                 Program1 p = new Program1();
                 Application.Run(frm);
            }
    
            public class Program1
            {
                private HELLO h;
                public Program1()
                {
                    h = new HELLO();
                }
            }
    
            public class HELLO
            {
                public HELLO()
                {
                    frm._Form1.ThreadSafe("Hello ");
                }
    
            }
        }
    

    Form1.cs

     public partial class Form1 : Form
        {
            public Form1 _Form1;
            string input;
            public delegate void SetTextCallback(string text);
    
            public Form1()
            {
                InitializeComponent();
                _Form1 = this;
            }
    
            public void ThreadSafe(string text)
            {
                if (this.screenTextBox.InvokeRequired)
                {
                    // It's on a different thread, so use Invoke.
                    SetTextCallback d = new SetTextCallback(SetText);
                    this.Invoke(d, new object[] { text + " (Invoke)" });
                }
                else
                {
                    // It's on the same thread, no need for Invoke
                    this.screenTextBox.Text = text + " (No Invoke)";
    
                }
            }
    
    
    
            // This method is passed in to the SetTextCallBack delegate
            // to set the Text property of textBox1.
            private void SetText(string text)
            {
                this.screenTextBox.Text = text;
            } 
        }
    

    编辑两个线程

    程序.cs

    static class Program
        {
    
            [STAThread]
    
            static void Main()
            {
    
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Thread thread1 = new Thread(new ThreadStart(openForms));
                thread1.SetApartmentState(ApartmentState.STA);
                thread1.Start();
                Thread thread2 = new Thread(new ThreadStart(openForms));
                thread2.SetApartmentState(ApartmentState.STA);
                thread2.Start();
            }
    
            static void openForms()
            {
                 Form1 frm = new Form1();
                 Program1 p = new Program1();
                 Application.Run(frm);
            }
    
            public class Program1
            {
                private HELLO h;
                public Program1()
                {
                    h = new HELLO();
                }
            }
    
            public class HELLO
            {
                public HELLO()
                {
                    Form1._Form1.ThreadSafe("Hello ");
                }
    
            }
        }
    

    Form1.cs

      public partial class Form1 : Form
        {
            public static Form1 _Form1;
            string input;
            public delegate void SetTextCallback(string text);
    
            public Form1()
            {
                InitializeComponent();
                _Form1 = this;
            }
    
            public void ThreadSafe(string text)
            {
                Thread.Sleep(100);
                if (this.screenTextBox.InvokeRequired)
                {
                    // It's on a different thread, so use Invoke.
                    SetTextCallback d = new SetTextCallback(SetText);
                    this.Invoke(d, new object[] { text + " (Invoke)" });
                }
                else
                {
                    // It's on the same thread, no need for Invoke
                    this.screenTextBox.Text = text + " (No Invoke)";
    
                }
            }
    
    
    
            // This method is passed in to the SetTextCallBack delegate
            // to set the Text property of textBox1.
            private void SetText(string text)
            {
                this.screenTextBox.Text = text;
            } 
        }
    

    【讨论】:

    • 谢谢,它对一个线程非常有效,但基本上我需要两个线程,每个线程都打开表单。你知道如何去做吗?
    猜你喜欢
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多