简体   繁体   English

输入来自不同类C#的文本框

[英]Input to a textbox from a different class C#

I don't want to put up the full code for this as it is for an assignment, but my main issue is getting text from a class within Program.cs to display in a textbox in Form.cs . 我不想为此写完整的代码,但我的主要问题是从Program.cs中的类获取文本以显示在Form.cs的文本框中。 I have tried almost everything and i still can't seem to get it to work. 我已经尝试了几乎所有内容,但似乎仍然无法正常工作。 I really just want to display text from my Hello class into the textbox in the form class. 我真的只想将我的Hello类中的文本显示到表单类的文本框中。 This is what i have at the moment 这就是我目前所拥有的

Program.cs - main code 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 - Where textbox is 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;
    } 

  }

I know the code may not make sense but that is mainly because i have to edit it because i cannot post my assignment code on here. 我知道代码可能没有意义,但这主要是因为我必须对其进行编辑,因为我无法在此处发布作业代码。 Can anyone see what my issue is ? 谁能看到我的问题是什么? Why text won't display in the form when opened, i've tried multiple solutions online and still nothing. 为什么文本在打开时不会显示在表单中,我已经在线尝试了多种解决方案,但仍然一无所获。

when you call form.ShowDialog(); 当您调用form.ShowDialog(); application main thread will stop here and Program1 p = new Program1() ; 应用程序主线程将在此处停止,并且Program1 p = new Program1() ; will not run until form close event call. 在表单关闭事件调用之前将不会运行。

Program.cs Program.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 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;
        } 
    }

Edit for two thread 编辑两个线程

Program.cs Program.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 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;
        } 
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM