我如何在C#中的另一种形式的条件下使用多个复选框 [英] How I Used Multiple Checkboxes In If Condition In Another Form In C#

查看:68
本文介绍了我如何在C#中的另一种形式的条件下使用多个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格1有八个复选框和一个文本框和一个按钮,当我在本文中写入数字时,我在水晶报表中打印其详细信息时,我选中了此复选框中的2或3或4或8。我需要为每个复选框创建选择



代码形式为: -



< pre lang =C#> public partial class FRM_RPT_CAR_Details:表格
{

public FRM_RPT_CAR_Details()
{
InitializeComponent();
}


私有 void button1_Click(< span class =code-keyword> object sender,EventArgs e)
{
RPT.FRM_RPT_CAR_ID frm = new RPT。 FRM_RPT_CAR_ID(txt_Car_id.Text);
frm.Show();
}
}






形式2中的
: - 有一个crystalReportViewr



  public   partial   class  FRM_RPT_CAR_ID:表格
{
int x;

SqlConnection con = new SqlConnection( Server =。; Database = WhiteWhaleDB; Integrated Security = true);
SqlCommand sd;
SqlDataReader sda;
string s;

public FRM_RPT_CAR_ID()
{
InitializeComponent();
}

public FRM_RPT_CAR_ID( string x)
{
InitializeComponent();

this .x = int .Parse(x);
}

private void FRM_RPT_CAR_ID_Load( object sender,EventArgs e)
{
FL.FRM_RPT_CAR_Details frm = new FL.FRM_RPT_CAR_Details();
if (frm.checkBox_Tashhem.Checked)
{
s = 选择s1.Car_id,将(varchar,s1.Change_date,101)转换为Change_date,Service_Type.Servce_desc,s1.Current_dist,s1.Prev_distance为Prev_dist,(s1.Current_dist - s1。 Prev_distance)作为Change_dist,s1.value_Tashhem作为来自Tashhem的值s1 INNER JOIN Service_Type ON s1.Servce_id = Service_Type.Servce_id其中s1.Car_id = + x;
DataSet_Car_id ds = new DataSet_Car_id();
SqlDataAdapter dataAdapter = new SqlDataAdapter(s,con);
dataAdapter.Fill(ds.Tables [ Car_Details]);
CrystalReport_Car_id report = new CrystalReport_Car_id();
report.SetDataSource(ds.Tables [ Car_Details]);
crystalReportViewer_Car_id.ReportSource = report;
crystalReportViewer_Car_id.Refresh();
}
}

解决方案

在包含CheckBoxes的表格中:



0.定义为Public a Dictionary< string,CheckState>名为'ChkBoxToChkState:



public Dictionary< string,> ChkBoxToChkState;



0.a.在表单加载事件中初始化该字典

  private   void  Form2_Load( object  sender,EventArgs e)
{
ChkBoxToChkState = new Dictionary< string,CheckState>
{
{checkBox1.Name,checkBox1.CheckState},
{checkBox2.Name,checkBox2.CheckState},
{checkBox3.Name,checkBox3.CheckState},
// 继续所有CheckBoxes
};
}

1。在窗体设计视图中:选择所有复选框



1.a.使用F4打开属性浏览器,单击事件选项卡



1.b.单击'CheckStateChanged条目,在代码中自动生成一个事件处理程序,如下所示:

  private  CheckBox currentCheckBox; 
private void checkBox1_CheckStateChanged( object sender,EventArgs e)
{
currentCheckBox = sender as CheckBox;
ChkBoxToChkState [currentCheckBox.Name] = currentCheckBox.CheckState;
}

在需要评估CheckBoxes状态的表单中:



0.假设您已经创建了一个实例具有名为'CBForm的CheckBox的表单,并且已经显示...即,您可以确保该表上的字典被正确实例化。



1.编写评估CheckBoxes的方法:

  private   void  EvaluateForm2CheckBoxState( )
{
var cBoxState = CBForm.ChkBoxToChkState;

if (cBoxState == null 返回;

if
cBoxState [ checkBox1] == CheckState.Checked
&& cBoxState [ checkBox2] == CheckState.Checked

{
// 规则#1 ....
// 做点什么
Console.WriteLine( rule 1);
}
其他 如果 false // if(?)...另一个测试
{
Console.WriteLine( rule?);
}
其他 // 最终测试
{
Console.WriteLine( 最终规则?);
}
}

注意这里做出的选择:使用Type'字符串作为字典中的键:这意味着用户(你)不能从外面更改CheckBoxes CheckState CheckBox表格。



如果你需要能够从主表单更改CheckState,那么你可以使用CheckBoxes作为字典中的键。



另请注意,这里假设访问CheckBoxes的值是按需完成的,而不是由某些用户操作触发(比如更改CheckBox中的CheckState值)在带有CheckBoxes的表单中。


i have form 1 that have eight Checkboxes and one text box and one button that when i write number in this text he print its details in crystal report when i checked 2 or 3 or 4 or 8 checked in this checkbox. and i need create select for each checked box

code in the form one is :-

public partial class FRM_RPT_CAR_Details : Form
{

    public FRM_RPT_CAR_Details()
    {
        InitializeComponent();
    }


    private void button1_Click(object sender, EventArgs e)
    {
        RPT.FRM_RPT_CAR_ID frm = new RPT.FRM_RPT_CAR_ID(txt_Car_id.Text);
        frm.Show();
    }
}




in form 2 :- that have a crystalReportViewr

public partial class FRM_RPT_CAR_ID : Form
    {
        int x;

        SqlConnection con = new SqlConnection("Server= .; Database= WhiteWhaleDB; Integrated Security = true");
        SqlCommand sd;
        SqlDataReader sda;
        string s;

        public FRM_RPT_CAR_ID()
        {
            InitializeComponent();
        }

        public FRM_RPT_CAR_ID(string x)
        {
            InitializeComponent();

            this.x = int.Parse(x);
        }

        private void FRM_RPT_CAR_ID_Load(object sender, EventArgs e)
        {
            FL.FRM_RPT_CAR_Details frm = new FL.FRM_RPT_CAR_Details();
            if (frm.checkBox_Tashhem.Checked)
            {
                s = "select s1.Car_id,  convert(varchar,s1.Change_date,101) as Change_date, Service_Type.Servce_desc,s1.Current_dist, s1.Prev_distance as Prev_dist, (s1.Current_dist - s1.Prev_distance) as Change_dist ,s1.value_Tashhem as Value from Tashhem s1 INNER JOIN Service_Type ON s1.Servce_id = Service_Type.Servce_id where s1.Car_id =" + x;
                DataSet_Car_id ds = new DataSet_Car_id();
                SqlDataAdapter dataAdapter = new SqlDataAdapter(s, con);
                dataAdapter.Fill(ds.Tables["Car_Details"]);
                CrystalReport_Car_id report = new CrystalReport_Car_id();
                report.SetDataSource(ds.Tables["Car_Details"]);
                crystalReportViewer_Car_id.ReportSource = report;
                crystalReportViewer_Car_id.Refresh();
            }
}

解决方案

In the Form with the CheckBoxes:

0. define as Public a Dictionary<string,CheckState> named 'ChkBoxToChkState:

public Dictionary<string,> ChkBoxToChkState;

0.a. initialize that Dictionary in the Form Load Event

private void Form2_Load(object sender, EventArgs e)
{
   ChkBoxToChkState = new Dictionary<string, CheckState>
   {
       {checkBox1.Name, checkBox1.CheckState},
       {checkBox2.Name, checkBox2.CheckState},
       {checkBox3.Name, checkBox3.CheckState},
       // continue for all CheckBoxes
   };
}

1. in the Form Design view: select all the CheckBoxes

1.a. use F4 to open the Property Browser, click the Events tab

1.b. click on the 'CheckStateChanged entry to automatically generate an Event Handler in your code that will look like this:

private CheckBox currentCheckBox;
private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
    currentCheckBox = sender as CheckBox;
    ChkBoxToChkState[currentCheckBox.Name] = currentCheckBox.CheckState;
}

In the Form that will need to evaluate the state of the CheckBoxes:

0. assume you have created an instance of the Form with the CheckBoxes named 'CBForm, and that it has been shown ... i.e., you can be sure the Dictionary on that Form is properly instantiated.

1. write the method that evaluates the CheckBoxes:

private void EvaluateForm2CheckBoxState()
{
    var cBoxState = CBForm.ChkBoxToChkState;

    if (cBoxState == null) return;

    if(
        cBoxState["checkBox1"] == CheckState.Checked
        && cBoxState["checkBox2"] == CheckState.Checked
    )
    {
        // rule #1 ....
        // do something
        Console.WriteLine("rule 1");
    }
    else if(false) // if(?) ... another test
    {
        Console.WriteLine("rule ?");
    }
    else // final test
    {
        Console.WriteLine("final rule ?");
    }
}

Note the choice made here: to use Type 'string as the Key in the Dictionary: that means the user (you) cannot change the CheckBoxes CheckState from outside the CheckBox Form.

If you need the ability to change the CheckState, from the Main Form, then you could use the CheckBoxes as the Key in the Dictionary.

Note also the assumption here that accessing the values of the CheckBoxes is done "on demand," rather than being triggered by some user-action (like changing the CheckState Value in a CheckBox in the Form with the CheckBoxes).


这篇关于我如何在C#中的另一种形式的条件下使用多个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆