weixin_44073486 2018-12-26 18:26 采纳率: 0%
浏览 614

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条回答 默认 最新

  • threenewbee 2018-12-26 17:00
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛