Visual Basic输入框关闭错误
问题描述:
这是一个奇怪的问题,并且对这个问题包含的大量代码表示歉意,但是,我已经给了其他人的项目来维护,并且用户已经找到了我一个错误。有一个按钮可以打开InputBox,如下所示。Visual Basic输入框关闭错误
的形式用于输入文件的路径导入。如果用户没有输入路径或不正确的路径,则显示错误 - 很好。现在,问题是,如果用户按下“取消”按钮或右上角的x关闭表单,它也会返回相同的错误,表示无法找到路径。
通过以下代码查看后,我无法弄清楚如何使它在按下x或Cancel时不显示错误,那么任何人都可以帮我解决问题吗?
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
将始终return
一个String
。
- 如果用户按下“确定”,它将返回
String
放入TextBox
。 - 如果他通过按X或取消它取消框
returns ""
。
我通常不会建议使用Inputbox来获取文件路径。改为使用OpenFileDialog
。如果您的Clipboard
中已有完整路径,则只需将其粘贴到OFD
的Filename-Textbox
中,然后按回车。
这应该让你开始:
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
嗨,这是因为输入框没有关闭事件。你需要像这样检查输入:'Dim importbox As String = InputBox(“Input path”,“Import”,“”)'然后'如果String.IsNullOrEmpty(输入框)然后'取消,或者为空' – pLurchi
Inputbox将会总是返回一个字符串。如果用户按下“确定”,它将返回放入文本框的字符串。如果他通过按下X或取消取消框,则返回“”。 – Luke
[VisualBasic InputBox取消]的可能重复(http://stackoverflow.com/questions/19679489/visualbasic-inputbox-cancel) – Luke