2017-05-11 13 views
0

私は高い値段と低い値を検索しています。私は、form1のテキストボックスから文字列に値を取得する必要があり、私の場合はcopyコマンドやstorescpのようなものを実行するためにform4で使われています。 例: Form1: パブリックstatic string port1 port1 = Pt1.text; dicompath = location.text;複数のテキストボックスの値を別のフォームの文字列に渡すか、別のフォームのテキストボックスから値/文字列を呼び出す

 Form4: 
     port1 = frm1.Port1.Text; 
     dicompath = frm1.location.text; 

     finalpath = port1 + " --fork -v -pm -fe .dcm -tn -sp -od " + ((char)34) + dicompath + ((char)34); 

     Process startInfo2 = new Process(); 
     startInfo2.StartInfo.CreateNoWindow = true; 
     startInfo2.StartInfo.UseShellExecute = false; 
     startInfo2.StartInfo.RedirectStandardOutput = true; 
     startInfo2.StartInfo.RedirectStandardError = true; 
     startInfo2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     startInfo2.StartInfo.FileName = @"C:\dcmtk\bin\storescp-tls.exe"; 
     startInfo2.StartInfo.Arguments = finalpath; 
     startInfo2.StartInfo.RedirectStandardOutput = true; 

何らかの理由で、「オブジェクト参照がオブジェクトのインスタンスに設定されていません」という問題が引き続き発生します。

+0

'オブジェクトのインスタンスに設定されていません.'あなたのQは私には少し曖昧です。ユーザーはform1に値を入力し、その値をform4に使用したいですか?または、後で何かを実行するために、form1上のユーザー入力値とform4上の別のユーザー入力値を結合したいですか? –

+0

別のフォームから呼び出すことができる独自の変数に格納する(結合しない)約4-6のテキストボックスがあります。 –

答えて

0

あなたの質問を読んでいる方法、ユーザーが入力した値をあるフォームから別のフォームに渡す方法を尋ねる方法。ここでは、基本的な概念を示す最​​小限の例を示します。 基本的には、は、パラメータを受け入れるフォームコンストラクタを作成するのに、が必要です。この新しいコンストラクターで余分なフォームをインスタンス化し、渡す値を使用することができます。このスニペットで

は、私が Form2 form2 = new Form2(theText);

のForm1.csを使用してのForm2にForm1のから値を渡している

あなたのコードの一部である
using System; 
using System.Drawing; 
using System.Windows.Forms; 

namespace PassValuesBetweenForms_43923548 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      CreateTextBox(); 
      CreateSubmitButton(); 
     } 

     private void CreateSubmitButton() 
     { 
      Button btn = new Button(); 
      btn.Text = "click me"; 
      btn.Click += ClickMeGotClicked; 
      btn.Location = new Point(5, 25); 
      this.Controls.Add(btn); 
     } 

     private void ClickMeGotClicked(object sender, EventArgs e) 
     { 
      string theText = ((TextBox)this.Controls["txtbx"]).Text; 
      Form2 form2 = new Form2(theText);//call the parametized constructor 
      form2.Show(); 
     } 

     private void CreateTextBox() 
     { 
      TextBox tb = new TextBox(); 
      tb.Location = new Point(5, 5); 
      tb.Width = 50; 
      tb.Name = "txtbx"; 
      this.Controls.Add(tb); 
     } 
    } 
} 

にForm2.cs

using System.Drawing; 
using System.Windows.Forms; 

namespace PassValuesBetweenForms_43923548 
{ 
    public partial class Form2 : Form 
    { 
     public Form2() 
     { 
      InitializeComponent(); 
     } 

     //make a parametized constructor 
     public Form2(string incomingFromForm1) 
     { 
      InitializeComponent(); 
      if (!string.IsNullOrEmpty(incomingFromForm1)) 
      { 
       MakeALabelWithThis(incomingFromForm1); 
      } 
     } 

     private void MakeALabelWithThis(string incomingFromForm1) 
     { 
      Label lbl = new Label(); 
      lbl.Text = incomingFromForm1; 
      lbl.Location = new Point(5,5); 
      this.Controls.Add(lbl); 
     } 
    } 
} 
関連する問題