首页 > 解决方案 > 在 c# 的不同类中检索最新的控件值(复选框、日期时间选择器)

问题描述

我想使用名为 GetProfileFilter 的函数访问最新的检查状态和最新的设置日期值。现在我的问题是,当 checkbox.Checkstate 更改或 datetimepicker 更改时,我无法获取最新值。我根据是否设置了复选框来设置值,并且我打算在其他地方使用 IBitWise 值.注意,在表单加载时,所有复选框都被选中。在表单类中,我只有几个变量以及按钮和复选框的事件处理程序以及日期时间选择器。向所有建议开放!

class Profile{
     public bool GetProfileFilter()
    {
        if (frmInactive.btnApplyWasClicked == true || frmInactive.btnCancelWasClicked == false)
        {
            frmInactive.ShowDialog();
            MessageBox.Show("Here");
            if (frmInactive.chkCancel.Checked == true)
            {
                MessageBox.Show("Cancel");
                IBitWise += Canceled;
            }
            if (frmInactive.chkDiscon.Checked == true)
            {
                MessageBox.Show("Discon");
                IBitWise += Discontinued;
            }
            if (frmInactive.chkVoidwoRes.Checked == true)
            {
                MessageBox.Show("Voidedwo");
                IBitWise += VoidedWoutRes;
            }
            if (frmInactive.chkVoidwRes.Checked == true)
            {
                MessageBox.Show("Voidedw");
                IBitWise += VoidedWRes;
            }  
            MessageBox.Show("Ibit value:" + IBitWise);
            return true;
        }
        else
        {
            return false;
        }    
    }

}

    public partial class FilterView : Form
{
    Utilities Utilities = new Utilities();
    //FilterView filterView = new FilterView();
    public FilterView()
    {
        InitializeComponent();
    }
    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void checkBox2_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void checkBox3_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void checkBox4_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void checkBox5_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void rsetbtn_Click(object sender, EventArgs e)
    {

    }

    private void aplybtn_Click(object sender, EventArgs e)
    {
        callonload();
    }

    private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {

    }

    private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
    {

    }

标签: c#winforms

解决方案


这是带有公开复选框的示例代码

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Profile profile = new Profile(this);
            profile.GetProfileFilter();
        }
    }
    class Profile
    {
        Form1 form1;

        public Profile(Form1 form1)
        {
            this.form1 = form1;
        }

        public bool GetProfileFilter()
        {
            form1.checkBox1.Checked = true;
            return true;
        }


    }
}

推荐阅读