小小菜鸟小小回复一下在form1添加一个lable 其text值等于form2中的textbox值form1的button中写show(form2)form2的button中写this.close()应该是没有问题的吧...
解决方案 »
- DataTableCollection”不包含“Rows”的定义啥会有这种错误
- C#serialport串口读写数据的问题。
- WPF如果窗体的大小以及窗体内控件的大小都是动态的话,就预览不了了
- 求教..关于form...
- 关于异步编程和多线程编程选择的问题
- 超级特难问题,有谁解决了,我出200大洋
- 请教将一个实体序列化的问题
- decimal 2.537868 取小数点后两位如何做到四舍五入
- DataGridView控件
- 如何在DATAGRID中单击一行改变其颜色及如何得到单击行的值?
- 我有一个SQL 脚本,在C#里建立好数据库连接后,怎么在C#中执行这个sql脚本??
- VS 2005和VS 2008
我就是这么写的呀,然后就出现上面的问题了
因为Form1中的button的方法new 一个form,同样你点继承他的窗口点button也会new一个窗口出来
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
test t=new test();
private void button1_Click(object sender, EventArgs e)
{ string a = textBox1.Text;
Form2 f2 = new Form2(a,t);
f2.ShowDialog();
textBox1.Text = t.A;
}
}
}
Form2的代码
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
}
private string a;
test t;
public Form2(string a,test t)
{ this.a = a;
this.t = t;
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = a;
} private void button1_Click(object sender, EventArgs e)
{
t.A = textBox1.Text;
this.Close();
}
}
}
另外要添加一个辅助类 class test
namespace WindowsFormsApplication1
{
public class test
{
private string a; public string A
{
get { return a; }
set { a = value; }
}
}
}自己研究下吧 写的太清楚了你看过以后又会忘记了
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
test t=new test();
private void button1_Click(object sender, EventArgs e)
{ string a = textBox1.Text;
Form2 f2 = new Form2(a,t);
f2.ShowDialog();
textBox1.Text = t.A;
}
}
}
Form2的代码
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
}
private string a;
test t;
public Form2(string a,test t)
{ this.a = a;
this.t = t;
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = a;
} private void button1_Click(object sender, EventArgs e)
{
t.A = textBox1.Text;
this.Close();
}
}
}
另外要添加一个辅助类 class test
namespace WindowsFormsApplication1
{
public class test
{
private string a; public string A
{
get { return a; }
set { a = value; }
}
}
}自己研究下吧 写的太清楚了你看过以后又会忘记了
你可以在Form1中声明一个变量用来存储Form2中输入的文本 例如:String myInput = "";
在Form2构造函数中添加一个参数 private Form1 f1; public Form2(Form1 f){this.f1= f;}
在调用的时候创建对象 Form2 f2 = new Form2 (this); f2.ShowDialog();
点击关闭按钮的事件
private void closeButton_click (Object sender,EventArgs e)
{
f1.myInput = textbox1.text;
this.Dispose();}
回到Form1就可以使用myInput了 说白了就是把Form1的当前对象当做参数传给Form2 这样两个窗体就可以交互数据了
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace test01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string Text1; //定义字符串,接收form2中textbox的值 private void button1_Click(object sender, EventArgs e)
{
Form2 sh = new Form2(); //点击按钮,显示form2
sh.ShowDialog();
this.label1.Text = Text1; //把从form2中接收过来的值显示在label上
}
}
}
再看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 test01
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1.Text1 = this.textBox1.Text; //把输入到textbox的值传给form1中定义的Text1字符串
this.Close(); //退出
}
}
}