gpt4 book ai didi

vb.net - Visual Basic InputBox关闭错误

转载 作者:行者123 更新时间:2023-12-03 08:12:03 26 4
gpt4 key购买 nike

这是一个很奇怪的问题,对于该问题所包含的大量代码表示歉意,但是,我得到了别人要维护的项目,并且用户来找我时出错了。有一个按钮可以打开InputBox,如下所示。

enter image description here

该表格用于输入要导入的文件的路径。如果用户没有输入路径,或者输入的路径不正确,则会显示错误-可以。现在的问题是,如果用户按下“取消”按钮或右上角的x来关闭表单,它还会返回相同的错误,表示找不到路径。

看完以下代码后,我不知道如何制作该代码,以便在按x或“取消”时不显示错误,所以有人可以帮助我吗?

 Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click

Try
Dim importbox As String = InputBox("Input path", "Import", "")
Dim fi As New FileInfo(importbox)
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & fi.DirectoryName

Dim conn As New OleDbConnection(connectionString)
conn.Open()

Dim add1 As String = ""
Dim add2 As String = ""
Dim add3 As String = ""
Dim add4 As String = ""
Dim add5 As String = ""
Dim postcode As String = ""
Dim telephone As String = ""
Dim fax As String = ""
Dim email As String = ""
Dim customercode As String = ""
Dim customername As String = ""
Dim webpage As String = ""
Dim mobile As String = ""

Dim headerTable As DataTable = ugHeaders.DataSource
Dim csvArray(headerTable.Rows.Count) As String
Dim i As Integer = 0
For Each dr As DataRow In headerTable.Rows
csvArray(i) = dr.Item("CSVName")
Next

For Each dr As DataRow In headerTable.Rows
Select Case dr.Item("DBName").ToString.Trim
Case "Add1"
add1 = dr.Item("CSVName")
Case "Add2"
add2 = dr.Item("CSVName")
Case "Add3"
add3 = dr.Item("CSVName")
Case "Add4"
add4 = dr.Item("CSVName")
Case "Add5"
add5 = dr.Item("CSVName")
Case "PostCode"
postcode = dr.Item("CSVName")
Case "Telephone"
telephone = dr.Item("CSVName")
Case "Fax"
fax = dr.Item("CSVName")
Case "Email"
email = dr.Item("CSVName")
Case "Customer_Name"
customername = dr.Item("CSVName")
Case "Customer_Code"
customercode = dr.Item("CSVName")
Case "webpage"
webpage = dr.Item("CSVName")
Case "mobile_phone"
mobile = dr.Item("CSVName")
End Select
Next

Dim sqlSelect As String = "SELECT Company, [" & add1 & "], [" & add3 & "], [" & postcode & "], [" & add2 & "], " & _
"[" & telephone & "], [" & fax & "], [" & email & "], [" & customercode & "], " & _
"[" & add4 & "], [" & add5 & "], [" & webpage & "], [" & mobile & "] FROM " & fi.Name

Dim cmdSelect As New OleDbCommand(sqlSelect, conn)

Dim adapter1 As New OleDbDataAdapter(cmdSelect)

Dim ds As New DataSet
adapter1.Fill(ds, "DATA")

pb_progress.Maximum = ds.Tables(0).Rows.Count
pb_progress.Value = 0

For Each dr As DataRow In ds.Tables(0).Rows
Try
Debug.WriteLine(dr.Item(customercode).ToString.Trim)

If dr.Item(customercode).ToString.Trim = "" Then
Dim str As String = dr.Item(customername)
If str.Trim = "" Then Continue For
Dim length As Integer = str.Length
If length < 20 Then
Else
length = 20
End If

str = Replace(str.Substring(0, length), " ", "_").ToUpper
str = Regex.Replace(str, "[^a-zA-Z _&]", "")

Dim found As Boolean = True
Dim loopcount As Integer = 1

Do Until found = False
Dim checkSql As String = "SELECT * FROM Customers WHERE [Customer_Code] = @ccode"
Dim checkCmd As New OleDb.OleDbCommand(checkSql, con)
checkCmd.Parameters.AddWithValue("@ccode", str)
Dim checkDa As New OleDb.OleDbDataAdapter(checkCmd)
Dim checkDt As New DataTable
checkDa.Fill(checkDt)

If checkDt.Rows.Count <> 0 Then
found = True
str &= CStr(loopcount)
loopcount += 1
Else
found = False
End If
Loop

dr.Item(customercode) = str
Else
Dim found As Boolean = True
Dim loopcount As Integer = 1
Do Until found = False
Dim checkSql As String = "SELECT * FROM Customers WHERE [Customer_Code] = @ccode"
Dim checkCmd As New OleDb.OleDbCommand(checkSql, con)
checkCmd.Parameters.AddWithValue("@ccode", dr.Item(customercode))
Dim checkDa As New OleDb.OleDbDataAdapter(checkCmd)
Dim checkDt As New DataTable
checkDa.Fill(checkDt)

If checkDt.Rows.Count <> 0 Then
found = True
dr.Item(customercode) &= CStr(loopcount)
loopcount += 1
Else
found = False
End If
Loop
End If

Dim sql As String
sql = "INSERT INTO Customers(Customer_Code, Customer_Name, Contract_Payment_Terms, Aq_Date, Telephone, Fax, Email, Average_Payment_Terms, webpage, mobile_phone) " & _
"VALUES(@ccode, @cname, 30, #01/01/2016#, @ctele, @cfax, @email, 30, @webpage, @mobile);"
Dim cmd As New OleDb.OleDbCommand(sql, con)
With cmd.Parameters
.AddWithValue("@ccode", dr.Item(customercode))
.AddWithValue("@cname", dr.Item(customername))
.AddWithValue("@ctele", dr.Item(telephone).ToString.Truncate(48))
.AddWithValue("@cfax", dr.Item(fax))
.AddWithValue("@email", dr.Item(email))
.AddWithValue("@webpage", dr.Item(webpage))
.AddWithValue("@mobile", dr.Item(mobile))
End With
cmd.ExecuteNonQuery()

sql = "INSERT INTO [Customer_Addresses] (Cust_Code, PostCode, Alias, Add1, Add2, Add3, Add4, Add5) VALUES(@ccode, @pcode, 'Default'" & _
",@add1, @add2, @add3, @add4, @add5);"
cmd = New OleDb.OleDbCommand(sql, con)
With cmd.Parameters
.AddWithValue("@ccode", dr.Item(customercode))
.AddWithValue("@pcdoe", dr.Item(postcode))
.AddWithValue("@add1", dr.Item(add1))
.AddWithValue("@add2", dr.Item(add2))
.AddWithValue("@add3", dr.Item(add3))
.AddWithValue("@add4", dr.Item(add4))
.AddWithValue("@add5", dr.Item(add5))
End With
cmd.ExecuteNonQuery()

Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try


pb_progress.Increment(1)
Next

MsgBox("Import successful", MsgBoxStyle.OkOnly, "Success")

Catch ex As Exception
errorLog(ex)
End Try
End Sub

最佳答案

Inputbox将始终returnString

  • 如果用户按下“确定”,它将返回String中放入的TextBox
  • 如果他通过按X或Cancel it取消框returns ""

  • 我通常不建议使用Inputbox获取文件路径。请改用 OpenFileDialog。如果您的 Clipboard中已经有完整路径,则可以将其粘贴到 Filename-TextboxOFD中,然后按Enter。

    这应该使您开始:
    Dim ofd as new OpenFileDialog()
    // Show the File Dialog to the user and detect he pressed OK or Cancelled
    if ofd.ShowDialog = Windows.Forms.DialogResult.OK
    // Always check, if the file really exists
    if IO.File.exists(ofd.FileName)
    importbox = ofd.FileName
    Else
    msgbox("File does not exist")
    Exit Sub
    End if
    Else
    Exit Sub
    End if

    关于vb.net - Visual Basic InputBox关闭错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38764441/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com