【已解决】如何删除Label控件所创分页多余信息
本帖最后由 dnvplj 于 2019-7-22 18:42 编辑请问各位朋友, 到最后一页,还是显示第上页的部分信息,如何将其删除,只显示最后一页的信息,见下图。
说明:代码是本论坛 afan 大的,原代码是用“listview”创建的分页,我改为“Label”创建的分页
源码地址:http://autoitx.com/forum.php?mod ... =1&authorid=7644923
#include <GuiListView.au3>
#include <WindowsConstants.au3>
$IniFile = @ScriptDir & "\Config.ini"
$var = _IniReaNames($IniFile)
If @error Then Exit MsgBox(48, "出错!", "发生错误,INI文件读取失败!") ;INI文件读取错误将退出
Dim $iChange = 11, $dqyc = 1 ;$iChange 每页显示多少个项目;$dqyc 为当前页次
Dim $zys = Ceiling($var / $iChange)
GUICreate("Listview分页显示", 400, 450)
$Button1 = GUICtrlCreateButton("<", 110, 400, 30, 22)
$Button2 = GUICtrlCreateButton(">", 190, 400, 30, 22)
$Label = GUICtrlCreateLabel('1', 143, 405, 18, 17, 0x0002)
GUICtrlCreateLabel('/' & $zys, 162, 405, 23, 17)
$Button3 = GUICtrlCreateButton("返回首页", 30, 400, 80, 22)
$Button4 = GUICtrlCreateButton("跳至尾页", 220, 400, 80, 22)
$Combo = GUICtrlCreateCombo('', 305, 402, 60, 22)
$Combodata = ''
For $i = 1 To $zys
$Combodata &= '第' & $i & '页|'
Next
GUICtrlSetData(-1, $Combodata, '第1页')
GUISetState()
Go(1) ;首次读取
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
Case $Button1 ;上一页
Go($dqyc - 1)
Case $Button2 ;下一页
Go($dqyc + 1)
Case $Button3 ;首页
Go(1)
Case $Button4 ;尾页
Go($zys)
Case $Combo ;下拉选择页次
Go(StringRegExpReplace(GUICtrlRead($Combo), '[^\d]', ''))
EndSwitch
WEnd
Func Go($yc)
Local $iNo1 = ($yc - 1) * $iChange + 1
Local $iNo2
If $yc = $zys Then
$iNo2 = $var
SetState(64, 128, 64, 128)
ElseIf $yc = 1 Then
$iNo2 = $iNo1 + $iChange - 1
SetState(128, 64, 128, 64)
Else
$iNo2 = $iNo1 + $iChange - 1
SetState(64, 64, 64, 64)
EndIf
Read($iNo1, $iNo2)
GUICtrlSetData($Label, $yc)
GUICtrlSetData($Combo, '第' & $yc & '页')
$dqyc = $yc
EndFunc ;==>Go
Func SetState($1, $2, $3, $4)
GUICtrlSetState($Button1, $1)
GUICtrlSetState($Button2, $2)
GUICtrlSetState($Button3, $3)
GUICtrlSetState($Button4, $4)
EndFunc ;==>SetState
Func Read($iNo1, $iNo2)
Local $s = 1
For $i = $iNo1 To $iNo2
GUICtrlCreateLabel($i & '、 ' & $var[$i], 45, 10 + $s * 30, 130, 28)
$s += 1
Next
EndFunc ;==>Read
Func _IniReaNames($IniFile)
Local Const $N = 2
Local $var[$N], $p = 1
Local $i = 1
While 1
Local $ClsName = IniRead($IniFile, $i, "test", "")
If $ClsName = "" Then ExitLoop
ReDim $var[$p + 1][$N]
$var[$p] = IniRead($IniFile, $i, "test", "")
$p += 1
$i += 1
WEnd
$var = UBound($var, 1) - 1
Return $var
EndFunc ;==>_IniReaNames
你这每点击一次翻页就创建了一列控件生命不息 创建不止…
应该先创建所有并隐藏,需要时显示对应的。
BTW, 测试至少得要提供可测试的基本条件,ini文件呢? 没有就创建…
$IniFile = @ScriptDir & "\Config.ini"
If Not FileExists($IniFile) Then
Local $sini = ''
For $ii = 1 To 30
$sini &= '[' & $ii & ']' & @CRLF & 'test = 第' & $ii & '测试项目' & @CRLF
Next
Local $hFO = FileOpen($IniFile, 2 + 8 + 512)
FileWrite($hFO, $sini)
FileClose($hFO)
EndIf
本帖最后由 dnvplj 于 2019-7-22 17:41 编辑
afan 发表于 2019-7-22 15:15
你这每点击一次翻页就创建了一列控件生命不息 创建不止…
应该先创建所有并隐藏,需要时显示对应的。
...
afan大,真是对不起,配置文件已上传,但配置文件不是固定的,也可能在加“关键字和值”。
Global $aIniData = _IniReaNames(@ScriptDir & '\Config.ini')
Global $iChange = 11, $nPageNow = 1 ;$iChange 每页显示多少个项目;$nPageNow 为当前页次
Global $nCount = Ceiling($aIniData / $iChange)
Global $Button1, $Button2, $Button3, $Button4, $Label, $Combo, $aILabel, $aLast
_GUICreate()
_PageChange(1) ;首次读取
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
Case $Button1 ;上一页
_PageChange($nPageNow - 1)
Case $Button2 ;下一页
_PageChange($nPageNow + 1)
Case $Button3 ;首页
_PageChange(1)
Case $Button4 ;尾页
_PageChange($nCount)
Case $Combo ;下拉选择页次
_PageChange(StringRegExpReplace(GUICtrlRead($Combo), '[^\d]', ''))
EndSwitch
WEnd
Func _GUICreate()
GUICreate('Label 控件列 分页显示', 400, 450)
Dim $Button1 = GUICtrlCreateButton('<', 110, 400, 30, 22)
Dim $Button2 = GUICtrlCreateButton('>', 190, 400, 30, 22)
Dim $Label = GUICtrlCreateLabel('1', 143, 405, 18, 17, 0x0002)
GUICtrlCreateLabel('/' & $nCount, 162, 405, 23, 17)
Dim $Button3 = GUICtrlCreateButton('返回首页', 30, 400, 80, 22)
Dim $Button4 = GUICtrlCreateButton('跳至尾页', 220, 400, 80, 22)
Dim $Combo = GUICtrlCreateCombo('', 305, 402, 60, 22, 0x3)
Local $Combodata = ''
For $i = 1 To $nCount
$Combodata &= '第' & $i & '页|'
Next
GUICtrlSetData(-1, $Combodata, '第1页')
Dim $aILabel = _CreateAll()
GUISetState()
EndFunc ;==>_GUICreate()
Func _PageChange($yc)
Local $iNo1 = ($yc - 1) * $iChange + 1
Local $iNo2
If $yc = $nCount Then
$iNo2 = $aIniData
_SetState(64, 128, 64, 128)
ElseIf $yc = 1 Then
$iNo2 = $iNo1 + $iChange - 1
_SetState(128, 64, 128, 64)
Else
$iNo2 = $iNo1 + $iChange - 1
_SetState(64, 64, 64, 64)
EndIf
_Show($iNo1, $iNo2)
GUICtrlSetData($Label, $yc)
GUICtrlSetData($Combo, '第' & $yc & '页')
$nPageNow = $yc
EndFunc ;==>_PageChange
Func _SetState($1, $2, $3, $4)
GUICtrlSetState($Button1, $1)
GUICtrlSetState($Button2, $2)
GUICtrlSetState($Button3, $3)
GUICtrlSetState($Button4, $4)
EndFunc ;==>_SetState
Func _Show($iNo1, $iNo2)
If $aLast Then
For $ii = $aLast To $aLast
GUICtrlSetState($aILabel[$ii], 32)
Next
EndIf
For $ii = $iNo1 To $iNo2
GUICtrlSetState($aILabel[$ii], 16)
Next
Dim $aLast = [$iNo1, $iNo2]
EndFunc ;==>_Show
Func _CreateAll()
Local $aC[$aIniData + 1]
For $ii = 1 To $aIniData
$aC[$ii] = GUICtrlCreateLabel($ii & '、 ' & $aIniData[$ii], 45, 30 * Mod($ii - 1, $iChange) + 40, 130, 28)
GUICtrlSetState(-1, 32)
Next
Return $aC
EndFunc ;==>_CreateAll
Func _IniReaNames($IniFile)
If Not FileExists($IniFile) Then
Local $sini = ''
For $ii = 1 To 30
$sini &= '[' & $ii & ']' & @CRLF & 'test = 第' & $ii & '测试项目' & @CRLF
Next
Local $hFO = FileOpen($IniFile, 2 + 8 + 512)
FileWrite($hFO, $sini)
FileClose($hFO)
EndIf
Local Const $N = 2
Local $aIniData[$N], $p = 1
Local $i = 1
While 1
Local $ClsName = IniRead($IniFile, $i, 'test', '')
If $ClsName = '' Then ExitLoop
ReDim $aIniData[$p + 1][$N]
$aIniData[$p] = IniRead($IniFile, $i, 'test', '')
$p += 1
$i += 1
WEnd
$aIniData = UBound($aIniData, 1) - 1
Return $aIniData
EndFunc ;==>_IniReaNames
afan 发表于 2019-7-22 18:20
请问 afan 大,第93至101行起什么作用?
dnvplj 发表于 2019-7-22 18:36
请问 afan 大,第93至101行起什么作用?
方便任何人测试脚本,无需另外下载 ini 文件(如无会自动生成) dnvplj 发表于 2019-7-22 17:34
afan大,真是对不起,配置文件已上传,但配置文件不是固定的,也可能在加“关键字和值”。
#include <WindowsConstants.au3>
Global $IniFile = @ScriptDir & "\Config.ini"
$var = _IniReaNames($IniFile)
If @error Then Exit MsgBox(48, "出错!", "发生错误,INI文件读取失败!") ;INI文件读取错误将退出
Dim $iChange = 10, $dqyc = 1 ;$iChange 每页显示多少个项目;$dqyc 为当前页次
Dim $zys = Ceiling($var / $iChange)
Global $var
GUICreate("Label分页显示", 400, 450)
$Button1 = GUICtrlCreateButton("<", 110, 400, 30, 22)
$Button2 = GUICtrlCreateButton(">", 190, 400, 30, 22)
$Label = GUICtrlCreateLabel('1', 143, 405, 18, 17, 0x0002)
GUICtrlCreateLabel('/' & $zys, 162, 405, 23, 17)
$Button3 = GUICtrlCreateButton("返回首页", 30, 400, 80, 22)
$Button4 = GUICtrlCreateButton("跳至尾页", 220, 400, 80, 22)
$Combo = GUICtrlCreateCombo('', 305, 402, 60, 22)
$Combodata = ''
For $i = 1 To $zys
$Combodata &= '第' & $i & '页|'
Next
GUICtrlSetData(-1, $Combodata, '第1页')
GUISetState()
Go(1) ;首次读取
Go($dqyc + 2)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
Case $Button1 ;上一页
Go($dqyc - 1)
Case $Button2 ;下一页
Go($dqyc + 1)
Case $Button3 ;首页
Go(1)
Case $Button4 ;尾页
Go($zys)
Case $Combo ;下拉选择页次
Go(StringRegExpReplace(GUICtrlRead($Combo), '[^\d]', ''))
EndSwitch
WEnd
Func Go($yc)
Local $iNo1 = ($yc - 1) * $iChange + 1
Local $iNo2
If $yc = $zys Then
$iNo2 = $var
SetState(64, 128, 64, 128)
ElseIf $yc = 1 Then
$iNo2 = $iNo1 + $iChange - 1
SetState(128, 64, 128, 64)
Else
$iNo2 = $iNo1 + $iChange - 1
SetState(64, 64, 64, 64)
EndIf
Read($iNo1, $iNo2)
GUICtrlSetData($Label, $yc)
GUICtrlSetData($Combo, '第' & $yc & '页')
$dqyc = $yc
EndFunc ;==>Go
Func SetState($1, $2, $3, $4)
GUICtrlSetState($Button1, $1)
GUICtrlSetState($Button2, $2)
GUICtrlSetState($Button3, $3)
GUICtrlSetState($Button4, $4)
EndFunc ;==>SetState
Func Read($iNo1, $iNo2)
$var = _IniReaNames($IniFile)
Local $s = 1
For $i = $iNo1 To $iNo2
GUICtrlCreateLabel($i & '、 ' & $var[$i], 45, 10 + $s * 30, 130, 28)
$s += 1
Next
EndFunc ;==>Read
Func _IniReaNames($IniFile)
Local Const $N = 2
Local $var[$N], $p = 1
Local $i = 1
While 1
Local $ClsName = IniRead($IniFile, $i, "test", "")
If $ClsName = "" Then ExitLoop
ReDim $var[$p + 1][$N]
$var[$p] = IniRead($IniFile, $i, "test", "")
$p += 1
$i += 1
WEnd
$var = UBound($var, 1) - 1
Return $var
EndFunc ;==>_IniReaNames
chzj589 发表于 2019-7-22 19:24
感谢回复,运行该代码就直接显示第3页。 不错支持一下
页:
[1]