为什么我不能使用ListBox.FindStringExact? [英] Why Can't I use ListBox.FindStringExact?

查看:165
本文介绍了为什么我不能使用ListBox.FindStringExact?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VB.NET v4编写和应用程序。我有2个列表框,使用相同数据源的变体。我希望能够从列表框1中选择并让列表框2设置它的选择以匹配列表框1中的那个。



我找到了怎么做这在MSDN上,但出于某种原因,当我尝试使用

I am using VB.NET v4 to write and app. I have 2 list boxes that use variations of the same data source. I want to be able to select from list box 1 and have list box 2 set it's selection to match the one from list box 1.

I have found how to do this on MSDN, but for some reason, when I try to use

ListBox2.FindStringExact(string)



VS2012说FindStringExact不是System.Windows.Controls.Listbox的成员。



MSDN说它是System.Windows.Forms的成员,但VS2012不允许我使用


VS2012 says that FindStringExact is not a member of System.Windows.Controls.Listbox.

MSDN says it's a member of System.Windows.Forms, but VS2012 won't let me use

Imports System.Windows.Forms



,因为它没有显示为可用。


, as it doesn't show as being available.

推荐答案

好的,我得到它。

FindStringExact是System.Windows.Forms.ListBox类的一个方法。

但你使用的是WPF。

这两个是有自己的控件集的库,有一些共同点,也有一些差别。

WPF ListBo x显然没有这种方法。



干得好:)



从你的回答我的评论:我不建议混合使用Windows Forms和WPF。 此处 [ ^ ]是MSDN WPF ListBox控件的页面。到目前为止,我不是WPF专家,所以我不会冒险给出正式的解决方案,但是你可以找到用提供的方法实现目标的方法。
Ok, I get it.
FindStringExact is a method of System.Windows.Forms.ListBox class.
But you are using WPF.
These are two libraries that have their own sets of controls, which have some things in common, and some differences also.
WPF ListBox does not have this method, apparently.

Good work :)

From your answer to my comment: I would not recommend to mix the use of Windows Forms and WPF. Here[^] is the MSDN page of the WPF ListBox control. I'm not a WPF specialist, by far, so I would not risk giving a formal solution, but there you may find the way to achieve your goal with provided methods.


请看我的评论这个问题。没有什么需要解释,但我会尝试。



你把一切都搞砸了。不,不仅没有这样的方法,而且你的声明MSDN说它是System.Windows.Forms的成员,但VS2012不会让我使用也与真相无关。这又是你的幻想。事实上,有一些以此名称已知的.NET FCL方法,例如,这两个: https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.findstringexact%28v=vs.110%29.aspx [ ^ ]。



但这些方法与您的WPF代码无关。但是如果你想使用 System.Windows.Forms.ListBox ,请使用它,但不要询问WPF ListBox

实际上, System.Windows.Forms 的这两种方法完全冗余。您可以循环遍历列表框项目并找到您想要的任何内容。



但更重要的是要了解类似的功能不适用于WPF。在WPF列表框中,项目不是字符串。 (惊喜?然后阅读MSDN文档。)

在WPF中,它们是任意对象。但是在WPF中,实际的UI元素是基于 System.Object 类型的元素动态创建的,并出现在WPF 可视树中。这是一个非常重要的WPF概念,您无法理解: https:// msdn .microsoft.com / library / ms753391%28v = vs.100%29.aspx [ ^ ]。



如果需要,可以遍历列表中的所有元素框并在每个上调用函数 System.Object.ToString()来查找字符串,但这是一种蹩脚的方法。强大的方法是将适当的语义敏感数据类型的数据作为列表框项而不是字符串。您可以通过覆盖项类型的 System.Object.ToString()来控制字符串表示。我在过去的答案中解释了它适用于WPF和表格:

更新不同窗口上的列表视图 [ ^ ],

wpf无模式对话框和窗口,如何沟通 [ ^ ],

如何以两种形式复制列表框之间的所有项目 [ ^ ],

combobox.selectedvalue在winform中显示{} [ ^ ]。



因此,您可以实现搜索:遍历列表框项目在一个循环中,检查每一个以找到你想要的任何匹配。



-SA
Please see my comment to the question. There is nothing which needs explanations, but I'll try.

You mess up everything in your mind. No, not only there is no such method, but your statement "MSDN says it's a member of System.Windows.Forms, but VS2012 won't let me use" also has nothing to do with truth. It's, again, your fantasy. In fact, there are some .NET FCL methods known under this name, for example, these two: https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.findstringexact%28v=vs.110%29.aspx[^].

But these methods have nothing to do with your WPF code. But if you want to use System.Windows.Forms.ListBox, use it, but then don't ask about WPF ListBox.
In fact, those two methods of System.Windows.Forms are totally redundant. You can traverse your list box items in a loop and find whatever your want.

But it's more important to understand that similar functions could not be applicable to WPF. In WPF list box, items are not strings. (Surprise? Then just read MSDN documentation.)
In WPF, they are arbitrary objects. But in WPF, actual UI elements are created on the fly based on those elements of the type System.Object and appear in the WPF visual tree. This is a very important WPF concept which you cannot afford not to understand: https://msdn.microsoft.com/library/ms753391%28v=vs.100%29.aspx[^].

If you want, you can traverse all the elements of the list box and call the function System.Object.ToString() on each, to find your string, but this is a lame approach. The robust approach is to put data of appropriate semantically sensible data types as list box items, not strings. You can control the string representation by overriding System.Object.ToString() of your item type. I explained it in my past answers which are applicable to both WPF and Forms:
Updating listviews on different windows[^],
wpf modeless dialog and windows, how to communicate[^],
How to copy all the items between listboxes in two forms[^],
combobox.selectedvalue shows {} in winform[^].

So, you have the way to implement the search: traverse list box items in a loop, and inspect each to find any kind of match you want.

—SA


这篇关于为什么我不能使用ListBox.FindStringExact?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆