使用AIGPT编写的VB代码,总是有一个语法错误,不知道这代码怎样
vb吧
全部回复
仅看楼主
level 7
peeppp 楼主
使用几个AIGPT(chatgpt,文心一言),给关键词,要求编一个程序,试下效果,换了几个AI,都有语法错误,烦请大侠能不能看看这代码是不是按我要求写的?出错的那2处代码怎么改?
==========================================
编一个VB6.0程序,功能是:上面有两个文本框text1和text2,两个按钮Command1和Command2,一个Combo1,将 filePath = "d:\t1.txt" 设为全局变量进行调用,在text1和text2中分别输入内容后,点击 Command1按钮就将text1和text2存入d:\t1.txt中,每次一行,可以无限次点击并保存,在text1中输入内容,点Command2就遍历d:\t1.txt,找到对应的内容加载到text2中,而combo1中可以实时显示出t1.txt中text1段中的内容,这样可以快速选择,以达到在text1中输入内容点击Command2同样的效果,注意不要出现 Exit While 语法错误 ,如果点击Command1时,text1中输入的内容与 t1.txt中text1段相同时,用msgbox提示“存在重复内容”,只有不重复的才能保存。
===========================================
Dim filePath As String
Private Sub Form_Load()
filePath = "d:\t1.txt" '设定全局变量
End Sub
Private Sub Command1_Click()
Dim txt1 As String, txt2 As String
Dim fileNum As Integer
txt1 = Text1.Text
txt2 = Text2.Text
If txt1 = "" Or txt2 = "" Then '如果text1或text2中有一个为空则提示错误
MsgBox "请输入内容!", vbExclamation
Exit Sub
End If
fileNum = FreeFile '打开文件
Open filePath For Append As fileNum
Dim lineText As String
Dim flag As Boolean
flag = False
While Not EOF(fileNum) '遍历文件
Line Input #fileNum, lineText
Dim pos As Integer
pos = InStr(lineText, ":") '查找冒号分隔符位置
Dim str1 As String
str1 = Trim(Left(lineText, pos - 1)) '获取text1部分的内容
If str1 = txt1 Then '如果存在重复内容则退出
flag = True
MsgBox "存在重复内容!", vbExclamation
Exit While
End If
Wend
If Not flag Then '如果没有重复内容则将text1和text2写入文件
Print #fileNum, txt1 & ": " & txt2
End If
Close fileNum '关闭文件
Text1.Text = "" '清空text1和text2
Text2.Text = ""
End Sub
Private Sub Command2_Click()
Dim fileNum As Integer, lineText As String
Combo1.Clear
fileNum = FreeFile '打开文件
Open filePath For Input As fileNum
While Not EOF(fileNum) '遍历文件
Line Input #fileNum, lineText
Dim pos As Integer
pos = InStr(lineText, ":") '查找冒号分隔符位置
Dim str1 As String, str2 As String
str1 = Trim(Left(lineText, pos - 1)) '获取text1部分的内容
str2 = Trim(Right(lineText, Len(lineText) - pos)) '获取text2部分的内容
Combo1.AddItem str1 '将text1部分的内容添加到Combo1中
Wend
Close fileNum '关闭文件
End Sub
Private Sub Combo1_Click()
Dim fileNum As Integer, lineText As String
fileNum = FreeFile '打开文件
Open filePath For Input As fileNum
While Not EOF(fileNum) '遍历文件
Line Input #fileNum, lineText
Dim pos As Integer
pos = InStr(lineText, ":") '查找冒号分隔符位置
Dim str1 As String, str2 As String
str1 = Trim(Left(lineText, pos - 1)) '获取text1部分的内容
str2 = Trim(Right(lineText, Len(lineText) - pos)) '获取text2部分的内容
If str1 = Combo1.Text Then '如果找到了对应的text1内容则显示text2的内容并退出循环
Text2.Text = str2
Exit While
End If
Wend
Close fileNum '关闭文件
End Sub
2023年05月25日 00点05分 1
level 7
peeppp 楼主
以下这段也是AI写的(我稍微改动了一点点),可以实现功能,但没有判断重复的功能
Dim filePath As String ' 将 filePath 声明为全局变量
Private Sub Form_Load()
filePath = "d:\t1.txt"
'Combo1.Style = fmStyleDropDownList '将 Combo1 设置为下拉列表框模式
LoadCombo1 ' 读取 d:\t1.txt 中的数据并将 Text1 的数据添加到 Combo1 中
End Sub
Private Sub Button1_Click()
Dim fileNum As Integer
fileNum = FreeFile()
If Text1 = "" Or Text2 = "" Then
MsgBox "请输入内容"
Else
Open filePath For Append As fileNum
Print #fileNum, Text1.Text & " " & Text2.Text
Close fileNum
Combo1.AddItem Text1.Text ' 将 Text1 的数据添加到 Combo1 中
Text1 = ""
Text2 = ""
End If
End Sub
Private Sub Button2_Click()
Dim fileNum As Integer
Dim lineText As String
Dim tempArr() As String
Dim selectedItem As String
selectedItem = Combo1.List(Combo1.ListIndex) ' 获取 Combo1 中选中的项
fileNum = FreeFile()
Open filePath For Input As fileNum
Do While Not EOF(fileNum)
Line Input #fileNum, lineText
tempArr = Split(lineText, " ")
If tempArr(0) = selectedItem Then
Text2.Text = tempArr(1)
Close fileNum
Exit Sub
End If
Loop
Close fileNum
End Sub
Private Sub Combo1_Change()
Dim fileNum As Integer
Dim lineText As String
Dim tempArr() As String
Dim selectedItem As String
selectedItem = Combo1.List(Combo1.ListIndex) ' 获取 Combo1 中选中的项
fileNum = FreeFile()
Open filePath For Input As fileNum
Do While Not EOF(fileNum)
Line Input #fileNum, lineText
tempArr = Split(lineText, " ")
If tempArr(0) = selectedItem Then
Text2.Text = tempArr(1)
Close fileNum
Exit Sub
End If
Loop
Close fileNum
End Sub
Private Sub LoadCombo1()
Dim fileNum As Integer
Dim lineText As String
Dim tempArr() As String
fileNum = FreeFile()
Open filePath For Input As fileNum
Do While Not EOF(fileNum)
Line Input #fileNum, lineText
tempArr = Split(lineText, " ")
Combo1.AddItem tempArr(0) ' 将 t1.txt 中的 Text1 的数据添加到 Combo1 中
Loop
Close fileNum
End Sub
2023年05月25日 00点05分 2
吧务
level 14
没有exit while,可以换成do…loop循环配合exit do,如果用while…wend循环中途退出就用goto
2023年05月26日 06点05分 3
1