vb.net 保存文本框值供以后使用
vb.net save textbox values for later use
我在表单中使用 textbox
来搜索 pdf 文件中的单词。我想将这些搜索词保存在某个地方以备后用。因此,当用户在 textbox
中键入一个字母时,将会出现一个包含之前搜索过的词的下拉列表。 windows 之类的东西在资源管理器中。
有没有人有例子或者有人熟悉这个?
我找到了一些代码 Saving text box values to a file to load again 并根据需要进行了更改。当我单击“提交”按钮时,它会将我的 ComboBox1 的内容保存到一个文本文件中。当我加载表单时,文本文件被读取并加载到 ComboBox1 中,就像以前使用的搜索一样。
'now save the search word to our textfile
PresetName = TextBoxFreeText.Text
If PresetName <> "" Then
TextBoxFreeText.Items.Add(PresetName)
Call SaveData(PresetName)
End If
加载保存值的代码:
Private Sub ReadData()
If My.Computer.FileSystem.FileExists(FilePath) = True Then
myReader = New StreamReader(FilePath)
Dim myText = myReader.ReadLine
While myText IsNot Nothing
listPreset.Add(myText)
myText = myReader.ReadLine
End While
myReader.Close()
'Add Preset Names to ComboBox
If listPreset.Count > 0 Then
Dim PresetName As String
Dim index As Integer
For i = 0 To listPreset.Count - 1
index = listPreset.Item(i).IndexOf(",")
PresetName = Mid(listPreset.Item(i), 1, index)
TextBoxFreeText.Items.Add(PresetName)
Next
End If
End If
End Sub
保存ComboBox1内容的代码
Private Sub SaveData(ByVal PresetName As String)
Dim FileString As String = PresetName & ","
'Build File String
FileString &= TextBoxFreeText.Text & ","
listPreset.Add(FileString)
myWriter = New StreamWriter(FilePath)
For i = 0 To listPreset.Count - 1
myWriter.WriteLine(listPreset.Item(i))
Next
myWriter.Close()
End Sub
还有两点我想改。我再问一个新问题。
- 防止在文本文件中输入重复的搜索字符串。
- 为 ComboBox1 中加载的搜索字符串设置最大值(可能为 10 个单词)
您可以在 StringCollection
as the data source of the TextBox.AutoCompleteCustomSource
属性.
类型的应用程序设置条目中存储和检索搜索词列表
添加新条目
- Select
YourAppName
项目 菜单中的属性。
- Select 设置 选项卡。
- 在 Name 列中添加新条目,例如:SearchWords。
- 来自类型列,selectSystem.Collections.Specialized.StringCollection.
- 关闭对话框并保存。
检索搜索词
在表单的构造函数或 Load
事件中,假设您有一个名为 txtSearch
:
的搜索 TextBox
' +
Imports System.Linq
Imports System.Collections.Specialized
Private Sub YourForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.SearchWords Is Nothing Then
My.Settings.SearchWords = New StringCollection
End If
Dim acc As New AutoCompleteStringCollection
acc.AddRange(My.Settings.SearchWords.Cast(Of String).ToArray())
txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest
txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource
txtSearch.AutoCompleteCustomSource = acc
End Sub
更新collection
每次执行搜索时都需要添加新词:
' When you click a search button...
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
AddSearchWord()
End Sub
' If you call the search routine when you press the Enter key...
Private Sub txtSearch_KeyDown(sender As Object, e As KeyEventArgs) Handles txtSearch.KeyDown
If e.KeyCode = Keys.Enter Then
AddSearchWord()
End If
End Sub
' Update the collection...
Private Sub AddSearchWord()
If txtSearch.Text.Trim.Length = 0 Then Return
If Not txtSearch.AutoCompleteCustomSource.Contains(txtSearch.Text) Then
If txtSearch.AutoCompleteCustomSource.Count > 10 Then
txtSearch.AutoCompleteCustomSource.RemoveAt(
txtSearch.AutoCompleteCustomSource.Count - 1)
End If
txtSearch.AutoCompleteCustomSource.Insert(0, txtSearch.Text)
End If
End Sub
保存collection
关闭表单时更新 SearchWord
字符串 collection:
Private Sub YourForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
My.Settings.SearchWords = New StringCollection
My.Settings.SearchWords.AddRange(txtSearch.AutoCompleteCustomSource.Cast(Of String).ToArray)
My.Settings.Save()
End Sub
演示
我在表单中使用 textbox
来搜索 pdf 文件中的单词。我想将这些搜索词保存在某个地方以备后用。因此,当用户在 textbox
中键入一个字母时,将会出现一个包含之前搜索过的词的下拉列表。 windows 之类的东西在资源管理器中。
有没有人有例子或者有人熟悉这个?
我找到了一些代码 Saving text box values to a file to load again 并根据需要进行了更改。当我单击“提交”按钮时,它会将我的 ComboBox1 的内容保存到一个文本文件中。当我加载表单时,文本文件被读取并加载到 ComboBox1 中,就像以前使用的搜索一样。
'now save the search word to our textfile
PresetName = TextBoxFreeText.Text
If PresetName <> "" Then
TextBoxFreeText.Items.Add(PresetName)
Call SaveData(PresetName)
End If
加载保存值的代码:
Private Sub ReadData()
If My.Computer.FileSystem.FileExists(FilePath) = True Then
myReader = New StreamReader(FilePath)
Dim myText = myReader.ReadLine
While myText IsNot Nothing
listPreset.Add(myText)
myText = myReader.ReadLine
End While
myReader.Close()
'Add Preset Names to ComboBox
If listPreset.Count > 0 Then
Dim PresetName As String
Dim index As Integer
For i = 0 To listPreset.Count - 1
index = listPreset.Item(i).IndexOf(",")
PresetName = Mid(listPreset.Item(i), 1, index)
TextBoxFreeText.Items.Add(PresetName)
Next
End If
End If
End Sub
保存ComboBox1内容的代码
Private Sub SaveData(ByVal PresetName As String)
Dim FileString As String = PresetName & ","
'Build File String
FileString &= TextBoxFreeText.Text & ","
listPreset.Add(FileString)
myWriter = New StreamWriter(FilePath)
For i = 0 To listPreset.Count - 1
myWriter.WriteLine(listPreset.Item(i))
Next
myWriter.Close()
End Sub
还有两点我想改。我再问一个新问题。
- 防止在文本文件中输入重复的搜索字符串。
- 为 ComboBox1 中加载的搜索字符串设置最大值(可能为 10 个单词)
您可以在 StringCollection
as the data source of the TextBox.AutoCompleteCustomSource
属性.
添加新条目
- Select
YourAppName
项目 菜单中的属性。 - Select 设置 选项卡。
- 在 Name 列中添加新条目,例如:SearchWords。
- 来自类型列,selectSystem.Collections.Specialized.StringCollection.
- 关闭对话框并保存。
检索搜索词
在表单的构造函数或 Load
事件中,假设您有一个名为 txtSearch
:
TextBox
' +
Imports System.Linq
Imports System.Collections.Specialized
Private Sub YourForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.SearchWords Is Nothing Then
My.Settings.SearchWords = New StringCollection
End If
Dim acc As New AutoCompleteStringCollection
acc.AddRange(My.Settings.SearchWords.Cast(Of String).ToArray())
txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest
txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource
txtSearch.AutoCompleteCustomSource = acc
End Sub
更新collection
每次执行搜索时都需要添加新词:
' When you click a search button...
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
AddSearchWord()
End Sub
' If you call the search routine when you press the Enter key...
Private Sub txtSearch_KeyDown(sender As Object, e As KeyEventArgs) Handles txtSearch.KeyDown
If e.KeyCode = Keys.Enter Then
AddSearchWord()
End If
End Sub
' Update the collection...
Private Sub AddSearchWord()
If txtSearch.Text.Trim.Length = 0 Then Return
If Not txtSearch.AutoCompleteCustomSource.Contains(txtSearch.Text) Then
If txtSearch.AutoCompleteCustomSource.Count > 10 Then
txtSearch.AutoCompleteCustomSource.RemoveAt(
txtSearch.AutoCompleteCustomSource.Count - 1)
End If
txtSearch.AutoCompleteCustomSource.Insert(0, txtSearch.Text)
End If
End Sub
保存collection
关闭表单时更新 SearchWord
字符串 collection:
Private Sub YourForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
My.Settings.SearchWords = New StringCollection
My.Settings.SearchWords.AddRange(txtSearch.AutoCompleteCustomSource.Cast(Of String).ToArray)
My.Settings.Save()
End Sub
演示