
有关C#数据在窗口之间传递的问题?
form1的主要代码:namespaceWindowsFormsApplication1{publicpartialclassForm1:Form{publicForm1...
form1的主要代码:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.lable1.Text = this.textBox1.Text;
}
}
}
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 Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public System.Windows.Forms.Label lable1;
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
绝对有问题! 展开
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.lable1.Text = this.textBox1.Text;
}
}
}
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 Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public System.Windows.Forms.Label lable1;
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
绝对有问题! 展开
3个回答
展开全部
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2.XXX = this.textBox1.Text;
}
}
public partial class Form2 : Form
{
public static string XXX;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
this.label.Text = XXX;
}
}
首先在窗体2中定义一个公共的字符串变量,就可以在窗体1中直接给它赋值,达到了传递数据的目的。以上代码将其定义为了静态的,这样就可以在窗体1的按钮事件中直接对其赋值,要不就只能在获取窗体2的实例后才能对其进行赋值。
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2.XXX = this.textBox1.Text;
}
}
public partial class Form2 : Form
{
public static string XXX;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
this.label.Text = XXX;
}
}
首先在窗体2中定义一个公共的字符串变量,就可以在窗体1中直接给它赋值,达到了传递数据的目的。以上代码将其定义为了静态的,这样就可以在窗体1的按钮事件中直接对其赋值,要不就只能在获取窗体2的实例后才能对其进行赋值。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询