Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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 :-

C#
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

C#
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();
            }
}
Posted
Comments
ZurdoDev 25-Jan-16 10:13am    
I don't understand your question.
Kevin Marois 25-Jan-16 10:22am    
Are you asking how to get the values from the checkboxes in one form into another form?
Member 12244977 25-Jan-16 11:10am    
yes :)
Kevin Marois 25-Jan-16 11:13am    
Simple. Create a class that holds the checkbox values, then pass that class to the other form
Member 12244977 25-Jan-16 12:11pm    
tell me how i do this please ?

1 solution

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
C#
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:
C#
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:
C#
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).
 
Share this answer
 
Comments
Philippe Mori 25-Jan-16 21:38pm    
Not really the code I would recommend... What it the purpose of the dictionary if entry are indexed by control name? It would just make the code more fragile if the control get renamed to some more adequate name by a future maintainer...

In that case, you won't get any benefit from the easier hack to make the checkboxes themselves public... In almost any case, you want to use another object to represent the data or in simple case, extra argument to the constructor or properties to the form).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900