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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?