Get a RETURN from a Invoke method

2019-05-24 10:25发布

问题:

I am trying to read value from a listbox item that is on another thread.

I tried to make a new method to run the invoke command, I can manage to send a command to the listbox like add via the invoke method but i cant seem to get a response, i cant seem to get the value of the item, i have tried a few ways, once i change it from a void to a string things start to get hairy...

  thread t1 = new thread(thethread)
    t1.start()

    public void thethread()
    {
    string text = readListBoxSelected(listBox1) + " lala" ;
    }



    public static string readListBoxSelected(ListBox listbox)
    {
        if (listbox.InvokeRequired)
        {
            return (string)listbox.Invoke(
              new Func<String>(() => readListBoxSelected(listbox))
            );
        }
        else
        {

            string varText = listbox.SelectedValue.ToString();
            return varText;
        }
        }

Above is a example of what i am trying to do.

Here is the error:

System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=** StackTrace: at **.Form1.readListBoxSelected(ListBox listbox) in e:\documents and settings\scott\my documents\visual studio 2010\Projects*****\Form1.cs:line 133 at ***.Form1.<>c_DisplayClass5.b_3() in e:\documents and settings\scott\my documents\visual studio 2010\Projects******\Form1.cs:line 127 InnerException:

I imagine what is wrong is exactly what it says "Object reference not set to an instance of an object"....... All my variables seem to be declared as fair as i am aware, how can i correct this??

I get the feeling i am going about the entire thing wrongly.... 0_o Thanks in Advance, Scott

回答1:

Try This

public static string readListBoxSelected(ListBox listbox)
    {
        if (listbox.InvokeRequired)
        {
            return (string)listbox.Invoke(
              new Func<String>(() => readListBoxSelected(listbox))
            );
        }
        else
        {
if(istbox.SelectedValue != null)

            return  listbox.SelectedValue.ToString();
else
return String.Empty
        }
        }


回答2:

Code looks fine, the problem seems on the SelectedValue, is it null. ???



回答3:

Thanks guys,

You where correct, Problem was it was returning a null value.. I was so sure that i was selecting the item correctly i never thought it could be the problem.

Turns out the problem was two things:

1) The way i was selecting item, i was using listbox.Selecteditem = 1 , now if i use listbox.setSelected(1,true) all is good :)

and

2) The way i was getting the items text was wrong, listbox.SelectedValue is nothing, it dosnt do what we all imagine it to do... the call i need was listbox.Text .........

public static string readListBoxSelected(ListBox listbox)
{
    if (listbox.InvokeRequired)
    {
        return (string)listbox.Invoke(
          new Func<String>(() => readListBoxSelected(listbox))
        );
    }
    else if(listbox.Text != null)
    {
        return  listbox.Text.ToString();
    } 
    else
    return String.Empty;
    }


public void selectListBoxItem(ListBox listbox, int num)
{
    Invoke(new MethodInvoker(delegate { listbox.SetSelected(num,true); }));
}

I must say this is the most anoying thing i have ever done... Everything requires i write a delegate / invoke method for it... Everything... woudlnt something so common be supported by .net on the fly....

Seems a waist of time to write individual delegates for EVERYTHING...

Thanks guys all is working now, yesterday i couldn't foresee me getting to this point, Overall problem was Wrong Calls, the invoke was all fine :)

Scott

EDIT:

ok it was returning NULL simply because listbox.SelectedValue isnt really the call im after to read the selectedvalue (you would think it was), if i change it to listbox1.text all works fine.... rather silly this .net object oriented stuff if i do say so....

I must say what a joke... thats kindly destroyed my faith in object oriented programming.. I understand this is not a discussion fourm but honestly the call SelectedValue.toString() should do what we all think it will do.... nope we need to use .Text to get what we require 0_o.........