将信息从组合框写入文本文件 [英] Writing information from Combo-box to text file

查看:181
本文介绍了将信息从组合框写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将组合框中的信息改为文本文件,以便保存。如果组合框中的信息是John,Marry,Jack,我希望它出现在文本文件中,如下所示:

  John 
Mary
Jack

我目前使用的代码给出JohnMaryJack的结果文本文件

 对于每个项目作为对象在cmbworld.Items 
Dim test As String
test = item
sb.AppendFormat({0},item)
Dim FILE_NAME As String =D:\Documents\test.txt
如果System.IO.File.Exists (FILE_NAME)= True然后
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Write(test)
objWriter.WriteLine()
objWriter.Close
MsgBox(文本写入文件)
否则
MsgBox(文件不存在)
结束如果

下一页

解决方案

/ div>

首先,我将从For Each循环写入文件。这样你只能写一次文件。
其次你可以稍微调整@BiggsTRC的答案到

  sb.AppendFormat({0} ,item,Environment.NewLine)

此外,使用变量 test 写入textfile,而不是你使用的stringbuilder。



因此,你的代码看起来像这样:

  Dim sb as new StringBuilder()

对于每个项目作为对象在cmbworld.Items
'Dim test As String
'test = item
sb.AppendFormat({0} {1},item,Environment.NewLine)
下一页

Dim FILE_NAME As String =D:\Documents\ test.txt
如果System.IO.File.Exists(FILE_NAME)= True然后
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Write(sb.ToString ))'在这里使用stringbuilder
objWriter.WriteLine()
objWriter.Close()
MsgBox(文本写入文件)
否则
MsgBox文件不存在)
结束如果

它,因为很长时间我写了VB和我没有VS目前在那一刻,但我想你得到的图片; - )


I am trying to right the information from a combobox into a text file so it can be saved. If the information in the combobox is John, Marry, Jack I would like it to appear in the text file like this:

John
Mary
Jack

The code I currently use give a result of JohnMaryJack in the text file

For Each item As Object In cmbworld.Items
        Dim test As String
        test = item
        sb.AppendFormat("{0}", item)
        Dim FILE_NAME As String = "D:\Documents\test.txt"
        If System.IO.File.Exists(FILE_NAME) = True Then
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
            objWriter.Write(test)
            objWriter.WriteLine()
            objWriter.Close()
            MsgBox("Text written to file")
        Else
            MsgBox("File Does Not Exist")
        End If

    Next

How would I fix this?

解决方案

First I would take the writing to the file out of the For Each-loop. This way you only write to the file once. Second you can slightly adapt the answer of @BiggsTRC to

sb.AppendFormat("{0} {1}", item, Environment.NewLine)

Furthermore you use the variable test to write to the textfile, instead of the stringbuilder you used. This way the formating never gets into the file.

So than your piece of code looks something like this:

Dim sb as new StringBuilder()

For Each item As Object In cmbworld.Items
        'Dim test As String
        'test = item
        sb.AppendFormat("{0} {1}", item, Environment.NewLine)
Next

Dim FILE_NAME As String = "D:\Documents\test.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
    Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
    objWriter.Write(sb.ToString()) 'Use the stringbuilder here
    objWriter.WriteLine()
    objWriter.Close()
    MsgBox("Text written to file")
Else
    MsgBox("File Does Not Exist")
End If

There could be some syntax-minor error in it, cause it's a long time I've written VB and I haven't a VS present at the moment, but I think you get the picture ;-)

这篇关于将信息从组合框写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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