.net 将值从外部类传递到windows窗体
我试图将一些值传递给外部类的方法,以便从windows窗体进行计算,然后将解的值返回到窗体。 这是Form1.cs,我在这里声明了属性.net 将值从外部类传递到windows窗体,.net,winforms,.net,Winforms,我试图将一些值传递给外部类的方法,以便从windows窗体进行计算,然后将解的值返回到窗体。 这是Form1.cs,我在这里声明了属性 public partial class Form1 : Form { private string _info; private string _info2; private string _info3; private string _info4; private string _info5; private
public partial class Form1 : Form
{
private string _info;
private string _info2;
private string _info3;
private string _info4;
private string _info5;
private string _info6;
public string info { set { _info = value; } get { return _info; } }
public string info2 { set { _info2 = value; } get { return _info2; } }
public string info3 { set { _info3 = value; } get { return _info3; } }
public string info4 { set { _info4 = value; } get { return _info4; } }
public string info5 { set { _info5 = value; } get { return _info5; } }
public string info6 { set { _info6 = value; } get { return _info6; } }
现在我正在调用button\u click事件处理程序上的外部类
private void button1_Click(object sender, EventArgs e)
{
if (textBox8.Text != null && textBox9.Text != null && textBox10.Text != null && textBox11.Text != null && textBox13.Text != null)
{
AT at = new AT();
at.cal_cir(textBox8.Text, textBox9.Text, textBox10.Text, textBox11.Text, textBox13.Text);
label39.Text = _info;
label40.Text = _info2;
label41.Text = _info3;
label42.Text = _info4;
label43.Text = _info5;
label44.Text = _info6;
}
这是外部类
class AT
{ /* public string per_elon{get {return per_elon;}}
public string elongation { get { return elongation; } }
public string gauge_len { get { return gauge_len; } }
public string area { get { return area; } }
public string yield_str { get { return yield_str; } }
public string ultimate_str { get { return ultimate_str; } }*/
public void cal_cir (string a, string b, string c, string d, string e)
{
double width = Convert.ToDouble(a);
double thickness = Convert.ToDouble(b);
double final_len = Convert.ToDouble(e);
int yield = Convert.ToInt16(c);
int ultimate = Convert.ToInt16(d);
var area = width * thickness;
var gauge_len = (double)5.65 * (Math.Sqrt(area));
var elongation = final_len - gauge_len;
var per_elon = (elongation / gauge_len) * 100;
var yield_str = yield / area;
Math.Round(yield_str);
var ultimate_str = ultimate / area;
Math.Round(ultimate_str);
Form1 frm = new Form1();
frm.info = per_elon.ToString();
frm.info2 = elongation.ToString();
frm.info3 = gauge_len.ToString();
frm.info4 = area.ToString();
frm.info5 = yield_str.ToString();
frm.info6 = ultimate_str.ToString();
但是它并没有显示数据……请有人帮忙,只有这一部分是至关重要的。这是开始处理oop时重复最多的问题之一。当你在做
Form1 frm = new Form1();
在cal\u cir
方法中,您正在创建Form1
的新实例。您没有将值传递给已经存在(显示)的表单实例。可能有100种不同的方法来解决这个问题,但我将给您两个简单的解决方法:
将表单实例传递给您的AT
类
private void button1_Click(object sender, EventArgs e)
{
if (textBox8.Text != null && textBox9.Text != null && textBox10.Text != null && textBox11.Text != null && textBox13.Text != null)
{
AT at = new AT(this);
at.cal_cir(textBox8.Text, textBox9.Text, textBox10.Text, textBox11.Text, textBox13.Text);
label39.Text = _info;
label40.Text = _info2;
label41.Text = _info3;
label42.Text = _info4;
label43.Text = _info5;
label44.Text = _info6;
}
}
class AT
{
public AT(Form1 parent) { this.parent = parent; }
Form1 parent;
public void cal_cir (string a, string b, string c, string d, string e)
{
double width = Convert.ToDouble(a);
double thickness = Convert.ToDouble(b);
double final_len = Convert.ToDouble(e);
int yield = Convert.ToInt16(c);
int ultimate = Convert.ToInt16(d);
var area = width * thickness;
var gauge_len = (double)5.65 * (Math.Sqrt(area));
var elongation = final_len - gauge_len;
var per_elon = (elongation / gauge_len) * 100;
var yield_str = yield / area;
Math.Round(yield_str);
var ultimate_str = ultimate / area;
Math.Round(ultimate_str);
//Form1 frm = new Form1(); avoid this
parent.info = per_elon.ToString();
parent.info2 = elongation.ToString();
parent.info3 = gauge_len.ToString();
parent.info4 = area.ToString();
parent.info5 = yield_str.ToString();
parent.info6 = ultimate_str.ToString();
}
}
或者(更好)
我可以看到您正试图使用
frm
在classAT
中传递计算值。frm
是如何设置的?…创建实例时,您没有通过构造函数将其传递到AT
。@user2423661如果答案左上角的绿色勾号对您有帮助,您应该接受答案。这是SO鼓励的做法。
public void cal_cir (string a, string b, string c, string d, string e)
{
double width = Convert.ToDouble(a);
double thickness = Convert.ToDouble(b);
double final_len = Convert.ToDouble(e);
int yield = Convert.ToInt16(c);
int ultimate = Convert.ToInt16(d);
var area = width * thickness;
var gauge_len = (double)5.65 * (Math.Sqrt(area));
var elongation = final_len - gauge_len;
var per_elon = (elongation / gauge_len) * 100;
var yield_str = yield / area;
Math.Round(yield_str);
var ultimate_str = ultimate / area;
Math.Round(ultimate_str);
//infos are properties of AT class to be exposed publicly
info = per_elon.ToString();
info2 = elongation.ToString();
info3 = gauge_len.ToString();
info4 = area.ToString();
info5 = yield_str.ToString();
info6 = ultimate_str.ToString();
}
//and from your form class
private void button1_Click(object sender, EventArgs e)
{
if (textBox8.Text != null && textBox9.Text != null && textBox10.Text != null && textBox11.Text != null && textBox13.Text != null)
{
AT at = new AT();
at.cal_cir(textBox8.Text, textBox9.Text, textBox10.Text, textBox11.Text, textBox13.Text);
label39.Text = at.info;
label40.Text = at.info2;
label41.Text = at.info3;
label42.Text = at.info4;
label43.Text = at.info5;
label44.Text = at.info6;
}
}