找回密码
 加入
搜索
查看: 4459|回复: 13

怎么把TXT文件读入文本框中?

[复制链接]
发表于 2008-7-30 08:01:58 | 显示全部楼层 |阅读模式
怎么把TXT文件读入文本框中,然后在文本框中修改,点确定按钮能够保存修改后的TXT文件内容。

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 633, 447, 193, 125)
$Edit1 = GUICtrlCreateEdit("", 24, 16, 289, 153, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))
GUICtrlSetData(-1, "Edit1")
$Button1 = GUICtrlCreateButton("确定", 184, 184, 113, 33, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit

        EndSwitch
WEnd


[ 本帖最后由 redapple2008 于 2008-7-31 07:00 编辑 ]
发表于 2008-7-30 08:38:33 | 显示全部楼层
$file = FileOpen("1.txt", 0)
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $Cnt += $Line & @CR;如果不用精确到行,可以使用 FileRead 
 Wend
GUICTRLSETDATA($Input1, $CNT)

手头没有 AU3 相关的东西,大致是这样,难免有错。
重新写回去,可以在帮助中查找 FileWrite。
 楼主| 发表于 2008-7-30 08:38:59 | 显示全部楼层
为什么读TXT文件的时候是选中状态?
 楼主| 发表于 2008-7-30 08:44:34 | 显示全部楼层
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
 
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 633, 447, 193, 125)
$Edit1 = GUICtrlCreateEdit("", 24, 16, 289, 153, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))
;GUICtrlSetData(-1, "Edit1")
$Button1 = GUICtrlCreateButton("确定", 184, 184, 113, 33, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$file = FileOpen("新建 文本文档.txt", 0)
dim $Cnt
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $Cnt += $Line & @CR
        ;如果不用精确到行,可以使用 FileRead 
        GUICTRLSETDATA($Edit1, $CNT)
 Wend

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
 
    EndSwitch
WEnd


还是不行?
发表于 2008-7-30 11:02:46 | 显示全部楼层
读取
while 1
if @error<>0 then exitloop
else
$line=FileReadLine($file,$i) & @CRLF & $line
endif
$i=$i+1
wend
GuiCtrlsetdata($Edit, $line)
写回
filewrite($fiel, $line)

[ 本帖最后由 netegg 于 2008-7-30 11:06 编辑 ]
 楼主| 发表于 2008-7-30 18:15:04 | 显示全部楼层
为什么文本框读TXT文件的时候是选中状态?
 楼主| 发表于 2008-7-30 18:36:55 | 显示全部楼层
回写不了。请改一下谢谢!

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
 
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 633, 447, 193, 125)
$Edit1 = GUICtrlCreateEdit("", 24, 16, 289, 153, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))
;GUICtrlSetData(-1, "Edit1")
$Button1 = GUICtrlCreateButton("确定", 184, 184, 113, 33, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$file = FileOpen("新建 文本文档.txt", 0)
$i=1
While 1
                $line=FileReadLine($file,$i) 
  ;  $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
          $i=$i+1
           GUICTRLSETDATA($Edit1, $line)
  ;  $Cnt += $Line & @CR
    ;如果不用精确到行,可以使用 FileRead 
        FileClose("新建 文本文档.txt")
 Wend
 
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
                Case $Button1
          filewrite($file, $line)
    EndSwitch
WEnd
发表于 2008-7-30 20:18:06 | 显示全部楼层
#include <file.au3>
#include <GuiEdit.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $Files="1.txt"

$GUI = GUICreate("", 423, 254, 193, 115)
$Edit = GUICtrlCreateEdit("", 8, 8, 401, 201)
$OK = GUICtrlCreateButton("编辑", 8, 216, 75, 25, 0)
Readdate()
GUISetState()

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $OK
                        Setdate()

        EndSwitch
WEnd

Func Readdate()
        Dim $aRecords
        If Not _FileReadToArray($Files, $aRecords) Then
                MsgBox(4096, "错误", "无法打开文件!", 2)
                Exit
        EndIf
        For $x = 1 To $aRecords[0]
                GUICtrlSetData($Edit, $aRecords[$x] & @CRLF, $x)
        Next
EndFunc   ;==>readdate

Func Setdate()
        $Max = _GUICtrlEdit_GetLineCount($Edit)
        For $y = 1 To $Max
                $txt = _GUICtrlEdit_GetLine($Edit, $y)
                _FileWriteToLine($Files, $y, $txt, 1)

        Next
EndFunc   ;==>Setdate
发表于 2008-7-30 20:56:18 | 显示全部楼层
干嘛要用数组
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $Files="log.txt"

$GUI = GUICreate("", 423, 254, 193, 115)
$Edit = GUICtrlCreateEdit("", 8, 8, 401, 201)
GUICtrlSetData(-1,readdate())
$OK = GUICtrlCreateButton("保存", 8, 216, 75, 25, 0)
GUISetState()
send("{down}")
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        ExitLoop
                Case $OK
                        Setdate()

        EndSwitch
WEnd

Func Readdate()
        Local $txt,$temp
        $temp=FileOpen($Files,0)
        $txt=FileRead($temp)
        FileClose($temp)
        Return $txt
EndFunc   ;==>readdate

Func Setdate()
        Local $txt,$temp
        $temp=FileOpen($Files,2)
        $txt=GUICtrlRead($Edit)
        FileWrite($temp,$txt)
        FileClose($temp)
EndFunc   ;==>Setdate

评分

参与人数 1金钱 +2 贡献 +1 收起 理由
redapple2008 + 2 + 1 我很赞同

查看全部评分

发表于 2008-7-30 22:06:52 | 显示全部楼层

回复 9# pcbar 的帖子

老大就是老大,简洁明了,学习了。
发表于 2009-10-25 12:41:49 | 显示全部楼层
对在下有用,留着备用!
发表于 2011-12-26 17:18:42 | 显示全部楼层
不错。。学习了。
发表于 2012-10-5 13:47:00 | 显示全部楼层
谢谢,学习了
发表于 2013-1-29 16:08:21 | 显示全部楼层
学习了,谢谢
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-29 11:42 , Processed in 0.090478 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表