C#checkBox_CheckedChanged在不同的文件中引用

时间:2011-02-24 21:43:32

标签: c# forms wpf-controls checkbox

要求:我有一个用C#编写的Windows应用程序,我正在尝试添加一个复选框,如果选中它,那么搜索中的文件将被复制到基于的子目录中邮政编码。

问题:当我从另一个页面上的MainForm.cs中引用addzipdir_checkBox.Equals(true)时,SearchProcess.cs出现错误:“当前上下文中不存在addzipdir_checkBox”。引用checkBox_CheckedChanged出现的正确方法是什么?

这是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.");
        }
    }

以下是SearchProcess.cs上生成错误的代码:

        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 个答案:

答案 0 :(得分:0)

  • 您需要确保addzipdir_checkBox是公开的。为此,您需要使用表单编辑器,选择addzipdir_Checkbox并将属性网格“修饰符”项更改为publicinternal

  • 然后,您需要找到一种方法来引用此表单的实例,如下所示:

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

答案 1 :(得分:0)

我发现如果右键单击一个变量并单击“转到定义”,则可以更容易地找到引用变量的位置。我右键单击了另一个通过MainForm调用的变量,发现它们都是通过SearchCriteria文件调用的。我必须通过SearchCriteria.cs文件带来MainForm.cs文件中引用的addzipdir_checkbox值,然后在SearchProcess.cs文件中调用它。

这是我在SearchCriteria.cs文件中的代码:

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;
    }
}               

这是我在Searchprocess.cs文件中的代码:

            // 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)