这让我发疯了。我在菜单屏幕上通过button_click调用表单。在Form2上的form_load中途,它将从菜单屏幕重新调用button_click而不是完成form_load。它始终发生在第二个组合框的execute_reader上。
我已经尝试为第二个组合框注释掉整个代码块并且form_load完成但是如果我然后关闭该表单并再次点击菜单屏幕上的按钮,它会重新调用相同的button_click但只是在适配器之后.fill伪(表)。
来自frmMenu的代码
Private Sub btnManageEquipment_Click(sender As Object, e As EventArgs) Handles_ btnManageEquipment.Click
frmManageEquipment2.Show()
Me.Hide()
End Sub
来自frmManageEquipment2_Load的代码
Private Sub frmManageEquipment2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
strString = "Select * from equipment"
adapter = New MySqlDataAdapter(strString, conn)
Dim commandBuilder As New MySqlCommandBuilder(adapter)
table.Locale = System.Globalization.CultureInfo.InvariantCulture
adapter.Fill(table)
bindEquipment.DataSource = table
navEquipment.BindingSource = bindEquipment
Me.txtEqName.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_Name", True))
Me.cboArea.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_area", True))
Me.cboEqSubarea.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_subarea", True))
'Me.cboEqType.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_type", True))
'Me.txtMarca.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_marca", True))
'Me.txtModel.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_model", True))
'Me.txtCapacity.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_capacity", True))
'Me.txtSerieEquipe.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_SerieEquipe", True))
'Me.txtSerieMotor.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_SerieMotor", True))
'Me.txtEngineMarca.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_EngineMarka", True))
'Me.txtEngineModel.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_EngineModel", True))
'Me.txtAirFilter.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_AirFilter", True))
'Me.txtTorqueConversion.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_TorqueConversion", True))
'Me.txtTransmission.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_Transmission", True))
'Me.txtAxle.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_Axle", True))
'Me.txtBrakes.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_Brakes", True))
'Me.txtStarterElecSystem.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_StarterElectSystem", True))
'Me.txtTireSize.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_TireSize", True))
'Me.txtFireSupprSystem.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_FireSupprSystem", True))
'Me.txtCost.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_cost", True))
'Me.txtPurchaseDate.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_PurchaseDate", True))
'Me.txtLifeExpectance.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_LifeExpectancy", True))
'Me.txtSellPrice.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_SellPrice", True))
'Me.dtpSellDate.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_SellDate", True))
''Me.txtEqID.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_ID", True))
'Me.txtEqID.DataBindings.Add(New Binding("Text", Me.bindEquipment, "Eq_ID", True))
cboArea.AutoCompleteMode = AutoCompleteMode.Append
strSQLArea = "Select distinct(eq_Area) from Equipment order by eq_Area"
sqlCommand = New MySqlCommand(strSQLArea, conn)
Dim readerArea As MySqlDataReader = sqlCommand.ExecuteReader()
While readerArea.Read()
cboArea.Items.Add(readerArea.GetString(0))
End While
cboArea.AutoCompleteMode = AutoCompleteMode.Append
cboArea.AutoCompleteSource = AutoCompleteSource.ListItems
cboEqSubarea.AutoCompleteMode = AutoCompleteMode.Append
strSQLSubArea = "Select distinct(eq_SubArea) from Equipment order by eq_SubArea"
sqlCommand = New MySqlCommand(strSQLSubArea, conn)
Dim readerSubArea As MySqlDataReader = sqlCommand.ExecuteReader()
'*****This is where it kicks back to the button click from the menu screen*******
While readerSubArea.Read()
cboEqSubarea.Items.Add(readerSubArea.GetString(0))
End While
cboEqSubarea.AutoCompleteMode = AutoCompleteMode.Append
cboEqSubarea.AutoCompleteSource = AutoCompleteSource.ListItems
End Sub
* * *我已经改变了我完全处理这个问题的方法,但仍然遇到同样的问题。现在我使用数据适配器并在其属性字段中绑定我的文本框。我真的想使用自动完成功能
Private Sub frmManageEquipment2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'AssetmgtDataSet.equipment' table. You can move, or remove it, as needed.
Me.EquipmentTableAdapter.Fill(Me.AssetmgtDataSet.equipment)
strSQL = "Select distinct(eq_Area) from Equipment order by eq_Area"
txtArea.AutoCompleteMode = AutoCompleteMode.SuggestAppend
txtArea.AutoCompleteSource = AutoCompleteSource.CustomSource
sqlCommand = New MySqlCommand(strSQL, conn)
Dim readerArea As MySqlDataReader = sqlCommand.ExecuteReader
Dim sStringCollArea As New AutoCompleteStringCollection
While readerArea.Read()
sStringCollArea.AddRange(New String() {readerArea(0)})
End While
readerArea.Close()
txtArea.AutoCompleteCustomSource = sStringCollArea
strSQL = "Select distinct(eq_SubArea) from Equipment order by eq_SubArea"
txtSubarea.AutoCompleteMode = AutoCompleteMode.SuggestAppend
txtSubarea.AutoCompleteSource = AutoCompleteSource.CustomSource
sqlCommand = New MySqlCommand(strSQL, conn)
Dim readerSubArea As MySqlDataReader = sqlCommand.ExecuteReader
Dim sStringCollSubArea As New AutoCompleteStringCollection
While readerSubArea.Read()
****'This is where it goes back to the menu screen now****
sStringCollSubArea.AddRange(New String() {readerSubArea(0)})
End While
readerSubArea.Close()
txtSubarea.AutoCompleteCustomSource = sStringCollSubArea
End Sub
答案 0 :(得分:1)
将所有sql内容放在一个单独的线程上。然后从form_load启动线程。这应该可以解决这个问题。在UI线程上输入繁重的计算/ DBMS代码等并不是一个好的编程习惯。它会在UI和计算方面产生问题。
答案 1 :(得分:0)
不要先直接创建它的实例来调用
Dim nWindow As New frmManageEquipment2()
nWindow.Show()
在Shown事件中编写您的编码,而不是在Load事件中