0

Requirement: I have a windows application written in C# and I'm trying to add a checkbox to it where if it is checked, than the files from the search will be copied into subdirectories based on zip code.

Problem: When I reference addzipdir_checkBox.Equals(true) from MainForm.cs on a different page SearchProcess.cs I get the error: "addzipdir_checkBox does not exist in the current context". What is the proper way to reference the checkBox_CheckedChanged occurence?

Here's the code on MainForm.cs:

    private void addzipdir_checkBox_CheckedChanged(object sender, EventArgs e)
    {
        if (addzipdir_checkBox.Equals(true))
        {
           Log("Organize files by zip code.");
        }

        if (addzipdir_checkBox.Equals(false))
        {
           Log("Don't Organize files by zip code.");
        }
    }

Here's the code on SearchProcess.cs generating an error:

        if (addzipdir_checkBox.Equals(true))
        {

            // adds the given lead's agentid and zip code to the targetpath string 
            string targetzipdir = m_sc.get_TargetPath() + "\\" + AgentID + "\\" + ZIP;

            // If the given lead's zip code subdirectory doesn't exist, create it.
            if (!Directory.Exists(targetzipdir))
            {
                Directory.CreateDirectory(targetzipdir);
            }

            targetFileAndPath = m_sc.get_TargetPath() + "\\" + AgentID + "\\" + ZIP + "\\" + fullFileName;
        }   // end if addzipdir_checkBox.Equals(true)
2
  • 2
    I believe checking the box should update a variable somewhere so that you can reference the variable and not the control which is on a different form. Feb 24, 2011 at 21:45
  • I added the line bool addzipdirectory_checkbox=1; under the first if statement, if (addzipdir_checkBox.Equals(true)) but i still couldn't reference it in the searchprocess.cs file. should i use the invoke statement to bring it in like this m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on State: " + m_sc.get_State() }); or if (m_sc.get_SearchAND()==true) { m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search for All" }); } or bring it in from the Utility.Properties.Settings.Default. area? Feb 25, 2011 at 14:28

2 Answers 2

0
  • You need to make sure addzipdir_checkBox is public. For this, you need to use the form editor, select the addzipdir_Checkbox and change the property grid 'Modifiers' item to public or internal.

  • Then, you need to find a way to reference the instance of this form, something like this:

    if (myForm.addzipdir_checkBox.Equals(true)) { ... ... }

4
  • Thanks for the response! I made the modifier property public for addzipdir_Checkbox but when I added if (m_form.addzipdir_checkbox() = 1) I get the error: MainForm doesn't contain definition for addzipdir_checkbox and no extension method addzipdir_Checkbox (are you missing a using directive or assembly reference?). I also tried putting if (m_form.get_addzipdir_checkbox() = 1) and that came up with the same error. Feb 25, 2011 at 14:56
  • My MainForm file is using the following: using System; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; ---------------------------------- and SearchProcess is using the following: using System; using System.Windows.Forms; using System.Threading; using Com.StellmanGreene.CSVReader; using System.IO; using System.Data; using System.Collections.Generic; Feb 25, 2011 at 14:57
  • @Brian - what's the type of checkbox? Maybe you want to do something like 'if (m_form.addzipdir_checkbox.Checked) {...}' ? Feb 25, 2011 at 15:14
  • that didn't work either. it still doesnt recognize the addzipdir_checkbox value. how do i look up the checkbox type? Feb 25, 2011 at 16:38
0

I found that if you right click a variable and click "Go To Definition" it's a lot easier to find where the variable is referenced. I right clicked another variable that was called in through the MainForm and found that they were all called through the SearchCriteria file. I had to bring the addzipdir_checkbox value referenced on the MainForm.cs file through the SearchCriteria.cs file and then call it in the SearchProcess.cs file.

Here's my code on the SearchCriteria.cs file:

public class SearchCriteria
{
    private String Corp;
    private String OrderNumber;
    private String Campaign;
    private String City;
    private String State;
    private String Zip;
    private String SourcePath;
    private String TargetPath;
    private bool SearchOR;
    private bool SearchAND;
    private bool addzipdirectory_checkBox;

    public SearchCriteria()
    {
    }

    public SearchCriteria(String Corp,
                          String OrderNumber,
                          String Campaign,
                          String City,
                          String State,
                          String Zip,
                          String SourcePath,
                          String TargetPath,
                          bool SearchOR,
                          bool SearchAND,
                          bool addzipdirectory_checkBox)              
    {
        this.Corp = Corp;
        this.OrderNumber = OrderNumber;
        this.Campaign = Campaign;
        this.City = City;
        this.State = State;
        this.Zip = Zip;
        this.SourcePath = SourcePath;
        this.TargetPath = TargetPath;
        this.SearchOR = SearchOR;
        this.SearchAND = SearchAND;
        this.addzipdirectory_checkBox = addzipdirectory_checkBox;
    }
    public bool get_addzipdir_checkBox()
    {
        return addzipdirectory_checkBox;
    }
    public void set_addzipdir_checkBox(bool x)
    {
        addzipdirectory_checkBox = x;
    }
}               

Here's my code on the Searchprocess.cs file:

            // Copy the file if ANY of the search criteria have been met
            if (found)
            {

                m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"FOUND: Order_No: " + Order_No +
                                                                        " barcode: " + barcode +
                                                                        " MailerCode: " + MailerCode +
                                                                        " AgentID: " + AgentID +
                                                                        " City: " + City +
                                                                        " State: " + State +
                                                                        " ZIP: " + ZIP});


                //passes values to TransferFile 
                TransferFile(directory, barcode, AgentID, ZIP);
            }
        } // end for that finds each matching record 

    }

    // find and copy the file to the target directory string ZIP
    private void TransferFile(string sourceDir, string filename, string AgentID, string ZIP)
    {
        string fullFileName = filename + ".pdf";
        string fullFileNameAndPath = sourceDir + "\\" + fullFileName;
        string targetFileAndPath;

        if (m_sc.get_addzipdir_checkBox()==true)
        {

            // adds the given lead's agentid and zip code to the targetpath string 
            string targetzipdir = m_sc.get_TargetPath() + "\\" + AgentID + "\\" + ZIP;

            // If the given lead's zip code subdirectory doesn't exist, create it.
            if (!Directory.Exists(targetzipdir))
            {
                Directory.CreateDirectory(targetzipdir);
            }

            targetFileAndPath = m_sc.get_TargetPath() + "\\" + AgentID + "\\" + ZIP + "\\" + fullFileName;
        }   // end if addzipdir_checkBox.Equals(true)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.