vb.net 如何从文本文件中加载带有项目的组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12299881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:16:21  来源:igfitidea点击:

How to load a ComboBox with Items from a text file

vb.netwinformscomboboxtext-files

提问by ABANDOND ACOUNT

What is the simplest way of importing items from a text file into a ComboBoxcontrol? The text file contains multiple lines. I need to add each line from the text file as an item in the combo box.

将项目从文本文件导入ComboBox控件的最简单方法是什么?文本文件包含多行。我需要将文本文件中的每一行添加为组合框中的一个项目。

回答by Steven Doggart

If the format of the text file is one combo box item per line, and you want to load them into a WinForm ComboBoxcontrol, then this would be the simplest way to do it:

如果文本文件的格式是每行一个组合框项,并且您想将它们加载到 WinFormComboBox控件中,那么这将是最简单的方法:

ComboBox1.Items.AddRange(File.ReadAllLines(filePath))

You'll need to add Imports System.IOto the top of the code file. Either that or specify the full name of the Fileclass:

您需要添加Imports System.IO到代码文件的顶部。或者指定类的全名File

ComboBox1.Items.AddRange(System.IO.File.ReadAllLines(filePath))

回答by Edgar Cabrales

There are a couple ways of doing it. These are two ways you can do it with error handling:

有几种方法可以做到。您可以通过以下两种方式进行错误处理:

1

1

Try
  ComboBox1.Items.Clear()
  Dim objReader As New System.IO.StreamReader(File Path)

  Do While Not objReader.EndOfStream
    ComboBox1.Items.Add(objReader.ReadLine)
     Loop
  objReader.Close() 
Catch ex As Exception
    MsgBox(Messege)
End Try

2

2

Try
  ComboBox1.Items.Clear()
  ComboBox1.Items.AddRange(File.ReadAllLines(filePath))
Catch ex As Exception
    MsgBox(Messege)
End Try

Either one of these will work for you. It is always good practice to use error handling so your program doesn't crash and can do something else in case it fails to read the file. I did add a ComboBox1.Items.Clear() in the beginning because what will end up happening is if you have your code in a subroutine where it is refreshing and constantly reading the text file live, it will keep on adding to the list the the things that are already in the combobox and will have repeated values. For Example, lets say you have in the textfile the words EARTH MARS In the combobox, you will have the word EARTH MARS. But if you have it to refresh constantly lets just say 4 times, the combobox will now display EARTH MARS EARTH MARS EARTH MARS EARTH MARS. Now if you use my code, it will read EARTH MARS and put in the combobox EARTH MARS and if it refreshes 4 or how many ever times, it will still display EARTH MARS because it is clearing the items in the combobox before going back to the textfile and reading what is on it. Now if you add the word JUPITER to the text file, your combobox will now append the word JUPITER to the combobox items list, and voila, nor repeated values.

其中任何一个都适合你。使用错误处理总是好的做法,这样您的程序就不会崩溃,并且可以在无法读取文件的情况下执行其他操作。我确实在开始时添加了一个 ComboBox1.Items.Clear() ,因为最终会发生的是,如果您将代码放在一个子程序中,它会刷新并不断地实时读取文本文件,它将继续添加到列表中已经在组合框中并且将具有重复值的内容。例如,假设您在文本文件中有单词 EARTH MARS 在组合框中,您将有单词 EARTH MARS。但是,如果您让它不断刷新,只需说 4 次,组合框现在将显示 EARTH MARS EARTH MARS EARTH MARS EARTH MARS。现在如果你使用我的代码,它将读取 EARTH MARS 并将其放入组合框 EARTH MARS,如果它刷新 4 次或刷新了多少次,它仍将显示 EARTH MARS,因为它在返回文本文件并阅读其上的内容之前正在清除组合框中的项目. 现在,如果您将单词 JUPITER 添加到文本文件中,您的组合框现在会将单词 JUPITER 附加到组合框项目列表中,瞧,也没有重复的值。

回答by Derek John Scott

ComboBox1.Items.AddRange(System.IO.File.ReadAllLines(local path))

ComboBox1.Items.AddRange(System.IO.File.ReadAllLines(local path))

Ex:

前任:

Public Class Form1
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

  ComboBox1.Items.AddRange(System.IO.File.ReadAllLines("C:\zone\target.txt"))

  End Sub

notice, under form_load trigger

注意,在 form_load 触发器下

回答by Fahad Ahmad

Thanks Derek. It worked for me.

谢谢德里克。它对我有用。

ComboBox1.Items.AddRange(System.IO.File.ReadAllLines(local path))