C# 组合框不显示项目

发布于 2024-11-27 13:53:15 字数 857 浏览 1 评论 0原文

我试图在 C# 中填充组合框,但由于某种原因,这些项目没有出现。

public List<string> items
{
    set
    {
        combobox.Items.Clear();
        foreach(string s in value)
        {
            combobox.Items.Add(s);
        }
        combobox.Update();
    }
}

这看起来是非常简单的代码。我根本看不出哪里出了问题。 它是这样调用的:

private void StoreNames(List<string> names)
{
    if (selectionForm.InvokeRequired)
        selectionForm.Invoke((MethodInvoker)delegate { selectionForm.items = names; });
    else
        selectionForm.items = names;
}

有趣的是,当 InvokeRequired 返回 true 时它似乎可以工作,但当它返回 false 时则不起作用。

编辑: 我发现 selectionForm.IsHandleCreated 当前为 false。这会导致 InvokeRequired 返回 false,但这也是定期调用 setter 不起作用的原因。我不知道为什么 IsHandleCreated 设置为 false。该表格已被 Show()n 。

I am trying to populate a combobox in C#, but for some reason, the items do not appear.

public List<string> items
{
    set
    {
        combobox.Items.Clear();
        foreach(string s in value)
        {
            combobox.Items.Add(s);
        }
        combobox.Update();
    }
}

This seems like incredibly straightforward code. I simply cannot see what is wrong.
It is being called like this:

private void StoreNames(List<string> names)
{
    if (selectionForm.InvokeRequired)
        selectionForm.Invoke((MethodInvoker)delegate { selectionForm.items = names; });
    else
        selectionForm.items = names;
}

Interestingly, it seems to work when InvokeRequired returns true, but does not work when it returns false.

EDIT:
I discovered that selectionForm.IsHandleCreated is currently false. This is causing InvokeRequired to return false, but is also why calling the setter regularly isn't working. I don't have any idea why IsHandleCreated is set to false. The Form has been Show()n.

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

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

发布评论

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

评论(5

木有鱼丸 2024-12-04 13:53:15

不知道为什么你的代码不起作用 - 我尝试过,它工作得很好。

然而,下面是一些更简单的代码,它也可以工作——你可能会发现这样做反而会让你的问题消失。这确实假设没有其他原因导致您需要遍历该属性 - 这是一种非常不寻常的做事方式。

public void StoreNames(List<string> input)
{
    if (comboBox1.InvokeRequired)
        comboBox1.Invoke((MethodInvoker)delegate {
            StoreNames(input);
        });
    else
    {
        comboBox1.Items.Clear();
        comboBox1.Items.AddRange(input.ToArray());
    }
}

在这里,我们只是将列表直接传递给组合框上的 items.AddRange() 方法。

我怀疑这对你不起作用 - 正在发生其他事情,但我已经从后台工作者(其中 InvokeRequired 为 true)和主 UI 线程尝试过它。

Not sure why your code isn't working - I tried it and it works just fine.

However, below is some more straightforward code which also works - you may find that doing it this way instead makes your problem go away. This does presume that there is not other reason why you need to go through that property - that is quite an unusual way of doing things.

public void StoreNames(List<string> input)
{
    if (comboBox1.InvokeRequired)
        comboBox1.Invoke((MethodInvoker)delegate {
            StoreNames(input);
        });
    else
    {
        comboBox1.Items.Clear();
        comboBox1.Items.AddRange(input.ToArray());
    }
}

Here we just pass the list straight to the items.AddRange() method on the comboBox.

I suspect this won't work for you - something else is going on, but I have tried it both from a backgroundworker (where InvokeRequired is true) and from the main UI thread.

生寂 2024-12-04 13:53:15

您同时提到组合框和组合框1 是否是一个拼写错误?也许那是你的错误。

Is it a typo that you refer to both combobox and combobox1? Perhaps that is your error.

梦回旧景 2024-12-04 13:53:15

您的意思是“不出现”,因为您可以在列表显示时看到它们,或者您甚至无法滚动到它们?

组合框有很多属性可以影响您所看到的内容。尝试为组合框.MaxDropDownItems 设置更大的值。

Do you mean "not appear" as in you can see them when the list displays, or as in you can't even scroll to them?

Combobox has a lot of properties that can affect what you see. Try a bigger value for combobox.MaxDropDownItems.

半﹌身腐败 2024-12-04 13:53:15

我在这个线程中的其他答案实际上只是展示了执行此操作的方法(如果您可以获取为您提供列表的代码)。既然听起来你不能,我就提供这个答案。

听起来关键问题是当调用该属性时,组合框尚未初始化。最好的答案是确保调用代码中不会发生这种情况。

如果您不能这样做,那么您应该等待该属性设置完毕,然后再使用它填充组合框。

我会通过在属性设置器中设置一个私人列表来做到这一点。在表单加载事件中,我将放置一些代码(可能在后台工作人员中),这些代码将等待私有列表不为空,然后将其分配给组合框。

看看 的这篇文章Jon Skeet 这里 讨论了如何正确等待另一个线程设置变量。执行此操作时您需要小心 - 线程问题可能会很严重。

My other answer in this thread is really just showing the way to doing this if you can get at the code providing you with the list. Since it sounds like you cannot, I'm providing this answer.

It sounds like the key problem is that when the property is called the combobox has not yet been initialized. The best answer for that is that make sure that this does not happen within the calling code.

If you can't do that then you should wait for the property to be set before you use it to populate the combobox.

I would do this be having a private list which gets set in the property setter. Within the form load event i would then put some code (possibly within a background worker) that will wait until the private list is not null and then assign it to the combobox.

Have a look at this post by Jon Skeet here where he discusses how to correctly wait for a variable to be set by another thread. You will want to be careful when doing this - threading issues can be nasty.

无畏 2024-12-04 13:53:15

输入图片这里的描述

我想我发现了问题。我照常选择了绘图模式。

enter image description here

I think I found the problem. I chose Drawmode as normal.

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