搬家第二天-37.Wincc V7.3 MSHFGid控件操作表格深入-多表格联合操作

假设现在我们有这样一个任务,ERP管理系统给现场控制系统下达了一个备选任务表,可能是一个配方清单。现场操作人员查询得到这个清单(原始表)以后可以根据现场生产实际情况依次选择清单记录,每选择一个就会在临时表里面增加一条记录,取消一条,也会相应删除临时表的记录。临时表记录添加后,还可以根据需要上下调整顺序,调整顺序后,原始表中相应字段也会记录这个顺序变化。临时表内容和顺序确认后点击按钮把相关参数传递给PLC,下面我们简要介绍一下实现这个功能的方法。

一 准备工作

    假设SQL Server中有这样一张表,作为ERP下达的备选任务表。

Wincc页面有两个MSHFGrid控件,分别作为原始表(用于查询备选清单)和临时表,一个读取按钮,一个上移按钮(名字修改为upbtn),一个下移按钮(名字修改为downbtn)。查询按钮脚本为:

Sub OnClick(ByVal Item)                          
Dim conn
Dim ssql
Dim ors
Dim ocom
Dim scon
Dim MSHFGrid,temptable,rowcount,i
Dim ADODC
Dim PCName,colscount
PCName=HMIRuntime.Tags("@LocalMachineName").Read
scon="Provider = SQLOLEDB.1;Integrated Security=SSPI;Persist SecurityInfo=False;Initial Catalog =MyDB;Data Source = " &PCName & "\WINCC"
ssql="select * from charttable"
Set conn=CreateObject("ADODB.Connection")
conn.ConnectionString=scon
conn.Cursorlocation=3
conn.open
Set ors=CreateObject("ADODB.RecordSet")
Set ocom=CreateObject("ADODB.Command")
ocom.commandtype=1
Set ocom.ActiveConnection=conn
ocom.CommandText=ssql
Set ors=ocom.Execute
Set MSHFGrid=ScreenItems("MSHFGrid1")
Set MSHFGrid.DataSource=ors
MSHFGrid.Refresh
MSHFGrid.colwidth(1)=2500
Set ors=Nothing
conn.close
Set conn=Nothing
' 增加一列用于勾选,一列记录临时表行号
colscount=MSHFGrid.Cols
MSHFGrid.Cols = colscount + 2
MSHFGrid.TextMatrix(0,colscount)="点击选择"
MSHFGrid.TextMatrix(0,colscount+1)="临时表行号"
MSHFGrid.colwidth(1)=2500

'可以把最后一列隐藏起来


'如果已经设定过一次再次点击读取数据,则清空临时表

Set temptable=ScreenItems("MSHFGrid2")
rowcount=temptable.Rows
temptable.Row=1
For i=1 To temptable.Rows-2
   temptable.RemoveItem temptable.Row
   temptable.Row=1
Next
End Sub

 

在原始表MSHFGrid控件的鼠标点击事件中写入以下代码,根据操作员点击的顺序,把相应的记录添加进入临时表,取消勾选就会删除临时表中相应的记录。

Sub Click(ByVal Item)                      
Dim mshfgrid,TempTable
Dim hid,vid
Dim colcount
Dim i,TempTable_currow
Dim delrow
Set mshfgrid=ScreenItems("MSHFGrid1")
Set TempTable=ScreenItems("MSHFGrid2")
TempTable.colwidth(1)=2500
colcount=mshfgrid.cols-2
TempTable.Cols=colcount+1 '增加一列记录在原表中的行号
hid=mshfgrid.Row
vid=mshfgrid.col
'初始化临时表
For i=1 To colcount-1
 TempTable.TextMatrix(0,i)=mshfgrid.TextMatrix(0,i)
Next
TempTable.TextMatrix(0,TempTable.cols-1)="原始表行号"
If (hid>=1 And hid<=mshfgrid.rows-1) And vid=colcount Then
   If mshfgrid.TextMatrix(hid,vid)="" Then
      mshfgrid.TextMatrix(hid,vid)="x"
      mshfgrid.ColAlignment(vid)=3  '居中
      TempTable.AddItem ""
     
      TempTable_currow=TempTable.Rows-2
      For i=1 To colcount-1
         TempTable.TextMatrix(TempTable_currow,i)=mshfgrid.TextMatrix(hid,i)
      Next
      TempTable.TextMatrix(TempTable_currow,0)="第" & CStr(TempTable_currow) & "步骤"
      mshfgrid.TextMatrix(hid,colcount+1)=CStr(TempTable.Rows-2)
      TempTable.TextMatrix(TempTable_currow,TempTable.Cols-1)=CStr(hid) '记录是原表哪一行
    Else
      delrow=BaseTable.TextMatrix(hid,BaseTable.cols-1)
      TempTable.RemoveItem delrow
      BaseTable.TextMatrix(hid,vid)=""
      BaseTable.TextMatrix(hid,vid+1)=""
      '删除临时表相应行之后,原始表中相应列数字需要修改
      For i=delrow To TempTable.Rows-2
         basetable.TextMatrix(temptable.textmatrix(i,temptable.cols-1),vid+1)=i
      Next
   End If
End If
End Sub

 

在上移按钮添加如下代码,上移后仍可以在原始表中取消勾选,然后临时表也会删除相应项目。

Sub OnClick(ByVal Item)                      
Dim currow,mshfgrid,temptable
Dim colvalue
Dim selrow,selcol
Dim rowno1,rowno2 '这两个变量存放临时表中“原表行号”
Dim rowno3,rowno4 '这两个变量存放原表行号
Dim i,btn
Set temptable=ScreenItems("MSHFGrid2")
Set mshfgrid=ScreenItems("MSHFGrid1")
Set btn=ScreenItems("upbtn")
selrow=temptable.Row
selcol=temptable.Col
For i=1 To temptable.cols-1
  colvalue=temptable.TextMatrix(selrow,i)
  temptable.TextMatrix(selrow,i)=temptable.TextMatrix(selrow-1,i)
  temptable.TextMatrix(selrow-1,i)=colvalue
Next
'改变原表中"临时表行号"字段
temptable.Row=temptable.Row-1
rowno1=temptable.TextMatrix(temptable.Row,temptable.cols-1)
rowno2=temptable.TextMatrix(temptable.Row+1,temptable.cols-1)
mshfgrid.TextMatrix(temptable.TextMatrix(temptable.Row,temptable.Cols-1),mshfgrid.cols-1)=temptable.Row
mshfgrid.TextMatrix(temptable.TextMatrix(temptable.Row+1,temptable.Cols-1),mshfgrid.cols-1)=temptable.Row+1

'如果移动到第一行,按钮失效
If temptable.Row=1 Then
   btn.Enabled=False
Else
   btn.Enabled=True
End If
End Sub

 

在下移按钮添加如下代码,下移后仍可以在原始表中取消勾选,然后临时表也会删除相应项目。

Sub OnClick(ByVal Item)    
Dim currow,mshfgrid,temptable
Dim colvalue
Dim selrow,selcol
Dim i,btn
Set temptable=ScreenItems("MSHFGrid2")
Set btn=ScreenItems("downbtn")
selrow=temptable.Row
selcol=temptable.Col
For i=1 To temptable.cols-1
  colvalue=temptable.TextMatrix(selrow,i)
  temptable.TextMatrix(selrow,i)=temptable.TextMatrix(selrow+1,i)
  temptable.TextMatrix(selrow+1,i)=colvalue
Next
temptable.Row=temptable.Row+1

'改变原表中"临时表行号"字段

rowno1=TempTable.TextMatrix(selrow,TempTable.cols-1)
rowno2=TempTable.TextMatrix(selrow+1,TempTable.cols-1)

BaseTable.TextMatrix(rowno1,BaseTable.cols-1)=selrow
BaseTable.TextMatrix(rowno2,BaseTable.cols-1)=selrow+1


If temptable.Row>=temptable.Row-2 Then
   btn.Enabled=False
Else
   btn.Enabled=True
End If
End Sub

 

保存后运行就能看到效果了,如何想进一步丰富功能,可以模拟临时表中任务完成一条改变一下背景颜色,或者删除掉已完成项目,同时取消原始表中的勾选,这里就不再阐述了。

posted @ 2021-01-30 20:46  来自金沙江的小鱼  阅读(1030)  评论(0编辑  收藏  举报