我有一个用户表单,我有一个添加按钮,弹出输入框以输入值,然后将其添加到组合框中。但是,当我卸载用户表单并再次加载它时,该项目不存在。
以下是我的代码:
Private Sub MetroButton1_Click(sender As Object, e As EventArgs) Handles MetroButton1.Click
Dim addnew As String
addnew = InputBox("Please Add New Department.", "Add New Department", vbInformation)
Me.ComboBox2.Items.Add(addnew)
Me.ComboBox2.Update()
End Sub
请告知。
由于 萨勒曼
答案 0 :(得分:0)
我从头开始创建一个新项目并完成它。只需将这些代码添加到您的表单中。 此外,您还可以下载here
的完整工作项目 Private Sub MetroButton1_Click(sender As Object, e As EventArgs) Handles MetroButton1.Click
Dim addnew As String
addnew = InputBox("Please Add New Department.", "Add New Department", vbInformation)
Me.ComboBox2.Items.Add(addnew)
Me.ComboBox2.Update()
'-----------------------
Dim fn As String = Application.StartupPath + "\cb.txt"
Dim a(Me.ComboBox2.Items.Count - 1) As String
'--------------------------------------------
For i As Integer = 0 To Me.ComboBox2.Items.Count - 1
a(i) = Me.ComboBox2.Items(i).ToString
Next
IO.File.WriteAllLines(fn, a)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim fn As String = Application.StartupPath + "\cb.txt"
If IO.File.Exists(fn) = False Then
Exit Sub
End If
Dim a As String() = IO.File.ReadAllLines(fn)
'-------------------------------
Me.ComboBox2.Items.Clear()
For Each c As String In a
If c <> vbCrLf Then
Me.ComboBox2.Items.Add(c)
End If
Next
'----------------------------------
End Sub