과제의 경우처럼 전체 코드를 사용하고 싶지 않지만 내 주요 문제는 Program.cs 내의 클래스에서 텍스트를 가져 와서 Form.cs의 텍스트 상자. 나는 거의 모든 것을 시도해 봤지만 여전히 작동시키지 못합니다. 필자는 Hello 클래스의 텍스트를 폼 클래스의 텍스트 상자에 표시하려고합니다. 주요 코드다른 클래스의 텍스트 상자에 입력 C#
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;
}
}
하지만 즉이 나는 순간
Program.cs에있을 것입니다 주로 여기에 내 과제 코드를 게시 할 수 없기 때문에 편집해야하기 때문입니다. 아무도 내 문제가 무엇인지 볼 수 있습니까? 왜 텍스트가 열릴 때 양식에 표시되지 않습니다, 나는 여러 솔루션을 온라인으로 시도했지만 아직 아무것도하지 못했습니다.
고마워요, 그것은 하나의 스레드에 대해 완벽하게 작동하지만 기본적으로 두 개의 스레드가 각각 폼을 열어야합니다. 이 작업을 수행하는 방법을 알고 있습니까? – CodeCompileHack
@CodeCompileHack : 두 개의 스레드에 대한 내 편집 참조 – KF2