簡體   English   中英

從另一個類訪問事件初始化變量

[英]Accessing an event initialized variable from another class

我正在嘗試使用由另一個類的拖放事件初始化的變量( fileName )。

理想情況下,如果fileName是一種全局變量,並且將通過拖放事件進行永久更新,那就太好了。 然后,我可以從另一個類中調用已經更新的變量。 可能嗎?

public void DragDropRichTextBox_DragDrop(object sender, DragEventArgs e)
{
    string[] fileName = e.Data.GetData(DataFormats.FileDrop) as string[];

    if (fileName != null)
    {
        foreach (string name in fileName)
        {
            try
            {
                AppendText(BinaryFile.ReadString(name);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

變量fileName包含從中拖動文件的目錄。 我想在自定義數據打印功能的單獨類中使用該目錄,該目錄需要文件的路徑。

如果我正確理解了這個問題,那么以下選項可能會為您完成這項工作:

  1. 將FileNames公開為可由其他類引用的屬性。
  2. 使用您自己的事件轉發您在DragDropRichTextBox_DragDrop事件句柄上接收的數據,例如:

     public class YourClass { // The other class would register to this event public event Action<string[]> TextBoxDropDown; public void DragDropRichTextBox_DragDrop( object sender,DragEventArgs e) { string[] fileNames = e.Data.GetData(DataFormats.FileDrop) as string[]; if (fileNames != null&&TextBoxDropDown !=null) { TextBoxDropDown(fileNames); } } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM