2016-05-24 6 views
2

私は2つのフォームを持っており、json URLと第1フォームのデータを第2フォームに使用したいと考えています。フォーム1からフォーム2へのjson URLの転送

形態1: iは第二の形態に

private void button1_Click(object sender, EventArgs e) 
    { 

     var json = new WebClient().DownloadString("http://dev.ibeaconlivinglab.com:1881/companybyuuid?uuid="+textBox1.Text); 
     List<details> detailsList = JsonConvert.DeserializeObject<List<details>>(json); 

     foreach (details dets in detailsList) 
     { 

      if (textBox1.Text == dets.uuid) 
      { 

       this.Hide(); 
       notifyIcon1.Visible = false; 

       Form2 secondForm = new Form2(); 
       secondForm.Show(); 

      } 
      else 
      { 

       MessageBox.Show("Company not found."); 

      } 
     } 
    } 

第フォームを開く場所です。

private void Form2_Load(object sender, EventArgs e){ 

     Location = new Point(Screen.PrimaryScreen.WorkingArea.Right - Width, 
     Screen.PrimaryScreen.WorkingArea.Bottom - Height); 

     Label namelabel = new Label(); 
     namelabel.Location = new Point(13, 30); 

     foreach (details dets in detailsList) 
     { 

      namelabel.Text = dets.id; 
      this.Controls.Add(namelabel); 

     } 
    } 

答えて

0

Form2でコンストラクタを使用します。 json、詳細オブジェクトまたは個々の詳細項目(ループ内)を渡すことができます。たとえば、jsonを渡すには、以下のスニペットを使用します。 URLなどのパラメータを追加してください。AppSettingsを使用して、アプリケーション全体で利用できるようにすることをお勧めします。

Form2 secondForm = new Form2(json, jsonUrl); 

およびコンストラクタを次のように変更します。

public partial class Form2 : Form 
    { 
     private string json = ""; 
     private string jsonUrl = ""; 

     public Form2(string jsonPassedId, string jsonUrlPassedIn) 
     { 
      json = jsonPassedId; 
      jsonUrl = jsonUrlPassedIn; 
      InitializeComponent(); 
     } 

     private void Form2_Load(object sender, EventArgs e) 
     { 
      // Use json and json URL here or in the constructor as required. 
     } 
    } 
} 
+0

私は2番目のフォームにURLを使用できません。 – discable10

+0

編集された回答を参照してください。コンストラクタメソッドのシグネチャを変更するだけで、必要なものを渡すことができます。 –

+0

jsonTextはどうですか? – discable10

関連する問題