单独自己一个窗体运行时,没有出现问题;但是当有打开多个窗体时,从其它窗体转到本窗体时,有时就会出现附件的错误,我在Activate事件上的代码是:执行一个过程RefreshData(完成刷新记录集的功能),过程代码如下:
Public Function RefreshData() As Boolean
Try
Dim cRow As Long
cRow = DataGrid1.CurrentRowIndex
If cRow < 0 Then
cRow = 0
End If
OleDbConnection1.Open()
Dim DataTem As New DataSet
DataGrid1.TableStyles.Clear()
OleDbDataAdapter1.Fill(DataTem, "List")
Newrow = DataTem.Tables(0).Rows.Count
DataGrid1.DataSource = DataTem.Tables(0)
OleDbConnection1.Close()
'初始化button4按钮状态
rs = New ADODB.Recordset
cmd = New ADODB.Command
cmd.ActiveConnection = mConn
cmd.CommandText = "Select IsNull(Sum(PassQty),0) From Base_ModelList Where MoOrdId='" & Trim(TextBox1.Text) & "'"
cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
rs = cmd.Execute
If rs.Fields(0)
.Value > 0 Then
Button4.Enabled = True
xDataSet = DataTem
Else
Button4.Enabled = False
xDataSet = Nothing
End If
If DataTem.Tables(0).Rows.Count >= 1 Then
tModify.Enabled = True
tDelete.Enabled = True
tPrintView.Enabled = True
tOther.Enabled = True
rs = New ADODB.Recordset
cmd = New ADODB.Command
cmd.ActiveConnection = mConn
cmd.CommandText = "Select IsNull(Sum(PassQty),0) From Base_ModelList Where MoOrdId='" & Trim(TextBox1.Text) & "'"
cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
rs = cmd.Execute
If rs.Fields(0)
.Value > 0 Then
Button4.Enabled = True
xDataSet = DataTem
End If
ComboBox2.Text = Trim(DataGrid1.Item(DataGrid1.CurrentRowIndex, 18)) '仓库备注
TextBox10.Text = Trim(DataGrid1.Item(DataGrid1.CurrentRowIndex, 20)) 'QA备注
Else
tModify.Enabled = False
tDelete.Enabled = False
tPrintView.Enabled = False
tOther.Enabled = False
Button4.Enabled = False
xDataSet = Nothing
ComboBox2.Text = "" '仓库备注
TextBox10.Text = "" 'QA备注
End If
If Newrow <> Oldrow Then
RefreshData = False
If Newrow >= 1 Then
If cRow > Newrow - 1 Then '防止超出有效范围
cRow = Newrow - 1
End If
DataGrid1.CurrentRowIndex = cRow
DataGrid1.Select(cRow) '确定当前选中行
If DataGrid1.Item(cRow, 19) = 0 Then
Button3.Text = "允许改判(&Y)"
Else
Button3.Text = "不允许改判(&Y)"
End If
End If
Else
RefreshData = True
If Newrow >= 1 Then
If cRow > Newrow - 1 Then '防止超出有效范围
cRow = Newrow - 1
End If
DataGrid1.CurrentRowIndex = cRow
DataGrid1.Select(cRow) '确定当前选中行
If DataGrid1.Item(cRow, 19) = 0 Then
Button3.Text = "允许改判(&Y)"
Else
Button3.Text = "不允许改判(&Y)"
End If
End If
End If
Oldrow = Newrow '保存最后一次刷新的记录数
'权限控制
If Trim(CurrentUserPower) = "READ" Or Trim(CurrentUserPower) = "PE" Or Trim(CurrentUserPower) = "PEA" Or Trim(CurrentUserPower) = "QA" Or Trim(CurrentUserPower) = "QAA" Then
tModify.Enabled = False
tDelete.Enabled = False
tPrintView.Enabled = False
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
End If
If Trim(CurrentUserPower) = "DM" Then
Button3.Enabled = False
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Function
应该不会是代码的问题,因为发生问题是在"从其它窗体将焦点移到本窗体时发生",这个问题我找到了一个解决方法,但不方便操作.这个方法是:在调用RefreshData过程前先写一句显示消息的语句(如messagebox.show("显示消息")),这样就在任何时候都不会出错了.
我试过将整个项目移到vb.net 2005上去测试,结果在vb.net 2005上没有发现这个问题,所以在此请教大家,是否是vb.net 2003在处理Activate事件上存在问题以及如何解决,谢谢!
附件:
4015552005710172533.JPG