作者b9433056 (阿哩唷 ^.^ )
看板C_Sharp
标题[问题]多表单传值问题?
时间Sun May 1 12:22:49 2011
现在有二个表单Form1与Form2
还有一个类别 class.cs
在From1输入的资料
会送到class.cs去做判读
之後转往Form2去做运算
Form2会有个数值与class.cs判读的资料做运算
我想问的是
如何在Form2中保留Form1送往class.cs的判读资料
因为在Form2中
class one = new class
这样在Form1的资料就都会被洗掉
谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.171.232.9
1F:推 tigertiger:指到同一个@@? 05/01 13:02
2F:→ b9433056:是指到同一个没错~ 05/01 14:24
3F:→ ssccg:在转Form2的时候把Form1中的class物件传过去,不要重new一个 05/01 14:58
4F:→ b9433056:那请问楼上的大大~怎麽转 05/01 15:01
5F:→ apiod:Form form2 = new form(class); 05/01 18:48
6F:→ b9433056:使用楼上的用法~不能解决问题哩 05/01 19:19
7F:→ james732:我会建议你把目前的程式码简化後贴上来,让大家帮你修改 05/01 19:46
以下为Form1的表单程式码
public void btnNext_Click(object sender, EventArgs e)
{
human safe = new human();
safe.age = Convert.ToInt32(txtAge.Text);
if (safe.age >= 20 == safe.age <= 120)
{
this.Hide();
frmSecond f2 = new frmSecond();
f2.Show();
}
if (rdbBoy.Checked)
{
safe.sex = 1;
}
if (rdbGirl.Checked)
{
safe.sex = 0;
}
}
以下为Form2的表单程式码
public void btnSpreadsheets_Click(object sender, EventArgs e)
{
double insurance;
int price;
price = Convert.ToInt32(txtPrice.Text);
insurance = price * 0.08 * safe.factory;
if (safe.sex == 1) lblResult.Text = "您是男性" + safe.age + "岁,
";
if (safe.sex == 0) lblResult.Text = "您是女性" + safe.age + "岁,
";
lblResult.Text += "您的保险试算金额为" + insurance + "元。";
}
以下为类别的程式码
public class human
{
public double factory;
private int _age;
public int age
{
get
{
return _age;
}
set
{
if (value < 20) MessageBox.Show("未满20岁无法驾车!");
if (value > 120) MessageBox.Show("输入之年龄有问题!");
_age=value;
}
}
private int _sex;
public int sex
{
get
{
return _sex;
}
set
{
if (value == 1)
{
if (age >= 20 == age < 30) factory = 1.2;
if (age >= 30 == age < 50) factory = 1;
if (age >= 50) factory = 0.9;
}
if (value == 0)
{
if (age >= 20 == age < 30) factory = 1;
if (age >= 30 == age < 50) factory = 0.9;
if (age >= 50) factory = 1.2;
}
_sex = value;
}
在Form1里输入的数值会传到类别去做判读
然後在Form2里
要把在Form1判读的延续至Form2中使用
麻烦各位解答 谢谢
※ 编辑: b9433056 来自: 118.171.232.9 (05/01 19:54)
8F:→ apiod:在你new form2的时候把class传过去阿 是哪里不行? 05/01 20:17
9F:→ b9433056:说~“型别”不能当变数使用 05/01 20:21
10F:→ james732:你可以把这个错误的相关程式码贴上来吗? 05/01 20:22
11F:→ james732:可以的话把整个专案压缩後放在免空让人下载 XD 05/01 20:22
12F:→ b9433056:程式码都贴上来了~ 05/01 20:23
13F:→ b9433056:以上程式码目前的错误在於 05/01 20:27
14F:→ b9433056:Form2没办法判读在Form1的变数 05/01 20:27
15F:→ b9433056:在Form2中所有safe.*的变数该怎麽执行才会跟Form1一样 05/01 20:28
16F:→ james732:想办法传过去罗,看要在建构子传或另外写个method传 05/01 20:29
17F:→ b9433056:我也是想破头了才来求救的~拜托了~ 05/01 20:30
18F:→ james732:如果你把整个专案放在网路上 我就改改看 05/01 20:31
19F:→ b9433056:这个我就不错了~去哪找网路空间? 05/01 20:33
20F:→ b9433056:错修改为“会”~没使用过网路硬碟 05/01 20:34
23F:→ james732:哈哈 坦白说我也很少用免空 都是丢hinet空间 XD 05/01 20:36
24F:→ apiod:frmfirst.cs => frmSecond f2 = new frmSecond(safe); 05/01 20:55
25F:→ apiod:frmSecond.cs ↓ 05/01 20:55
26F:→ apiod:human safe = new human(); 05/01 20:55
27F:→ apiod:public frmSecond(human safe) 05/01 20:56
28F:→ apiod:{ 05/01 20:56
29F:→ apiod: InitializeComponent(); 05/01 20:56
30F:→ apiod: this.safe = safe; 05/01 20:56
31F:→ apiod:} 05/01 20:56
32F:→ b9433056:感谢二位大大热心指导 05/01 20:57
※ 编辑: b9433056 来自: 118.171.232.9 (05/01 21:49)
※ 编辑: b9433056 来自: 118.171.232.9 (05/01 22:08)