我有两种形式,我想使用 json url 和从第一种形式到第二种形式的数据。
表格 1 :这是我打开第二个表单的地方
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);
}
}
在 Form2 上使用构造函数。您可以传入 json、详细信息对象或单个详细信息项(在循环中)。例如,要传递 json,请使用下面的代码片段。为 url 等添加其他参数。对于 URL,您可能需要考虑使用应用程序设置使其在整个应用程序中可用。
Form2 secondForm = new Form2(json, jsonUrl);
并在 Form2 中,将构造函数更改为以下内容。
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.
}
}
}