Get a RETURN from a Invoke method

做~自己de王妃 提交于 2019-12-06 09:55:42

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
        }
        }

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

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.........

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!