-1

我正在尝试将一些值传递给外部类的方法以从 Windows 窗体进行计算,然后将解决方案值返回给窗体。这是我在这里声明属性的 Form1.cs

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_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();

但它没有显示数据..请有人帮助只有这部分是至关重要的..

4

1 回答 1

1

这是开始处理 oop 时最重复的问题之一。当你在做

Form1 frm = new Form1(); 

在您的cal_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();
    }
}

或者(甚至更好),

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;
    }
}
于 2013-05-27T05:05:20.557 回答