vb程序中combo控件中用additem添加了几个选项后,在下次重启该程序时如何让combo中仍有上次添加的那几项

我的想法是在退出程序之前把combo中的项目储存到一个文件中,在下一次启动程序的时候combo自动读取该文件中的项目。请高手能给出一个范例的代码。不管怎样,只要能帮我在第二次启动程序后combo中仍有上次关闭程序前输入的项目就行。在生成exe文件后,每次运行一次程序都要自己往里面把上次添加的项目再输入一遍就太麻烦了。谢谢大家了~

Private Sub Form_Load()
If Dir("c:\test.txt") = "" Then Exit Sub
Combo1.Text = ""
Dim MyString As String
Open "c:\test.txt" For Input As #1
Do While Not EOF(1)
DoEvents
Line Input #1, MyString
Combo1.AddItem MyString
Loop
Close #1
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
Open "c:\test.txt" For Output As #3
For i = 0 To Combo1.ListCount - 1
Print #3, Combo1.List(i)
Next i
Close #3
End Sub追问

您这个代码写得很好,我可以单独分开实现,我还想问一下您,如何在写入txt之前把txt里面原有的内容清空?就是在这段代码之前
For i = 0 To Combo1.ListCount - 1
Print #3, Combo1.List(i)
Next i
Close #3
End Sub
插入一个把tet.txt清空的语句谢谢了~

追答

Open "c:\test.txt" For Output As #3
Print #3, “”
Close #3
这样就清空了。
选满意答案么?

温馨提示:内容为网友见解,仅供参考
第1个回答  2013-04-02
如下面这段代码

Dim DwgPath As String
Dim iniFile As String
Dim IniLine As String

iniFile = App.Path & "\abc.txt" ‘你的列表放的位置,建议后缀用ini

Open iniFile For Input As #1
Do While Not EOF(1)
Line Input #1, IniLine
if IniLine<>"" then ListBox1.AddItem LinLen ’把文本文件中的内容按行输入到ListBox1中
Loop
Close #1追问

但是如何把ListBox1中内容输入到文本内容中呢?您只告诉我了读入,但是必须先将ListBox1中内容储存到abc.txt中后才能执行您给我的这段代码呀。您能帮我再写一个配套的combo中的项目存储入txt中的代码吗?谢谢了~

追答

给你一段我的程序参考,读写改都有的;

运用于CAD上的,没有CAD,也可以对列表进行操作的!

追问

谢谢了~我认真研究一下,如果我成功了,立马把分给您~

追答

嗯 ,注意,我那个列表与dwg文件挂勾的,没有dwg文件也不会加载在ListBox1里面;用的时候注意一下!或把dwg关联的代码删掉!

文件尾加入内容
Open AddiniPath For Append As #1
Print #1, "加入的内容"
Close #1

你这个可以双击表框打开 ini文件或txt文件,手动改列表就好了,下次也是存好的!
方法:
Shell "notepad.exe D:\Excel\ModifyRecord.ini", vbNormalFocus

D:\Excel\ModifyRecord.ini为你的列表文件,或D:\Excel\ModifyRecord.txt

我说的ini文 件就是配置文件!

第2个回答  2013-04-02
Private Sub Form_Load()
Combo1.Text = ""
If Dir(App.Path & "\ComboSave.txt") <> "" Then
Dim ListAdd As Integer, Item() As String, ComboSaved As String
Open App.Path & "\ComboSave.txt" For Input As #Read
ComboSaved = Input(LOF(Read), 1)
Close #Read
Item = Split(ComboSaved, vbCrLf)
For ListAdd = 1 To UBound(Item)
Combo1.AddItem Item(ListAdd)
Next ListAdd
Else
Exit Sub
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim ListSave As Integer
Open App.Path & "\ComboSave.txt" For Output As #Save
For ListSave = 1 To Combo1.ListCount
Print #Save, Combo1.List(ListSave)
Next ListSave
Close #Save
End Sub

上面的代码实现运行时自动添加项目,退出是保存项目。
如果清空文件,可以直接使用删除:
Set WshShell = CreateObject("Wscript.Shell")
WshShell.Run "cmd.exe /c del " & App.Path & "\ComboSave.txt"
Open App.Path & "\ComboSave.txt" For Output As #Empty
Print #Empty, ""
Close #Empty
希望我能帮助到你。
第3个回答  2013-04-02
配置文件?追问

什么叫配置文件?我就是想在form_unload的时候把combo中项目写入txt文件中,让后下一次form_load的时候再把txt中项目加载到combo中。

追答

INI 文件以key=value的方式存取

Private Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileStringA" (ByVal AppName As String, ByVal KeyName As String, ByVal keydefault As String, ByVal Filename As String) As Long
用这两个读写,代码自己搞定吧,给你个例子吧
Private Function GetSet(F As String, nApp As String, nKey As String) As String
'读取某条目的设置
Dim nStr As String, nSize As Long, S As Long
nSize = 1024
nStr = String(nSize, 0)
S = GetPrivateProfileString(nApp, nKey, "", nStr, nSize, F)
S = InStr(nStr, vbNullChar & vbNullChar)
If S > 0 Then nStr = Left(nStr, S - 1)
GetSet = nStr
End Function
Public Sub SaveSet(strSection As String, strKey As String, strValue As String, strINI As String)
'保存配置文件
WritePrivateProfileString strSection, strKey, strValue, strINI
End Sub
Private Sub Command2_Click()
Dim strValue As String
strValue = GetSet("c:vb-test.ini", "OK", "isOK")
MsgBox strValue
End Sub
Private Sub Command3_Click()
SaveSet "OK", "isOK", "dfafdadfa", "c:vb-test.ini"
End Sub

vb程序中combo控件中用additem添加了几个选项后,在下次重启该程序时如...
Combo1.Text = ""Dim MyString As String Open "c:\\test.txt" For Input As #1 Do While Not EOF(1)DoEvents Line Input #1, MyString Combo1.AddItem MyString Loop Close #1 End Sub Private Sub Form_Unload(Cancel As Integer)Dim i As Integer Open "c:\\test.txt" For Output As ...

...box1中的数据自动生成一行在ComboBox1.AddItem "输入的内容"_百度...
i As Long text = combobox1.text '获取输入的内容 For i = 0 To combobox1.ListCount If combobox1.List(i) = text Then Exit Sub '如果输入的内容已经存在于列表中则退出 Next combobox1.AddItem text '添加输入的内容到列表中End Sub ...

VB中再combo中输入内容,点击确定按钮后清空combo中的text,同时前几次输...
'将combo1中的内容写入1.txt Open App.Path & "\\\\1.txt" For Append As #fNum If Combo1.Text <> "" Then Print #fNum, Combo1.Text End If Close #fNum '清空combo1.text Combo1.Text = ""'清空combo1的list内容,否则会出现重复添加 Combo1.Clear '调用addText函数,向combo1中添...

在vb中Combo1里面有A、B、C,在Combo2中当我点Combo1中A时候,
Private Sub Combo1_Click()Combo2.ClearSelect Case Combo1.TextCase "A" Combo2.AddItem "1" Combo2.AddItem "2" Combo2.AddItem "3"Case "B" Combo2.AddItem "4" Combo2.AddItem "5" Combo2.AddItem "6"Case "C" Combo2.AddItem "7" Combo2.AddItem "8"...

VB中如何实现输入符合下拉框选项的代码就能在下拉框中...
combo控件的change事件 判断combo控件的text属性是否为 字符串"3"如果是字符串"3" , 设置 listindex属性值为2 ,然后把输入焦点定位到text控件 Private Sub Combo1_Change()If Combo1.Text = "3" Then Combo1.ListIndex = 2 Text1.SetFocus End If End Sub ...

vb combo控件的变化问题,我combo1控件有几个下拉项,当选择某一项的时候...
Private Sub Form_Load()Combo1.AddItem (1)Combo1.AddItem (2)Combo1.AddItem (3)End Sub 以上的代码实现的功能和你设想的一样,测试无问题 Private Sub Combo1_Change()If IsNumeric(Combo1.Text) = False Then Combo1.Text = ""End If End Sub 如果有类似代码的话,显示就成空了 ...

vb combo控件问题
Private Sub Command1_Click()Combo1.ListIndex = 0 End Sub Private Sub Form_Load()Combo1.AddItem 123 Combo1.AddItem 234 End Sub 比如,以上程序就不会出错,当你单击command1按钮时,combo1就会显示123 listindex属性:当下拉内容这空时是-1;当有下拉内容时从0开始计数 你出现这个错误中因为...

Combo1的下拉列表中添加的多个项目如何在软件重启后照样显示?_百度知 ...
Private Sub Form_Load()Open App.Path & "\\temp.txt" For Input As #1 Dim str As String str = ""Do While Not EOF(1)Line Input #1, str '按行读取 Combo1.AddItem str Loop Close #1 End Sub 修改了一下,完全可以了,你要确保temp.txt中有分行的文字.

vb 中combox控件的使用
'添加窗体Form1,按钮Command1,Command2,Command3,组合框Combo1,然后添加如下代码:PrivateSubForm_Load()Combo1.AddItem"1"Combo1.AddItem"2"Combo1.AddItem"3"Command1.Visible=False Command2.Visible=False Command3.Visible=False EndSub PrivateSubCombo1_Click()Command1.Visible=False Command2....

VB下拉框怎么做Combo1
VB下拉框ComboBox 控件需要使用 AddItem 或 RemoveItem 方法。设置 List、ListCount、和 ListIndex 属性,使访问 ComboBox 中的项目成为可能。也可以在设计时使用 List 属性将项目添加到列表中。 ComboBox 控件,ComboBox 控件将 TextBox 控件和 ListBox 控件的特性结合在一起-既可以在控件的文本框部分输入信息,也可...

相似回答
大家正在搜