没有数据库的ComboBox ValueMember和DisplayMember [英] ComboBox ValueMember and DisplayMember without database

查看:111
本文介绍了没有数据库的ComboBox ValueMember和DisplayMember的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我自己有点难以找到解决方法的答案这个问题,同时进一步推进在我的代码中,但是在这个时候我无处可去,如果我不解决它,在这里你得到了我,求助。



我想一个简单的是/否答案可以让我高兴,如果没有更好的。但是为此,我可能应该解释我想要的每一点:



我有一个组合框,我希望它被填充并显示所有的名称所选目录中的文件或许多不同的文件,但在组合框中选择项目时,它会返回文件的整个路径。我觉得我应该更多地解释组合框和/或程序的功能,所以我不认为使用DataBase这是一个好主意,但它可能会有很多毫无价值的话,所以我很有道理会限制自己说我会< b>不< / b>使用数据库,因为这对于目的来说太慢了。



然后,最后,我的问题是:有没有更简单的方法来拥有一个显示成员和一个组合框中的值成员没有数据库,连接字符串等?



为了使它更快,我有这个代码的想法,所以如果答案是否定的,AndAlso答案比这更简单和/或更快/代码最优,我非常感谢提示(和代码片段)。



 私有 ColPaths()作为 字符串 
私有 ColSelPath 作为 字符串

私有 Sub FillCboBox()

< span class =code-keyword>对于 每个 Str 作为 字符串 Directory.GetFiles(InteractiveMap.GalleriesPath,_
* .lst,SearchOption.AllDirectories)

cboCollection.Items .Add(Path.GetFileNameWithoutExtension(Str))
ReDim 保留ColPaths(cboCollection.Items.Count - 1
ColPaths(cboCollection.Items.Count - 1 )= Str
Next
结束 Sub

私有 Sub cboCollection_SelectedIndexChanged(sender As 对象,e 作为 EventArgs) _
句柄 cboCollection.SelectedIndexChanged

ColSelPath = ColPaths(cboCollection.SelectedIndex)
结束 Sub





我应该补充一点,这只是我想填充和使用的三种不同组合框中的一种,除非一个文本文件的内容(用户也可以手动编辑,如果需要/需要)和另一个是文件/路径和相同类型的可编辑文本文件的内容的组合,这是数据库为什么不适合我的众多原因之一。



PS。我是这个网站的新手,所以我不知道这里的事情是如何运作的,我假设与其他论坛相似,但我看到了一些不同的,非常有趣的东西(比如工作区)。无论如何,也正因为如此,我想问你关于下面显示的预览,文本编辑器,除了带有......的橙色线条矩形外,我什么也看不到。



我的浏览器(chrome)不适合在这里工作吗?是网站?或者它会是什么? (对不起,如果这个问题不在正确的地方)



PS2。这段代码在vb.net中,但我也欢迎C#的答案。

我在VS2013工作和这个项目的框架(因为它简单,我希望它非常兼容)它的2.0



最后,抱歉我可怕的英语,并提前感谢你。

解决方案

要使用DataMember和ValueMember,你不必拥有数据库,但是你做了必须有一些数据。这意味着您可以使用DataTable或其他DataBinding类。如果您将文件加载到数据表中,可能有在线示例,您可以这样做。



另一种方式,可能更容易更改代码一点。来自 http://stackoverflow.com/questions / 3063320 / combobox-adding-text-and-value-to-an-item-no-binding-source [ ^ ],如果这是一个Winforms应用程序,那么你可以这样做。 ASP.Net ComboBox可以用不同的方式完成。



字典< string,> test =  new  Dictionary< string,> (); 
test.Add( 1 dfdfdf);
test.Add( 2 dfdfdf);
test.Add( 3 dfdfdf);
comboBox1.DataSource = new BindingSource(test, null );
comboBox1.DisplayMember = Value;
comboBox1.ValueMember = Key;

// 获取组合框选择(在处理程序中)
< span class =code-keyword> string value =((KeyValuePair< string,> )comboBox1.SelectedItem)。价值;


Hello everyone.

I am having a little hard time finding anywhere the answer to workaround this question by myself, while advancing further in my code, but at this time im nowhere else to go further if I don't solve it, and here you got me, aksing for help.

I think a simple yes/no answer can make me happy if there is nothing better. But for that, probably I should explain every bit of what I want:

I have a combobox which I want it to be filled and display with the names of all the files in a selected directory or many different ones, but when selecting an item in the combobox, it returns the whole path to the file. I feel like i should explain more of the functionality of the combobox and/or the program so it is justified the why i don't think using a DataBase its a good idea, but it probably will be a lot of worthless words, so I will limit myself to say that I will <b>not</b> use a database because that would be too much slow for the purpose.

Then, finally, my question is: Is there any simplier way to have a display member AND a value member within the combobox without a database, connection string, etc?

To make it faster, i had this idea of code, so if the answer is no, AndAlso the answer is simplier And/Or faster/code-optimal than this, I would appreciate the hint (and code snippet) very much.

Private ColPaths() As String
Private ColSelPath As String

Private Sub FillCboBox()

    For Each Str As String In Directory.GetFiles(InteractiveMap.GalleriesPath, _
                                                "*.lst", SearchOption.AllDirectories)

        cboCollection.Items.Add(Path.GetFileNameWithoutExtension(Str))
        ReDim Preserve ColPaths(cboCollection.Items.Count - 1)
        ColPaths(cboCollection.Items.Count - 1) = Str
    Next
End Sub

Private Sub cboCollection_SelectedIndexChanged(sender As Object, e As EventArgs) _
                                                Handles cboCollection.SelectedIndexChanged

    ColSelPath = ColPaths(cboCollection.SelectedIndex)
End Sub



I should add that this is only one of 3 different comboboxes that i want to fill and use with a very similar methood, unless one is with the content from a text file(easy for user to edit manually also, if wanted/needed) and the other one is with the combination of files/paths and content of the same kind of editable text files, that is one of the many reasons of why a database would not work for me.

PS. I am new at this site, so I don't know yet how do things work around here, i assume similar to other forums, but i have seen some different and very interesting things (like the workspace). Whatever, its also because of that, that i want to ask you about the "Preview" shown below here, the text editor, i cant see anything but an orange-line rectangle with "..." in it.

Is my browser (chrome) not well for working here? is the site? or what could it be? (sorry if this question is not in the correct place)

PS2. This code is in vb.net but I welcome C# answers too.
Im working in VS2013 and the framework of this project (because of its simplicity and that i want it to be very compatible) its 2.0

Finally, sorry for my horrible english, and thank you in advance.

解决方案

To use DataMember and ValueMember you don't have to have a DataBase, but you do have to have a data something. Meaning you could use a DataTable or other DataBinding class. If you loaded your files into a datatable, there are likely examples online, you could do it that way.

The other way and probably easier is to change your code a little bit. From http://stackoverflow.com/questions/3063320/combobox-adding-text-and-value-to-an-item-no-binding-source[^], if this is a Winforms app then you can do it this way. ASP.Net ComboBox can be done a different way.

Dictionary<string,>test = new Dictionary<string,>();
        test.Add("1", "dfdfdf");
        test.Add("2", "dfdfdf");
        test.Add("3", "dfdfdf");
        comboBox1.DataSource = new BindingSource(test, null);
        comboBox1.DisplayMember = "Value";
        comboBox1.ValueMember = "Key";

// Get combobox selection (in handler)
string value = ((KeyValuePair<string,>)comboBox1.SelectedItem).Value;


这篇关于没有数据库的ComboBox ValueMember和DisplayMember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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