Get the App
SLTechnology News&Howtos  ›  Development  › 

How to save the contents of a list box as a text file with VB

Shulou Source: shulou.com Published: 2022-06-02 18:12:06 10月24日 Update

This article mainly explains the "how to use VB to save list box content as a text file", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use VB to save list box content as a text file"!

Sometimes we need to save the contents of the list box, such as when making a list of playback files for the player. The solution can be like this: use the text box to save all the items in the list box. Here is an example to illustrate the implementation method (to try this example, add several controls by source, where the Multiline property of the TextBox control is set to True and the ScrollBars property is set to 2):

Private Sub Form_Load ()

List1.Clear 'clear the list box

Text1 = "" 'clear the text box

End Sub

'get the screen font name and add it to the list box (to make the list box have content)

Private Sub cmdFindFonts_Click ()

Dim fnt As Integer

For fnt = 0 To Screen.FontCount-1

List1.AddItem Screen.Fonts (fnt)

Next fnt

End Sub

'add all Item of the list box to the text box

Private Sub List1_Click ()

For Index = 0 To List1.ListCount-1

Text1.Text = Text1.Text & List1.List (Index) & vbCrLf

The function of vbrLf is to change lines.

Next Index

End Sub

'Save

Private Sub cmdSave_Click

CommonDialog1.Filter = "text document (* .txt) | * .txt | all files (*. *) | *. *"

CommonDialog1.FilterIndex = 1

CommonDialog1.ShowSave

F = CommonDialog1.FileName

Open F For Output As # 1

Print # 1, Text1.Text

Close # 1

End Sub

In this way, all items in the list box can be saved.

If you save only part of the list box, you can add the following code to the Click event of List1:

Dim i As Integer

I = I + List1.ListIndex

Text1.Text = Text1.Text + List1.List (Index + I) + vbCrLf

This example is debugged in PWin98 and VB6.0 environment.

Thank you for your reading, the above is the content of "how to use VB to save the contents of the list box as a text file", after the study of this article, I believe you have a deeper understanding of how to use VB to save the contents of the list box as a text file, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Content text file learning function properties ideas controls projects events code available in name font instance that is screen situation player article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MariaDB Docker Redmi Linux