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 real number of selected items here’s 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 I am trying to post to more than one group at facebook at the same time after the log in process the user selects the groups names that he/she wants to post to at litBox1 in listBox2 there’s the groups id(s) in the same order , so when user clicks on the btnPostText I move the selection from listBox1 to listBox2′ ,,, Now in Class2` I want to know if any Items are selected in listBox2 ,, the first code is in Class2. PostImg public static bool PostImg( , , ,)
Class2 contains the post procedures just like the Postimg it returns true if posted or false if not
here I am calling 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;
}
First things first, some basics.
Class2andFrmare two distinct classes. Normally they cannot see each other unless you pass a reference between them.Frmcan see thePostImagemethod inside Class2 because it was marked asstatic. But it doesn’t go the other way. So you need to pass a reference toFrmwhen you callPostImage. Easiest way of doing this is including it in the method signature:Now you call it:
Notice how we passed
thisas a parameter in the function. This is the reference we are going to use insidePostImage:And so on and so forth. The variable
MyFormis now a reference to the form that calledClass2.PostImage.