2013-03-17 46 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();申請主線程將停在這裏和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 2013-03-17 14:18:09

+0

@CodeCompileHack:查看我編輯的兩個線程 – KF2 2013-03-17 14:25:00

相關問題