2

这真让我抓狂。我正在从菜单屏幕上的 button_click 调用表单。在 Form2 上的 form_load 中途,它正在从菜单屏幕重新调用 button_click 并且没有完成 form_load。它总是发生在第二个组合框的 execute_reader 上。

我已经尝试注释掉第二个组合框的整个代码块并且 form_load 完成但是如果我关闭该表单并再次单击菜单屏幕上的按钮,它会重新调用相同的 button_click 但就在 adapter.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
4

2 回答 2

1

把所有的 sql 东西放在一个单独的线程上。然后从 form_load 启动线程。这应该可以解决问题。将繁重的计算/DBMS 代码等放在 UI 线程上并不是一个好的编程习惯。它会在 UI 和计算方面产生问题。

于 2013-12-18T10:58:38.913 回答
0

不要先从直接创建它的实例调用

Dim nWindow As New frmManageEquipment2()
nWindow.Show()

在 Shown 事件中而不是在 Load 事件中编写您的编码

于 2013-12-18T11:02:41.373 回答