将列表框项目分配给对象 [英] asssigning listbox items to a object

查看:55
本文介绍了将列表框项目分配给对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用folderbrowserdialog列出了列表框中的.doc文件,现在我需要将.doc文件合并到单个.doc文件中。



i需要将列表框项目(.doc文件)分配给对象的过程,以便我可以调用对象中的文件然后最后读取它以进行合并...



i had listed .doc files in a listbox using "folderbrowserdialog" , now i need to merge the .doc files to a single .doc file.

i need the procedure to assign the listbox items(.doc files) to a object , so that i can call the files in a object and then read it finally for merging ...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       flb.RootFolder = Environment.SpecialFolder.MyComputer
       flb.ShowNewFolderButton = False
       Dim result As DialogResult = flb.ShowDialog
       If (result = DialogResult.OK) Then
           ListBox1.Items.Clear()
           TextBox1.Text = flb.SelectedPath
           For Each filename In Directory.GetFiles(flb.SelectedPath, "*.doc").Union(Directory.GetFiles(flb.SelectedPath, "*.rtf").ToArray())
               ListBox1.Items.Add(Path.GetFileName(filename))
     Next
       End If
   End Sub

推荐答案

你不必添加类型为 string 的对象到列表框 Items - 它们可以是任何类型。除此之外,我建议使用一些语义描述类型而不是字符串,因为当您使用它的选择等时,您可能需要一些关于该项目的信息。



它只留下一个问题:项目中将显示哪些文本?解决方案很简单:在项目类型中,覆盖方法 ToString ,因为此函数返回的值是列表框中显示的值。



问题解决了。







请看到我过去的答案,我在相关问题上编写了几个过去的答案,源代码示例适用于 ComboBox ListBox

combobox.selectedvalue在winform中显示{} [ ^ ]。< br $>


-SA
You don''t have to add objects of the type string to the list box Items — they can be of any type. More than that, I recommend to use some semantically descriptive types instead of strings, as you will likely need some information on the item when you work with its selection, etc.

It leaves just for one problem: what text will be displayed in the items? The solution is simple: in the type of the item, override the method ToString as the value returned by this function is the value shown in the list box.

Problem solved.



Please see my past answer where I complied several past answers on related problems, with source code samples applicable to ComboBox and ListBox:
combobox.selectedvalue shows {} in winform[^].

—SA


这篇关于将列表框项目分配给对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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