C#非静态引用属性 窗体传值 类已经实例化,但是不能引用

其中一个窗体代码 Form3
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form3 : Form
{

    public Counter c1
    {
        get
        {
            Counter c1 = new Counter();
            c1.dataok = this.intok;
            c1.datapass = this.intpass;
            c1.datano = this.intno;
            c1.datatotal = this.inttotal;
            return c1;
        }
        set
        {
            Counter c1 = new Counter();
            c1.dataok = this.intok;
            c1.datapass = this.intpass;
            c1.datano = this.intno;
            c1.datatotal = this.inttotal;
        }
    }
    //设置三个对应认可度的变量并赋初值
    public int intok = 0;
    public int intpass = 0;
    public int intno = 0;
    public int inttotal = 0;
    public Form3()
    {
        InitializeComponent();
    }
    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        if (this.radioButton1.Checked)
        {
            this.intok = this.intok + 1;
        }
    }
    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        if (this.radioButton2.Checked)
        {
            this.intpass = this.intpass + 1;
        }
    }
    private void radioButton3_CheckedChanged(object sender, EventArgs e)
    {
        if (this.radioButton3.Checked)
        {
            this.intno = this.intno + 1;
        }
    }

    private void Form3_Load(object sender, EventArgs e)
    {
        //默认初始未选中
        this.radioButton1.Checked = false;
        this.radioButton2.Checked = false;
        this.radioButton3.Checked = false;

    }
    //当三个按钮有变化时自加
    private void button1_Click(object sender, EventArgs e)
    {
        //提交时累加  并关闭调查窗口  显示窗口一
        if (radioButton1.Checked)
            c1.dataok++;
        if (radioButton2.Checked)
            c1.datapass++;
        if (radioButton3.Checked)
            c1.datano++;
        //如果未选择过任何一个按钮  总数不自加
        if (intok != 0 || intno != 0 || intpass != 0)
            c1.datatotal++;


        this.Hide();
        Form1 frm1 = new Form1();
        frm1.Show();
    }


}

}

            另一个窗体代码  Form4
            using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}

    private void button1_Click(object sender, EventArgs e)
    {
        //用户选择退出时 关闭此窗口   回到窗口一
        this.Close();
        Form1 frm1 = new Form1();
        frm1.Show();
    }

    private void Form4_Load(object sender, EventArgs e)
    {
        //页面加载显示
        this.label5.Text = c1.datatotal.ToString();
        this.label2.Text = c1.dataok.ToString();
        this.label3.Text = c1.datapass.ToString();
        this.label4.Text = c1.datano.ToString();
    }

    private void label5_Click(object sender, EventArgs e)
    {

    }
}

}
Form3中的类已经实例化,Form4引用后c1不能识别,是什么问题呢

1个回答

你每次访问的都是你new的那个窗口,而不是之前已经显示的那个,所以悲剧了。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!