4
            Frm Form1 = new Frm();
                //here I always get the count =0
            if (Form1 .listBox2 .SelectedItems .Count  > 0)
            {
                string item;
                foreach (int i in Form1.listBox2.SelectedIndices)
                {
                    item = Form1.listBox2.Items[i].ToString();

当我在 Frm 中做同样的事情时,我得到了所选项目的真实数量,这是 Frm 中的代码

    public  void btnPostText_Click(object sender, EventArgs e)
    {
        listBox2.ClearSelected();
        if (listBox1.SelectedItems.Count > 0)
        {
            foreach (int index in listBox1.SelectedIndices)
                listBox2.SetSelected(index, true);
        }

从我的程序中,我尝试在登录过程后同时在 facebook 上发布多个群组,用户在 listBox2 的 litBox1 选择他/她想要发布到的群组名称,其中有群组 ID相同的顺序,所以当用户单击 btnPostText 时,我将选择从 listBox1 移动到 listBox2' ,,,现在在 Class2` 我想知道是否在 listBox2 中选择了任何项目,第一个代码在 Class2 中。PostImg public static bool PostImg( , , ,)

Class2 包含 post 过程,就像 Postimg 一样,如果已发布,则返回 true,否则返回 false

在这里我用 Frm 调用它

  if (Class2.PostImage(AppSettings.Default.AccessToken, textbox1.Text, textboxpic.Text) == true)
                MessageBox.Show("Post Done");

Class2 中的代码是

    public static bool PostImage(string AccessToken, string Status, string ImagePath) 
    {
        try
        {
            Frm Frm = new Frm();

            if (Frm .listBox2 .SelectedItems .Count  > 0)
            {
                string item;
                foreach (int i in Frm.listBox2.SelectedIndices)
                {
                    item = Frm.listBox2.Items[i].ToString();
                    groupid = item;


                    FacebookClient fbpost = new FacebookClient(AccessToken);
                    var imgstream = File.OpenRead(ImagePath);
                    dynamic res = fbpost.Post("/" + groupid + "/photos", new
                   {
                       message = Status,
                       File = new FacebookMediaStream
                       {

                           ContentType = "image/jpg",
                           FileName = Path.GetFileName(ImagePath)
                       }.SetValue(imgstream)

                   });

                    result = true;

                }
            }
            return result;
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
            return false;
        }
4

2 回答 2

3

First things first, some basics. Class2 and Frm are two distinct classes. Normally they cannot see each other unless you pass a reference between them.

Frm can see the PostImage method inside Class2 because it was marked as static. But it doesn't go the other way. So you need to pass a reference to Frm when you call PostImage. Easiest way of doing this is including it in the method signature:

public static bool PostImage(string AccessToken, string Status, string ImagePath, Frm MyForm) 

Now you call it:

if (Class2.PostImage(AppSettings.Default.AccessToken, textbox1.Text, textboxpic.Text, this) == true)

Notice how we passed this as a parameter in the function. This is the reference we are going to use inside PostImage:

if (MyForm.listBox2.SelectedItems .Count  > 0)

And so on and so forth. The variable MyForm is now a reference to the form that called Class2.PostImage.

于 2012-11-29T00:36:01.813 回答
1

Then you should pass reference to your form (or listBox) as a parameter of the PostImg method.

public static bool PostImg(Frm form, string AccessToken, string Status, string ImagePath )
{
    try
    {
        if (form.listBox2.SelectedItems.Count  > 0)
        {
            string item;
            foreach (int i in form.listBox2.SelectedIndices)
            {
                item = form.listBox2.Items[i].ToString();
                groupid = item;


                FacebookClient fbpost = new FacebookClient(AccessToken);
                var imgstream = File.OpenRead(ImagePath);
                dynamic res = fbpost.Post("/" + groupid + "/photos", new
               {
                   message = Status,
                   File = new FacebookMediaStream
                   {

                       ContentType = "image/jpg",
                       FileName = Path.GetFileName(ImagePath)
                   }.SetValue(imgstream)

               });

                result = true;

            }
        }
        return result;
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
        return false;
    }
}

And call it from the method in your form like this:

if (Class2.PostImage(this, AppSettings.Default.AccessToken, textbox1.Text, textboxpic.Text) == true)
                MessageBox.Show("Post Done");
于 2012-11-29T00:32:30.140 回答