2013-03-17 3 views
1

Я не хочу выставлять полный код для этого, как и для назначения, но моя основная проблема заключается в получении текста из класса в Program.cs для отображения в текстовое поле в Form.cs. Я пробовал почти все, и я все еще не могу заставить его работать. Я просто хочу отобразить текст из моего класса Hello в текстовое поле в классе формы. Это то, что я есть в данный моментВвод текстового поля из другого класса C#

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; 
    } 

    } 

Я знаю, что код не может иметь смысл, но это главным образом потому, что я должен отредактировать его, потому что я не могу опубликовать код назначения здесь. Кто-нибудь может понять, в чем моя проблема? Почему текст не будет отображаться в форме при открытии, я попробовал несколько решений в Интернете и все еще ничего.

ответ

0

когда вы позвоните form.ShowDialog(); приложение main thread остановится здесь и Program1 p = new Program1(); не будет работать до тех пор, пока не будет закрыт вызов формы.

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

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; 
     } 
    } 

Редактировать для двух резьбовых

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 "); 
      } 

     } 
    } 

Для m1.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; 
     } 
    } 
+0

Благодарим за это, он отлично работает для одной нити, но по существу мне нужны две нити, каждая из которых открывает форму. У вас есть идея, как это сделать? – CodeCompileHack

+0

@CodeCompileHack: см. Мое редактирование для двух потоков – KF2

Смежные вопросы