文本内行的写入与删除
本帖最后由 xyhqqaa 于 2012-5-2 08:41 编辑,关于在文本内的写入与删除。。当我选择某项的时候,就自动在文本相应行内写入相应数据。当我去掉某项的时候,自动从文本内删除相应数据。但是现在遇到的问题是,文本为空,比如当我添加完第三项跟第四项,相应的,我删除第三跟四的时候却没删除,,主要原因估计是参数只能删除对应行,但是我写入的时候,却是没法根据行写入。。。。。说的有点乱、。。希望有大哥能看懂,,代码如下。。。测试有错#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 226, 251, 430, 206)
$Check1 = GUICtrlCreateCheckbox("Checkbox1", 80, 32, 97, 17)
$Check2 = GUICtrlCreateCheckbox("Checkbox2", 80, 64, 97, 17)
$Check3 = GUICtrlCreateCheckbox("Checkbox3", 80, 104, 97, 17)
$Check4 = GUICtrlCreateCheckbox("Checkbox4", 80, 136, 97, 17)
$Button1 = GUICtrlCreateButton("写入", 80, 176, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
_check()
EndSwitch
WEnd
Func _check()
If GUICtrlRead($check1) = $GUI_CHECKED Then
_FileWriteToLine("c:\test.txt", 1, "Checkbox1", 1)
ElseIf GUICtrlRead($check1) =$GUI_UNCHECKEDThen
_FileWriteToLine("c:\test.txt", 1, "", 1)
EndIf
If GUICtrlRead($check2) = $GUI_CHECKED Then
_FileWriteToLine("c:\test.txt", 2, "Checkbox2", 1)
ElseIf GUICtrlRead($check2) = $GUI_UNCHECKED Then
_FileWriteToLine("c:\test.txt", 2, "", 1)
EndIf
If GUICtrlRead($check3) = $GUI_CHECKED Then
_FileWriteToLine("c:\test.txt", 3, "Checkbox3", 1)
ElseIf GUICtrlRead($check3) = $GUI_UNCHECKED Then
_FileWriteToLine("c:\test.txt", 3, "", 1)
EndIf
If GUICtrlRead($check4) = $GUI_CHECKED Then
_FileWriteToLine("c:\test.txt", 4, "Checkbox4", 1)
ElseIf GUICtrlRead($check4) = $GUI_UNCHECKED Then
_FileWriteToLine("c:\test.txt", 4, "", 1)
EndIf
EndFunc ;==>_check #include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 226, 251, 430, 206)
$Check1 = GUICtrlCreateCheckbox("Checkbox1", 80, 32, 97, 17)
$Check2 = GUICtrlCreateCheckbox("Checkbox2", 80, 64, 97, 17)
$Check3 = GUICtrlCreateCheckbox("Checkbox3", 80, 104, 97, 17)
$Check4 = GUICtrlCreateCheckbox("Checkbox4", 80, 136, 97, 17)
$Button1 = GUICtrlCreateButton("写入", 80, 176, 75, 25)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
_check()
EndSwitch
WEnd
Func _check()
Local $string = ""
For $i = 1 To 4
If BitAND(GUICtrlRead(Eval("Check" & $i)), $GUI_CHECKED) Then
$string &= "Check" & $i & @CRLF
EndIf
Next
_FileWrite("c:\test.txt", $string, 2)
EndFunc ;==>_check
Func _FileWrite($file, $str, $flag)
;$str = StringRegExpReplace($str, '\r\n(?=$)', '')
Local $open = FileOpen($file, $flag)
FileWrite($open, $str)
FileClose($open)
EndFunc ;==>_FileWrite
未清楚 _FileWriteToLine 的工作原理, 经多次测试, 最后一行总无法实现重写!
现在采取迂回战术. 在 Window 里, 文本的换行其实是 等于 '换行加回车'@CRLF
所以:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>
Local $nMsg
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 226, 251, 430, 206)
$Check1 = GUICtrlCreateCheckbox("Checkbox1", 80, 32, 97, 17)
$Check2 = GUICtrlCreateCheckbox("Checkbox2", 80, 64, 97, 17)
$Check3 = GUICtrlCreateCheckbox("Checkbox3", 80, 104, 97, 17)
$Check4 = GUICtrlCreateCheckbox("Checkbox4", 80, 136, 97, 17)
$Button1 = GUICtrlCreateButton("写入", 80, 176, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
_check()
EndSwitch
WEnd
Func _check()
Local $file = 'C:\test.txt'
Local $str = ['line 1', 'line 2', 'line 3', 'line 4']
Local $txt = '', $index = 0
For $i = $check1 To $Check4
If GUICtrlRead($i) = $GUI_CHECKED Then
$txt &= $str[$index] & @CRLF
Else
$txt &= @CRLF
EndIf
$index += 1
Next
$handle = FileOpen($file, 2)
FileWrite($handle, $txt)
FileClose($handle)
EndFunc ;==>_check 本帖最后由 netegg 于 2012-5-2 09:56 编辑
Local $msg, $ret=''
While 1
$msg = GUIGetMsg()
if $msg = $button then
Switch $gui_checked
Case GUICtrlRead($check1)
$ret &= GUICtrlRead($check1, 1) & @crlf
ContinueCase
Case GUICtrlRead($check2)
$ret &= GUICtrlRead($check2, 1) & @crlf
ContinueCase
Case GUICtrlRead($check3)
$ret &= GUICtrlRead($check3, 1) & @crlf
ContinueCase
Case GUICtrlRead($check4)
$ret &= GUICtrlRead($check4, 1) & @crlf
EndSwitch
FileOpen($file, 1)
FileWrite($file, $ret)
FileClose($file)
elseif $msg= -3 then
exitloop
endif
WEnd
楼上高人速度好快啊 回复 4# netegg
貌似蛋哥的方法比较简单,重写文本,管你几行。 本帖最后由 魔导 于 2012-5-2 13:12 编辑
小弟来学习学习,这个用来做记录的吗?
还有:如果问题已解决的话请楼主 标注 [已解决] 有好源码更新,谢谢楼主提供 已解决为绿色怎么搞的 已解决为绿色怎么搞的
502762378 发表于 2012-5-2 20:39 http://www.autoitx.com/images/common/back.gif
悬赏主题
页:
[1]