VB怎么打开记事本输入文字

如题所述

Private Sub Form_Click()

Dim str As String

Dim num As Integer

Open "d:\Myfile.txt" For Output As #1

str = “STUDY"

num = 12345

Print #1, "Print输出:"Print #1, str, num

Print #1, "*****************"                   

Print #1, "Write输出:"

Write #1, str, num   

Close #1

End Sub

记事本的效果如图:

希望能帮到你!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-08-15
首先新建一个模块,名字随便。把代码复制进去:

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处理过的字符串时,经常出现出错的问题,所以要用这个追问

我早就看见了 不是这个。。。。。。

第2个回答  2014-08-15
VB调用记事本Shell("notepad.exe", vbNormalFocus)