public partial class Calculator : Form
{
Price myPrice = new Price();
decimal dorm = 0;
decimal meal = 0;
public Calculator()
{
InitializeComponent();
}
private void getPriceButton_Click(object sender, EventArgs e)
{
decimal price = 0;
getInput();
price = dorm + meal;
myPrice.ShowDialog();
}
private void getInput()
{
if(allenRadioButton.Checked)
{
dorm = 1500;
}
if(pikeRadioButton.Checked)
{
dorm = 1600;
}
if(farthingRadioButton.Checked)
{
dorm = 1800;
}
if(universityRadioButton.Checked)
{
dorm = 2500;
}
if(sevenRadioButton.Checked)
{
meal = 600;
}
if(fourteenRadioButton.Checked)
{
meal = 1200;
}
if(unlimitedRadioButton.Checked)
{
meal = 1700;
}
}
这是form2(Price)代码:
public partial class Price : Form
{
Calculator myCalulator = new Calculator();
public Price()
{
InitializeComponent();
}
priceLabel.Text = price.myCalculator.TosString("c");
}
答案 0 :(得分:1)
您可以以第二种形式创建价格变量,并将价格传递给价格形式的构造函数:
public string price;
public Price(string price)
{
this.price = price;
InitializeComponent();
}
private void getPriceButton_Click(object sender, EventArgs e)
{
decimal price = 0;
getInput();
price = dorm + meal;
Price myPrice = new Price(price)
myPrice.ShowDialog();
}
答案 1 :(得分:0)
将价格作为参数传递给价格表格的新参数化构造函数,并将其用于进一步操作。
private decimal _price;
public Price(decimal pPrice)
{
InitializeComponent();
_price = pPrice;
...
}
还使用此新构造函数在按钮单击事件中实例化myPrice对象。等;
Price myPrice = new Price(price);
myPrice.ShowDialog();
有关将值从一个表单传递给另一个表单的其他方法,请参阅此链接; How can I pass values from one form to another?以及Passing Parameters back and forth between forms in C#
答案 2 :(得分:0)
你可以简单地通过添加一个变量并在windows窗体中公开它来做到这一点。
public int PassValue
在调用表单之前将值传递给它
form1 obj = new form1();
obj.PassValue = 34;
obj.Show();