大佬教程收集整理的这篇文章主要介绍了vb.net 保存文本框值以备后用,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在表单中使用 textBox
来搜索 pdf 文件中的单词。我想将这些搜索词保存在某处以备后用。因此,当用户在 textBox
中键入一个字母时,会出现一个包含先前搜索词的下拉列表。类似于 windows 在资源管理器中所做的事情。
有没有人有例子或者熟悉这个的人?
您可以在类型为 StringCollection
的应用程序设置条目中存储和检索搜索词列表作为 TextBox.AutoCompleteCustomsource
属性的数据源。
添加新条目
YourAppName
属性。检索搜索词
在表单的构造函数或 Load
事件中,假设您有一个名为 TextBox
的搜索 txtSearch
:
' +
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
更新集合
每次执行搜索时都需要添加新词:
' 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
保存收藏
关闭表单时更新 SearchWord
字符串集合:
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
演示
,我发现了一些代码 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
还有两件事我想改变。我会问一个新问题。
以上是大佬教程为你收集整理的vb.net 保存文本框值以备后用全部内容,希望文章能够帮你解决vb.net 保存文本框值以备后用所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。