[已解决]请教:edit文本框手动输入可以保存到ini文件赋值怎么却不行
本帖最后由 afoo 于 2016-1-4 19:53 编辑请教各位
如图所示,如果我手工在数据库名称的input控件中输入是能用iniwrite写入到ini文件中的,
但是点击选择按钮获取文件路径然后用guictrlsetdata复制给input控件后却不能用iniwrite写入到ini文件中了,
不知何故,请大家指教!谢谢
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>
Local Const $sFileName = "OITool.ini"
Local $sFileOpen = FileOpen ($sFileName)
Local $sType, $sFile, $sPassowrd, $Data_v
Local $sRead = IniRead($sFileName, "General", "Setup", "Default Value")
If $sRead = "1" Then
;~ MsgBox(0,"配制文件已存在","如需修改请删除配制文件。",5)
;~ Exit
EndIf
#Region ### START Koda GUI section ### Form=
$hGUI = GUICreate("OIT Auto", 356, 186, -1, -1)
$Database = GUICtrlCreateLabel("数据库类型", 50, 20, 64, 21)
$Datebasetype = GUICtrlCreateCombo("", 120, 16, 217, 21);~, BitOR($ES_READONLY,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "ACCESS|MSSQL", "ACCESS")
$Label1 = GUICtrlCreateLabel("数据库名称或路径", 15, 68, 100, 21)
$File_path = GUICtrlCreateInput("", 120, 64, 185, 21)
;~ GUICtrlSetState($File_path, $GUI_DROPACCEPTED)
$Select_file = GUICtrlCreateButton("选择", 308, 64, 30, 21)
$EnterPassLabel = GUICtrlCreateLabel("MSSQL密码", 57, 108, 65, 17, 0)
$PasswordEdit = GUICtrlCreateInput("", 120, 104, 217, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
;~ GUICtrlSetState(-1, $GUI_DISABLE)
$ButtonSave = GUICtrlCreateButton("保存[&S]", 182, 152, 75, 25)
$ButtonExit = GUICtrlCreateButton("退出[&C]", 263, 152, 75, 25)
GUISetState(@SW_SHOW, $hGUI)
#EndRegion ### END Koda GUI section ###
While 1
Local $sMessage = "请选择 MDB 文件"
$Data_v = GUICtrlRead($Datebasetype)
Switch $Data_v
Case "MSSQL"
_GUICtrlEdit_SetReadOnly($PasswordEdit, False)
Case "ACCESS"
_GUICtrlEdit_SetReadOnly($PasswordEdit, True)
EndSwitch
$nMsg = GUIGetMsg()
If $nMsg=$Select_file And $Data_v <> "MSSQL" Then
Local $sFileOpenDialog = FileOpenDialog($sMessage, "", "(*.mdb)", $FD_FILEMUSTEXIST)
GUICtrlSetData($File_path, $sFileOpenDialog)
EndIf
Switch $nMsg
Case $ButtonSave
_SaveFile()
Case $GUI_EVENT_CLOSE,$ButtonExit
ExitLoop
Case $GUI_EVENT_DROPPED
EndSwitch
FileClose($sFileOpen)
WEnd
GUIDelete($hGUI)
Func _SaveFile()
$sType = GUICtrlRead($Datebasetype)
$sFile = GUICtrlRead($File_path)
$sPassowrd = GUICtrlRead($PasswordEdit)
IniWrite($sFileName, "General", "Setup", "1")
IniWrite($sFileName, "DataType", "Type", $sType)
IniWrite($sFileName, "FilePath", "Path", $sFile)
IniWrite($sFileName, "Password_Set", "Password", $sPassowrd)
EndFunc;==>_SaveFile 错误在24行,$File_path 这个不是全局变量。 回复 2# ak47gglllk
你好!谢谢指点!
但我在最上面加了Global $File_path 貌似也不行啊 本帖最后由 haijie1223 于 2016-1-4 17:44 编辑
代码问题多多:
1、首先全局变量要用Global,不要用local。
2、读写INI文件用文件名,就不用fileopen创建文件句柄了。
3、在while循环里面尽量不要定义变量。
4、既然创建了条件运行语句Switch $nMsg,干嘛还要$nMsg=$Select_file?
5、不想手动修改Combo的话,加上静态文本样式。 回复 3# afoo
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>
Local Const $sFileName = "OITool.ini"
Local $sFileOpen = FileOpen ($sFileName)
Local $sType, $sFile, $sPassowrd, $Data_v
Local $sRead = IniRead($sFileName, "General", "Setup", "Default Value")
Global $File_path
If $sRead = "1" Then
;~ MsgBox(0,"配制文件已存在","如需修改请删除配制文件。",5)
;~ Exit
EndIf
#Region ### START Koda GUI section ### Form=
$hGUI = GUICreate("OIT Auto", 356, 186, -1, -1)
$Database = GUICtrlCreateLabel("数据库类型", 50, 20, 64, 21)
$Datebasetype = GUICtrlCreateCombo("", 120, 16, 217, 21);~, BitOR($ES_READONLY,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "ACCESS|MSSQL", "ACCESS")
$Label1 = GUICtrlCreateLabel("数据库名称或路径", 15, 68, 100, 21)
$File_path = GUICtrlCreateInput("", 120, 64, 185, 21)
;~ GUICtrlSetState($File_path, $GUI_DROPACCEPTED)
$Select_file = GUICtrlCreateButton("选择", 308, 64, 30, 21)
$EnterPassLabel = GUICtrlCreateLabel("MSSQL密码", 57, 108, 65, 17, 0)
$PasswordEdit = GUICtrlCreateInput("", 120, 104, 217, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
;~ GUICtrlSetState(-1, $GUI_DISABLE)
$ButtonSave = GUICtrlCreateButton("保存[&S]", 182, 152, 75, 25)
$ButtonExit = GUICtrlCreateButton("退出[&C]", 263, 152, 75, 25)
GUISetState(@SW_SHOW, $hGUI)
#EndRegion ### END Koda GUI section ###
While 1
Local $sMessage = "请选择 MDB 文件"
$Data_v = GUICtrlRead($Datebasetype)
Switch $Data_v
Case "MSSQL"
_GUICtrlEdit_SetReadOnly($PasswordEdit, False)
Case "ACCESS"
_GUICtrlEdit_SetReadOnly($PasswordEdit, True)
EndSwitch
$nMsg = GUIGetMsg()
If $nMsg=$Select_file And $Data_v <> "MSSQL" Then
Local $sFileOpenDialog = FileOpenDialog($sMessage, "", "(*.mdb)", $FD_FILEMUSTEXIST)
GUICtrlSetData($File_path, $sFileOpenDialog)
EndIf
Switch $nMsg
Case $ButtonSave
MsgBox(0,0,GUICtrlRead($File_path))
_SaveFile()
Case $GUI_EVENT_CLOSE,$ButtonExit
ExitLoop
Case $GUI_EVENT_DROPPED
EndSwitch
FileClose($sFileOpen)
WEnd
GUIDelete($hGUI)
Func _SaveFile()
$sType = GUICtrlRead($Datebasetype)
$sFile = GUICtrlRead($File_path)
$sPassowrd = GUICtrlRead($PasswordEdit)
IniWrite($sFileName, "General", "Setup", "1")
IniWrite($sFileName, "DataType", "Type", $sType)
IniWrite($sFileName, "FilePath", "Path", $sFile)
IniWrite($sFileName, "Password_Set", "Password", $sPassowrd)
EndFunc;==>_SaveFile
看下是否是你要的效果,楼上大神的建议仔细看下 回复 4# haijie1223
谢谢版主大人指教,我再折腾试试。
只是第五点不明白,烦请详解。 工作路径改变的问题,第9行改一下即可:Local Const $sFileName = @ScriptDir & '\OITool.ini' 本帖最后由 afoo 于 2016-1-4 19:57 编辑
终于达到我的目的了,如@afan版主所示。谢谢
谢谢楼上高手们,对于autoit,作为新手的我还有太多的东西需要学习,谢谢你们的热心指点。 回复 8# afoo
有些逻辑需要调整下#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
Local Const $sFileName = @ScriptDir & '\OITool.ini'
Local $sType, $sFile, $sPassowrd
Local $sRead = IniRead($sFileName, 'General', 'Setup', 'Default Value')
;~If $sRead = '1' Then Exit MsgBox(0, '配制文件已存在', '如需修改请删除配制文件。', 5)
Local $hGUI = GUICreate('OIT Auto', 356, 186)
GUICtrlCreateLabel('数据库类型', 50, 20, 64, 21)
Local $Datebasetype = GUICtrlCreateCombo('', 120, 16, 217, 21, 3)
GUICtrlSetData(-1, 'ACCESS|MSSQL', 'ACCESS')
GUICtrlCreateLabel('数据库名称或路径', 15, 68, 100, 21)
Local $File_path = GUICtrlCreateInput('', 120, 64, 185, 21)
Local $Select_file = GUICtrlCreateButton('选择', 308, 64, 30, 21)
GUICtrlCreateLabel('MSSQL密码', 57, 108, 65, 17, 0)
Local $PasswordEdit = GUICtrlCreateInput('', 120, 104, 217, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_PASSWORD))
_GUICtrlEdit_SetReadOnly($PasswordEdit, True)
Local $ButtonSave = GUICtrlCreateButton('保存[&S]', 182, 152, 75, 25)
Local $ButtonExit = GUICtrlCreateButton('退出[&C]', 263, 152, 75, 25)
GUISetState()
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $Datebasetype
Switch GUICtrlRead($Datebasetype)
Case 'MSSQL'
_GUICtrlEdit_SetReadOnly($PasswordEdit, False)
Case 'ACCESS'
_GUICtrlEdit_SetReadOnly($PasswordEdit, True)
EndSwitch
Case $Select_file
If GUICtrlRead($Datebasetype) <> 'MSSQL' Then
Local $sFileOpenDialog = FileOpenDialog('请选择 MDB 文件', '', '(*.mdb)', $FD_FILEMUSTEXIST)
If Not @Error Then GUICtrlSetData($File_path, $sFileOpenDialog)
EndIf
Case $ButtonSave
_SaveFile()
Case -3, $ButtonExit
Exit GUIDelete($hGUI)
Case $GUI_EVENT_DROPPED
EndSwitch
WEnd
Func _SaveFile()
$sType = GUICtrlRead($Datebasetype)
$sFile = GUICtrlRead($File_path)
$sPassowrd = GUICtrlRead($PasswordEdit)
IniWrite($sFileName, 'General', 'Setup', '1')
IniWrite($sFileName, 'DataType', 'Type', $sType)
IniWrite($sFileName, 'FilePath', 'Path', $sFile)
IniWrite($sFileName, 'Password_Set', 'Password', $sPassowrd)
EndFunc ;==>_SaveFile
回复 9# afan
版大这样的代码看着真舒心啊。我弄得太乱了,您这个很值得我们初学者学习。
另外,原来还可以用GUIGetMsg来获取几乎所有控件变动的信息啊,又学了一招。谢谢!
页:
[1]