从 Invoke 方法获取 RETURN

发布于 2024-11-15 13:07:37 字数 1498 浏览 3 评论 0原文

我正在尝试从另一个线程上的列表框项目中读取值。

我尝试创建一种新方法来运行调用命令,我可以设法将命令发送到列表框,例如通过调用方法添加,但我似乎无法得到响应,我似乎无法获取该项目的值,我尝试了几种方法,一旦我将它从空变为字符串,事情就开始变得毛茸茸的......

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

上面是我正在尝试做的一个例子。

这是错误:

System.NullReferenceException 是 未由用户代码处理
消息=对象引用未设置为 对象的实例。
Source=** StackTrace: 在 e:\documents 中的 **.Form1.readListBoxSelected(ListBox listbox) 处 设置\斯科特\我的文档\视觉 工作室 2010\项目*****\Form1.cs :线 133 在***.Form1.<>c_DisplayClass5.b_3() 在 e:\documents and settings\scott\my 文件\视觉工作室 2010\项目******\Form1.cs:line 127 内部异常:

我想问题正是它所说的“对象引用未设置为对象的实例”......我的所有变量似乎都被声明为公平的,我知道,我该如何纠正这??

我感觉我对整件事的处理都是错误的...... 0_o 提前致谢, 斯科特

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

八巷 2024-11-22 13:07:38

代码看起来不错,问题似乎出在SelectedValue上,是null吗? ???

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

终弃我 2024-11-22 13:07:38

谢谢大家,

你是对的,问题是它返回了一个空值。
我非常确定我正确选择了该项目,但我从未想过这可能是问题所在。

事实证明问题出在两件事上:

1)
我选择项目的方式,我使用 listbox.Selecteditem = 1 ,现在如果我使用 listbox.setSelected(1,true) 一切都很好:)

2)
我获取项目文本的方式是错误的,listbox.SelectedValue什么都不是,它没有做我们想象中的事情......我需要的调用是listbox.Text......

public static string readListBoxSelected(ListBox listbox)
{
    if (列表框.InvokeRequired)
    {
        返回(字符串)listbox.Invoke(
          new Func(() => readListBoxSelected(listbox))
        );
    }
    else if(listbox.Text != null)
    {
        返回 listbox.Text.ToString();
    } 
    别的
    返回字符串.Empty;
    }


公共无效selectListBoxItem(ListBox列表框,int num)
{
    Invoke(new MethodInvoker(delegate { listbox.SetSelected(num,true); }));
}

我必须说这是我做过的最烦人的事情...一切都需要我为它编写一个委托/调用方法...一切...不会被.net即时支持如此常见的东西...

似乎要花很多时间来写每件事的个人代表...

谢谢大家现在都在工作,昨天我无法预见我会走到这一步,
总体问题是错误的调用,调用一切正常:)

Scott

编辑:

好吧,它只是返回 NULL,因为 listbox.SelectedValue 并不是真正的调用之后读取 selectedvalue (你会认为是的),如果我将其更改为 listbox1.text 一切正常......如果我这么说的话,这个 .net 面向对象的东西相当愚蠢......

我必须说这是一个笑话......那是善意地摧毁了我对物体的信心面向编程..
我知道这不是一个讨论论坛,但老实说,调用 SelectedValue.toString() 应该做我们都认为它会做的事情......不,我们需要使用 .Text 来获取我们需要的 0_o...... ..

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

蘸点软妹酱 2024-11-22 13:07:37

试试这个

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

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
        }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文