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. 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
.
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");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.