【发布时间】:2012-05-09 10:13:21
【问题描述】:
全部。
我是 C# 的新手。我知道这是一个非常受欢迎的问题。但我不明白。我知道有一个错误,但在哪里? 例如 - Form1 代码的第一部分包含私有变量测试,我需要在 Form2 中获取此变量的值。哪里出错了?
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string test = "test this point";
Form2 dlg = new Form2();
dlg.test = test;
dlg.Show();
}
}
}
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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
public string test { get; set; }
public Form2()
{
InitializeComponent();
label1.Text = test;
}
}
}
【问题讨论】:
-
那么你必须将它分配给
Form2 test属性,你所拥有的是一个独立的字符串,它与Form2的属性无关。需要Form1中的dlg.test = test;。