Listbox count items from another class always returns 0

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(); 

and when I do the same in Frm, I get the actual number of elements selected here is the code in 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); } 

from my program, which I try to send to more than one group on facebook simultaneously after the registration process, the user selects the names of the groups that he / she wants to publish to litBox1 in listBox2, there are the identifiers of the groups (s) in the same order, therefore, when the user clicks on btnPostText, I move the selection from listBox1 to listBox2 ', Now in Class2` I want to know if any items were selected in listBox2, the first code is in Class2. PostImg public static bool PostImg (,,)

Class2 contains mail routines like Postimg, which returns true if sent or false if not

here i call it in frm

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

the code in Class2 is

  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
source share
2 answers

First of all, some basics. Class2 and Frm are two different classes. Usually they cannot see each other unless you pass the link between them.

Frm can see the PostImage method inside PostImage because it has been marked as static . But this is not so. Therefore, when calling PostImage you need to pass a link to Frm . The easiest way to do this is to include it in the method signature:

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

Now you name it:

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

Notice how we passed this as a parameter to the function. This is the link we will use inside PostImage :

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

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

+3
source

Then you must pass a link to your form (or listBox) as a parameter to 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 name it from the method in your form as follows:

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

All Articles