怎样用vb打开记事本并在其中输入文字

让它跟数据库的功能相似
就是用户单击command1时,新建一个记事本文档,将text1和text2的内容输出到上面,并保存,下次单击command2时,可以验证当前text1和text2的内容时候与记事本里相符
谢谢3楼(就是原来的1楼)即使修正
可是,模块里的GetConfigString = Trim0(str)是干什么的?
提示说子程序或函数未定义
是trim函数吗?我看不像哎,如果把这句去掉,程序倒是可以运行
但是验证时老是提示text1不正确之类的,我输对了啊
还有,看你很辛苦,提高悬赏楼!

首先新建一个模块,名字随便。把代码复制进去:

Public iniFile As String '存放ini配置文件的位置

'读写ini配置文件的函数
Public Declare Function GetPrivateProfileInt Lib "kernel32" _
Alias "GetPrivateProfileIntA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal nDefault As Long, _
ByVal lpFileName As String) _
As Long
Public 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 Integer, _
ByVal lpFileName As String) _
As Integer
Public Declare Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) _
As Long
'读取配置文件中的字符串
Public Function GetConfigString(ApplicationName As String, Key As String, Default As String) As String
Dim str As String * 256
Call GetPrivateProfileString(ApplicationName, Key, Default, str, 256, iniFile)
GetConfigString = Trim0(str)
End Function
'读取字符串中的整数
Public Function GetConfigInt(ApplicationName As String, Key As String, Default As Long) As Long
Dim i As Long
i = GetPrivateProfileInt(ApplicationName, Key, Default, iniFile)
GetConfigInt = i
End Function
'读取字符串中的布尔值
Public Function GetConfigBoolean(ApplicationName As String, Key As String, Default As Long) As Boolean
GetConfigBoolean = CBool(GetConfigString(ApplicationName, Key, str(Default)))
End Function
'写入配置文件
Public Sub PutConfigString(ApplicationName As String, Key As String, Value As String)
Call WritePrivateProfileString(ApplicationName, Key, Value, iniFile)
End Sub

然后
在主窗口中,双击空白处,粘贴进去下面一行代码:
iniFile = App.path & "\Config.ini"

双击Command1,加入下面2行代码
PutConfigString "Settings", "String1",Text1.Text
PutConfigString "Settings", "String2",Text2.Text

双击Command2,加入下面的所有代码:
Dim str1 As String,str2 As String
str1=GetConfigString("Settings", "String1", "")
str2=GetConfigString("Settings", "String2", "")
if Text1.Text <> str1 Then
msgbox "Text1中的内容与上次不同"
else
if text1.text<>str2 then
msgbox "Text2中的内容与上次不同"
endif
endif

不好意思,忘了一段代码。
'代替系统的Trim()函数,用以截取包含chr(0)的字符串
Public Function Trim0(str As String) As String
If Len(str) = 0 Then
Trim0 = ""
Exit Function
End If
Dim i As Long, b As Long, e As Long
Dim s As String
For i = 1 To Len(str)
s = Mid(str, i, 1)
If s <> Chr(0) And s <> Chr(32) Then
b = i
Exit For
End If
Next

For i = Len(str) To 1 Step -1
s = Mid(str, i, 1)
If s <> Chr(0) And s <> Chr(32) Then
e = i
Exit For
End If
Next
Trim0 = Mid(str, b, e - b + 1)
End Function
系统自己的Trim函数截取API处理过的字符串时,经常出现出错的问题,所以要用这个
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-08-16
'我提供一个简单点儿的代码

Private Sub Form_Load()
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.WriteLine ("哈哈,文件创建成功啦")
a.Close
End Sub

Open "c:\1.txt" For Input As #1 '把c:\test.txt换成你自己的文本文件名
z = 0
While Not EOF(1) '只要没到文件尾部就一直循环
Line Input #1, strtmp '每次读取一行
str1 = Split(strtmp, " ") '把读取到的单行文本按空格分隔成多个字符串,存放到str1中
z = z + 1
'这时,str1就是一个数组str1(0),str1(1)..分别对应field1, field2
Wend
Close #1
第2个回答  2010-08-19
其实不必那么复杂,楼主你看看我的把
Private Sub Command1_Click()
Open "d:\1.txt" For Output As #1
Print #1, text1.Text
Print #1, text2.Text
Close (1)
End Sub

Private Sub Command2_Click()
Open "d:\1.txt" For Input As #1
Line Input #1, a
Line Input #1, b
If a = text1.Text And b = text2.Text Then
MsgBox "正确"
Else
MsgBox "错误"
End If
End Sub