当前上下文中不存在 c#

发布于 2024-09-27 14:04:05 字数 1664 浏览 14 评论 0原文

在我的 win 表单应用程序中,我有一个列表框和一个文本框,该应用程序从服务器获取电子邮件并在列表框中显示主题等,当我单击列表框时,正文显示在文本框中。问题是我必须在选定的索引更改事件中重复下面的整个代码才能使其正常工作,否则我会收到“当前上下文中不存在”错误,这会减慢应用程序的速度。

// Create an object, connect to the IMAP server, login,
        // and select a mailbox.
        Chilkat.Imap imap = new Chilkat.Imap();
        imap.UnlockComponent("");
        imap.Port = 993;
        imap.Ssl = true;
        imap.Connect("imap.gmail.com");
        imap.Login("[email protected]", "pass");
        imap.SelectMailbox("Inbox");

        // Get a message set containing all the message IDs
        // in the selected mailbox.
        Chilkat.MessageSet msgSet;
        msgSet = imap.Search("ALL", true);

        // Fetch all the mail into a bundle object.
        Chilkat.EmailBundle bundle = new Chilkat.EmailBundle();
        bundle = imap.FetchBundle(msgSet);

        // Loop over the bundle and display the From and Subject.
        Chilkat.Email email;
        int i;
        for (i = 0; i < bundle.MessageCount - 1; i++)
        {

            email = bundle.GetEmail(i);
            listView1.Items.Add(email.From + ": " + email.Subject).Tag = i;


            richTextBox1.Text = email.Body;

        }

        // Save the email to an XML file
        bundle.SaveXml("bundle.xml");

这是我想要在选定的索引更改事件中工作的代码:

 if (listView1.SelectedItems.Count > 0)
        {
            richTextBox1.Text = bundle.GetEmail((int)listView1.SelectedItems[0].Tag).Body;
        }

当我使用此代码时,我收到错误“当前上下文中不存在捆绑包”;我该如何修复这个错误?

In my win forms app I have a listbox and a textbox the app gets email from a server and displays the subject etc in the listbox and when I click the listbox the body is shown in the textbox. The problem is I have to repeat the entire code below in the selected index changed event to get it to work otherwise I get "does not exists in current context" error this slows down the app.

// Create an object, connect to the IMAP server, login,
        // and select a mailbox.
        Chilkat.Imap imap = new Chilkat.Imap();
        imap.UnlockComponent("");
        imap.Port = 993;
        imap.Ssl = true;
        imap.Connect("imap.gmail.com");
        imap.Login("[email protected]", "pass");
        imap.SelectMailbox("Inbox");

        // Get a message set containing all the message IDs
        // in the selected mailbox.
        Chilkat.MessageSet msgSet;
        msgSet = imap.Search("ALL", true);

        // Fetch all the mail into a bundle object.
        Chilkat.EmailBundle bundle = new Chilkat.EmailBundle();
        bundle = imap.FetchBundle(msgSet);

        // Loop over the bundle and display the From and Subject.
        Chilkat.Email email;
        int i;
        for (i = 0; i < bundle.MessageCount - 1; i++)
        {

            email = bundle.GetEmail(i);
            listView1.Items.Add(email.From + ": " + email.Subject).Tag = i;


            richTextBox1.Text = email.Body;

        }

        // Save the email to an XML file
        bundle.SaveXml("bundle.xml");

and here is the code I would like to get to work in the selected index changed event:

 if (listView1.SelectedItems.Count > 0)
        {
            richTextBox1.Text = bundle.GetEmail((int)listView1.SelectedItems[0].Tag).Body;
        }

When I use this code I get the error "bundle does not exist in the current context"; how do I fix this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橙幽之幻 2024-10-04 14:04:05

看来您必须重新设计代码,以便您感兴趣的对象在需要它的上下文中可用。一种解决方案可能是:

class Form1
{
 ...

 // You need to have the bundle available in your event handler, so it should be 
 // a field (or property) on the form:
 Chilkat.EmailBundle bundle;

 // Call this e.g. on start up and possibly when a
 // refresh button is clicked:
 protected void RefreshMailBox()
 {
  Chilkat.Imap imap = new Chilkat.Imap();
  imap.UnlockComponent("");
  imap.Port = 993;
  imap.Ssl = true;
  imap.Connect("imap.gmail.com");
  imap.Login("[email protected]", "pass");
  imap.SelectMailbox("Inbox");

  Chilkat.MessageSet msgSet = imap.Search("ALL", true); 
  bundle = imap.FetchBundle(msgSet);
 }

 protected void YourEventHandler()
 {
  if (listView1.SelectedItems.Count > 0)
  {
   // bundle is now accessible in your event handler:
   richTextBox1.Text = bundle.GetEmail((int)listView1.SelectedItems[0].Tag).Body;
  }
 }

 ...
}

It seems that you have to redesign your code so that the object you are interested in is available in the context that needs it. One solution might be:

class Form1
{
 ...

 // You need to have the bundle available in your event handler, so it should be 
 // a field (or property) on the form:
 Chilkat.EmailBundle bundle;

 // Call this e.g. on start up and possibly when a
 // refresh button is clicked:
 protected void RefreshMailBox()
 {
  Chilkat.Imap imap = new Chilkat.Imap();
  imap.UnlockComponent("");
  imap.Port = 993;
  imap.Ssl = true;
  imap.Connect("imap.gmail.com");
  imap.Login("[email protected]", "pass");
  imap.SelectMailbox("Inbox");

  Chilkat.MessageSet msgSet = imap.Search("ALL", true); 
  bundle = imap.FetchBundle(msgSet);
 }

 protected void YourEventHandler()
 {
  if (listView1.SelectedItems.Count > 0)
  {
   // bundle is now accessible in your event handler:
   richTextBox1.Text = bundle.GetEmail((int)listView1.SelectedItems[0].Tag).Body;
  }
 }

 ...
}
迷你仙 2024-10-04 14:04:05

检查项目属性并确保两个项目都针对相同的框架。通常,当一个指向 .Net Framework 4 而另一个指向 .Net Framework 4 客户端配置文件时,会发生这种情况。

谢谢,
塞巴斯蒂安

Check the project properties and make sure both project are targeting the same framework. usually this happens when one if pointing to .Net Framework 4 and another to .Net Framework 4 Client Profile

Thanks,
Sebastian

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文