开发者

Does not exist in current context c#

开发者 https://www.devze.com 2023-01-20 10:02 出处:网络
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. Theproble

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("user@email.com", "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 em开发者_运维问答ail 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?


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("user@email.com", "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;
  }
 }

 ...
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消