请问如何做?
17 个解决方案
#1
參見
http://blog.csdn.net/tjvictor/archive/2006/06/23/824617.aspx
http://blog.csdn.net/tjvictor/archive/2006/06/23/824617.aspx
#2
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.Show();
}
public void Test()
{
textBox1.Text = "aaaa";
}
******************************
public partial class Form2 : Form
{
Form1 frm;
public Form2(Form1 _frm)
{
InitializeComponent();
frm=_frm;
}
private void button1_Click(object sender, EventArgs e)
{
frm.Test();
}
private void button2_Click(object sender, EventArgs e)
{
frm.textBox1.Text = textBox1.Text;
}
}
#3
楼上的代码是传了整个form给form2,
你还可以只把form1的textbox传给form2,
默认为引用传递,所以很好控制
你还可以只把form1的textbox传给form2,
默认为引用传递,所以很好控制
#4
楼上正解~
#5
在form1定义3个静态变量,然后再form2操作改变,也可以实现。
#6
楼上想法真的独到啊,不知道两个事件并发会变成什么样子..
#7
在form1定义3个静态变量,然后再form2操作改变,也可以实现。
#8
oniu828的明显违反迪米特原则,还是让form2公开个方法来接收这三个值吧。
#9
private void Clearbtn_Click(object sender, EventArgs e)
{
clear frm = new clear(this.textBox1.Text,this.textBox2.Text,this.textBox3.Text);
frm.Show();
}
public clear(string tbS1,string tbS2,string tbS3)
{
InitializeComponent();
this.textBox1.Text = tbS1;
this.textBox2.Text = tbS2;
this.textBox3.Text = tbS3;
}
这样解决的 对吗?
{
clear frm = new clear(this.textBox1.Text,this.textBox2.Text,this.textBox3.Text);
frm.Show();
}
public clear(string tbS1,string tbS2,string tbS3)
{
InitializeComponent();
this.textBox1.Text = tbS1;
this.textBox2.Text = tbS2;
this.textBox3.Text = tbS3;
}
这样解决的 对吗?
#10
在form2类中设一个public 变量 a,在form1中
form2 b=new form2();
b.a=this.textbox1;
b.show();
form2 b=new form2();
b.a=this.textbox1;
b.show();
#11
namespace xx
{ public partial class test: Form
{
form2.x=.............
}
}
{ public partial class test: Form
{
form2.x=.............
}
}
#12
(1) 使用替換控制權來進行傳遞
Form1_button1_click
value_transfer.Form2 myfrm=new Form2("ttt");
myfrm.Owner=this;
myfrm.ShowDialog();
Form2_button2_click
//通過移交控制權來傳遞參數
value_transfer.Form1 myfrm=new Form1();
myfrm=(Form1)this.Owner;
myfrm.textBox1.Text=this.textBox1.Text;
//myfrm.textBox1.Text=myfrm.add(3,5).ToString();
//add和myfrm.textBox1設置成PUBLIC才可以訪問的。
(2) 通過FORM. DialogResult來進行參數傳遞
Form1_button1_click
value_transfer.Form2 myfrm=new Form2();
if (myfrm.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
this.textBox1.Text =myfrm.textBox1.Text;
}
Form2_button1_click
this.DialogResult=System.Windows.Forms.DialogResult.OK;
(3) 通過類的屬性來傳遞參數
//建立公共類庫
Public class test()
{
//設置私有靜態字符類型
Private static string temp;
Public class test()
{}
Public string s
{
Set
{this.temp=values;}
Get
{return this.temp;}
Form2_button1_click
value_transfer.Class1 myclass=new Class1();
myclass.test=this.textBox1.Text.Trim();
Form1_button1_click
value_transfer.Class1 myclass=new Class1();
this.textBox1.Text=myclass.test;
(4) 窗體傳遞參數
FORM2(STRING ADB);
Form1_button1_click
value_transfer.Form2 myfrm=new Form2("ttt");
myfrm.Owner=this;
myfrm.ShowDialog();
Form2_button2_click
//通過移交控制權來傳遞參數
value_transfer.Form1 myfrm=new Form1();
myfrm=(Form1)this.Owner;
myfrm.textBox1.Text=this.textBox1.Text;
//myfrm.textBox1.Text=myfrm.add(3,5).ToString();
//add和myfrm.textBox1設置成PUBLIC才可以訪問的。
(2) 通過FORM. DialogResult來進行參數傳遞
Form1_button1_click
value_transfer.Form2 myfrm=new Form2();
if (myfrm.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
this.textBox1.Text =myfrm.textBox1.Text;
}
Form2_button1_click
this.DialogResult=System.Windows.Forms.DialogResult.OK;
(3) 通過類的屬性來傳遞參數
//建立公共類庫
Public class test()
{
//設置私有靜態字符類型
Private static string temp;
Public class test()
{}
Public string s
{
Set
{this.temp=values;}
Get
{return this.temp;}
Form2_button1_click
value_transfer.Class1 myclass=new Class1();
myclass.test=this.textBox1.Text.Trim();
Form1_button1_click
value_transfer.Class1 myclass=new Class1();
this.textBox1.Text=myclass.test;
(4) 窗體傳遞參數
FORM2(STRING ADB);
#13
通过构造函数把值传过去就OK了
#14
供参考,如下:
我的Form1是父窗体
Form2是子窗体
每个窗体里包含一个ListBox
然后根据Form1里的ListBox的选值改变Form2里Listbox的选值
ListBox里我设置的是1,2,3,4,5,6 6个值然后可以对应改变
Form1代码如下:
Public Class Form1
Private Sub ListBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.Click
Form2.ListBox1.SelectedItem = Me.ListBox1.SelectedItem
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
End Class
Form2代码如下:
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MdiParent = Form1
End Sub
End Class
我的Form1是父窗体
Form2是子窗体
每个窗体里包含一个ListBox
然后根据Form1里的ListBox的选值改变Form2里Listbox的选值
ListBox里我设置的是1,2,3,4,5,6 6个值然后可以对应改变
Form1代码如下:
Public Class Form1
Private Sub ListBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.Click
Form2.ListBox1.SelectedItem = Me.ListBox1.SelectedItem
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
End Class
Form2代码如下:
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MdiParent = Form1
End Sub
End Class
#15
最痴呆的方法就是把控键变为public,然后在form1里面声明form2的变量,调用form2.text1.text
#16
提问的人抛坑把自己埋了!别说你是做程序的
#17
form2 f=new form2(this.textbox1.text,this.textbox2.text,this.textbox3.text)
f.show();
在form2 中定义
public Form1(string str1,string str2,string str3)
{
InitializeComponent();
this.textbox1.text=str1;
this.textbox2.text=str2;
this.textbox3.text=str3;
}
f.show();
在form2 中定义
public Form1(string str1,string str2,string str3)
{
InitializeComponent();
this.textbox1.text=str1;
this.textbox2.text=str2;
this.textbox3.text=str3;
}
#1
參見
http://blog.csdn.net/tjvictor/archive/2006/06/23/824617.aspx
http://blog.csdn.net/tjvictor/archive/2006/06/23/824617.aspx
#2
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.Show();
}
public void Test()
{
textBox1.Text = "aaaa";
}
******************************
public partial class Form2 : Form
{
Form1 frm;
public Form2(Form1 _frm)
{
InitializeComponent();
frm=_frm;
}
private void button1_Click(object sender, EventArgs e)
{
frm.Test();
}
private void button2_Click(object sender, EventArgs e)
{
frm.textBox1.Text = textBox1.Text;
}
}
#3
楼上的代码是传了整个form给form2,
你还可以只把form1的textbox传给form2,
默认为引用传递,所以很好控制
你还可以只把form1的textbox传给form2,
默认为引用传递,所以很好控制
#4
楼上正解~
#5
在form1定义3个静态变量,然后再form2操作改变,也可以实现。
#6
楼上想法真的独到啊,不知道两个事件并发会变成什么样子..
#7
在form1定义3个静态变量,然后再form2操作改变,也可以实现。
#8
oniu828的明显违反迪米特原则,还是让form2公开个方法来接收这三个值吧。
#9
private void Clearbtn_Click(object sender, EventArgs e)
{
clear frm = new clear(this.textBox1.Text,this.textBox2.Text,this.textBox3.Text);
frm.Show();
}
public clear(string tbS1,string tbS2,string tbS3)
{
InitializeComponent();
this.textBox1.Text = tbS1;
this.textBox2.Text = tbS2;
this.textBox3.Text = tbS3;
}
这样解决的 对吗?
{
clear frm = new clear(this.textBox1.Text,this.textBox2.Text,this.textBox3.Text);
frm.Show();
}
public clear(string tbS1,string tbS2,string tbS3)
{
InitializeComponent();
this.textBox1.Text = tbS1;
this.textBox2.Text = tbS2;
this.textBox3.Text = tbS3;
}
这样解决的 对吗?
#10
在form2类中设一个public 变量 a,在form1中
form2 b=new form2();
b.a=this.textbox1;
b.show();
form2 b=new form2();
b.a=this.textbox1;
b.show();
#11
namespace xx
{ public partial class test: Form
{
form2.x=.............
}
}
{ public partial class test: Form
{
form2.x=.............
}
}
#12
(1) 使用替換控制權來進行傳遞
Form1_button1_click
value_transfer.Form2 myfrm=new Form2("ttt");
myfrm.Owner=this;
myfrm.ShowDialog();
Form2_button2_click
//通過移交控制權來傳遞參數
value_transfer.Form1 myfrm=new Form1();
myfrm=(Form1)this.Owner;
myfrm.textBox1.Text=this.textBox1.Text;
//myfrm.textBox1.Text=myfrm.add(3,5).ToString();
//add和myfrm.textBox1設置成PUBLIC才可以訪問的。
(2) 通過FORM. DialogResult來進行參數傳遞
Form1_button1_click
value_transfer.Form2 myfrm=new Form2();
if (myfrm.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
this.textBox1.Text =myfrm.textBox1.Text;
}
Form2_button1_click
this.DialogResult=System.Windows.Forms.DialogResult.OK;
(3) 通過類的屬性來傳遞參數
//建立公共類庫
Public class test()
{
//設置私有靜態字符類型
Private static string temp;
Public class test()
{}
Public string s
{
Set
{this.temp=values;}
Get
{return this.temp;}
Form2_button1_click
value_transfer.Class1 myclass=new Class1();
myclass.test=this.textBox1.Text.Trim();
Form1_button1_click
value_transfer.Class1 myclass=new Class1();
this.textBox1.Text=myclass.test;
(4) 窗體傳遞參數
FORM2(STRING ADB);
Form1_button1_click
value_transfer.Form2 myfrm=new Form2("ttt");
myfrm.Owner=this;
myfrm.ShowDialog();
Form2_button2_click
//通過移交控制權來傳遞參數
value_transfer.Form1 myfrm=new Form1();
myfrm=(Form1)this.Owner;
myfrm.textBox1.Text=this.textBox1.Text;
//myfrm.textBox1.Text=myfrm.add(3,5).ToString();
//add和myfrm.textBox1設置成PUBLIC才可以訪問的。
(2) 通過FORM. DialogResult來進行參數傳遞
Form1_button1_click
value_transfer.Form2 myfrm=new Form2();
if (myfrm.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
this.textBox1.Text =myfrm.textBox1.Text;
}
Form2_button1_click
this.DialogResult=System.Windows.Forms.DialogResult.OK;
(3) 通過類的屬性來傳遞參數
//建立公共類庫
Public class test()
{
//設置私有靜態字符類型
Private static string temp;
Public class test()
{}
Public string s
{
Set
{this.temp=values;}
Get
{return this.temp;}
Form2_button1_click
value_transfer.Class1 myclass=new Class1();
myclass.test=this.textBox1.Text.Trim();
Form1_button1_click
value_transfer.Class1 myclass=new Class1();
this.textBox1.Text=myclass.test;
(4) 窗體傳遞參數
FORM2(STRING ADB);
#13
通过构造函数把值传过去就OK了
#14
供参考,如下:
我的Form1是父窗体
Form2是子窗体
每个窗体里包含一个ListBox
然后根据Form1里的ListBox的选值改变Form2里Listbox的选值
ListBox里我设置的是1,2,3,4,5,6 6个值然后可以对应改变
Form1代码如下:
Public Class Form1
Private Sub ListBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.Click
Form2.ListBox1.SelectedItem = Me.ListBox1.SelectedItem
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
End Class
Form2代码如下:
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MdiParent = Form1
End Sub
End Class
我的Form1是父窗体
Form2是子窗体
每个窗体里包含一个ListBox
然后根据Form1里的ListBox的选值改变Form2里Listbox的选值
ListBox里我设置的是1,2,3,4,5,6 6个值然后可以对应改变
Form1代码如下:
Public Class Form1
Private Sub ListBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.Click
Form2.ListBox1.SelectedItem = Me.ListBox1.SelectedItem
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
End Class
Form2代码如下:
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MdiParent = Form1
End Sub
End Class
#15
最痴呆的方法就是把控键变为public,然后在form1里面声明form2的变量,调用form2.text1.text
#16
提问的人抛坑把自己埋了!别说你是做程序的
#17
form2 f=new form2(this.textbox1.text,this.textbox2.text,this.textbox3.text)
f.show();
在form2 中定义
public Form1(string str1,string str2,string str3)
{
InitializeComponent();
this.textbox1.text=str1;
this.textbox2.text=str2;
this.textbox3.text=str3;
}
f.show();
在form2 中定义
public Form1(string str1,string str2,string str3)
{
InitializeComponent();
this.textbox1.text=str1;
this.textbox2.text=str2;
this.textbox3.text=str3;
}