2011-02-24 31 views
0

要求:我有一個用C#編寫的Windows應用程序,我試圖在其中添加一個複選框,如果它被選中,比搜索的文件將複製到基於子目錄的在郵政編碼上。C#checkBox_CheckedChanged在不同文件中引用

問題:當我引用來自MainForm.cs addzipdir_checkBox.Equals(真)在不同的頁面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

我相信檢查框應更新變量的地方,讓你可以引用變量而不是其他形式的控件。 – 2011-02-24 21:45:54

+0

我加了一行bool addzipdirectory_checkbox = 1;在第一個if語句下,if(addzipdir_checkBox.Equals(true))但我仍然無法在searchprocess.cs文件中引用它。我應該使用invoke語句來使它像這樣m_form.Invoke(m_form.m_DelegateAddString,new Object [] {「Search on State:」+ m_sc.get_State()});或者如果(m_sc.get_SearchAND()== true) m_form.Invoke(m_form.m_DelegateAddString,new Object [] {「Search for All」}); }或從Utility.Properties.Settings.Default中將其帶入。區? – 2011-02-25 14:28:53

回答

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) 
0
  • 你需要確保addzipdir_checkBox是公開的。爲此,您需要使用表單編輯器,選擇addzipdir_Checkbox並將屬性網格的「修改器」項目更改爲publicinternal

  • 然後,你需要找到一種方式來引用這種形式的情況下,這樣的事情:

    如果(myForm.addzipdir_checkBox.Equals(真)){ ... ... }

+0

感謝您的迴應!我做了addzipdir_Checkbox的公共修飾符屬性,但是當我添加if(m_form.addzipdir_checkbox()= 1)時,我得到錯誤:MainForm不包含addzipdir_checkbox的定義,也沒有擴展方法addzipdir_Checkbox(你是否缺少using指令或程序集引用?)。我也試着把if(m_form.get_addzipdir_checkbox()= 1)和那個出現了相同的錯誤。 – 2011-02-25 14:56:06

+0

我的MainForm文件正在使用以下內容:using System;使用系統。ComponentModel;使用System.Drawing;使用System.Linq;使用System.Text;使用System.Windows.Forms;使用System.Threading; ----------------------------------和SearchProcess使用以下內容:using System;使用System.Windows.Forms;使用System.Threading;使用Com.StellmanGreene.CSVReader;使用System.IO;使用System.Data;使用System.Collections.Generic; – 2011-02-25 14:57:06

+0

@布萊恩 - 什麼類型的複選框?也許你想要做一些像'if(m_form.addzipdir_checkbox.Checked){...}'? – 2011-02-25 15:14:50

相關問題