Sign in to follow this  
Followers 0
paul1149

Two basic MsgBox / InputBox questions

14 posts in this topic

Hi, Brand new here but an old AutoIt lover.

I'm finishing a script which formats Wordpress posts for publication. The last step is to provide a means of escape for if I realize I'm not yet ready to run the script.

Is there any way to enable the Cancel button to actually do something, without going the longhand GUI route? Currently I'm simply writing MsgBox(flag, "title", "text")

Secondly, it would help a lot if I could drag and drop text to an input box, but the ones I specify are impervious to doing so.

Thanks much. I see the old yahoo group has gone to seed, so I appreciate this forum.

bb,

Paul

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Welcome to the forums :D

Msgbox returns the ID of the button pressed

You can do

$pressed=Msgbox(1, "Dilema", "OK or Cancel - the choice is yours")
if $pressed==2 Then Msgbox(0, "yey", 'yey you pressed "Cancel"')

It is written in the helpfile...

Edited by muhmuuh

I ran. I ran until my muscles burned and my veins pumped battery acid. Then I ran some more.

Share this post


Link to post
Share on other sites

Welcome to the forums :D

Msgbox returns the ID of the button pressed

You can do

$pressed=Msgbox(1, "Dilema", "OK or Cancel - the choice is yours")
if $pressed==2 Then Msgbox(0, "yey", 'yey you pressed "Cancel"')

It is written in the helpfile...

Thanks for the welcome, and the answer. I did look in Help but came up empty. I'll put this to use and get back on it.

Thanks much,

Paul

Share this post


Link to post
Share on other sites

I know you said you'd rather not take the GUI approach, but here's a GUI anyway.

Next poster will probably have a super easy way to make inputbox accept drag and drop. 8)

#include <GUIConstantsEx.au3>

$input = _InputBox("Drag and drop example", "Try drag and drop!")
If Not @error Then
    MsgBox(4096, "drag drop file", $input )
EndIf


Func _InputBox($sTitle, $sPrompt)
    Local $file, $btnOk, $btnCancel, $msg, $ret
        
    GUICreate($sTitle, 320, 120, @DesktopWidth / 2 - 160, @DesktopHeight / 2 - 45, -1, 0x00000018); WS_EX_ACCEPTFILES
    GUICtrlCreateLabel($sPrompt, 10, 10)
    $file = GUICtrlCreateInput("", 10, 50, 300, 21)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    $btnOk = GUICtrlCreateButton("Ok", 100, 80, 100, 25)
    $btnCancel = GUICtrlCreateButton("Cancel", 210, 80, 100, 25)

    GUISetState()

    $msg = 0
    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Return SetError(1,0,0)
            Case $msg = $btnOk
                ExitLoop
            Case $msg = $btnCancel
                Return SetError(1,0,0)
        EndSelect
    WEnd

    $ret = GUICtrlRead($file)
    If ($ret == "") Then
        Return SetError(1,0,0)
    Else
        Return $ret
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

Welcome to the forums :D

Msgbox returns the ID of the button pressed

You can do

$pressed=Msgbox(1, "Dilema", "OK or Cancel - the choice is yours")
if $pressed==2 Then Msgbox(0, "yey", 'yey you pressed "Cancel"')

It is written in the helpfile...

muhmuuh, I have half success. On the msgboxes, this is working:

$pressed = MsgBox( 305, "Press'ENTER' to cancel!", "Paras to be blockquoted must have 'xxx ' prepended. ", 3)

if $pressed==2 Then Exit

But on the inputboxes, this doesn't work:

$URL = InputBox ("Add Social Media Linkbar", "Enter post's URL:", "", "", 400, 100, 0, 400)

if $URL==1 Then Exit

Help says 1 is the error code for the cancel button, whereas 0 is a successful return. But obviously I'm missing a connection of some kind.

Thanks,

Paul

Share this post


Link to post
Share on other sites

You get the last error code with the @error macro.

$URL = InputBox ("Add Social Media Linkbar", "Enter post's URL:", "", "", 400, 100, 0, 400)

If @error == 1 Then Exit

Share this post


Link to post
Share on other sites

I know you said you'd rather not take the GUI approach, but here's a GUI anyway.

Next poster will probably have a super easy way to make inputbox accept drag and drop. 8)

#include <GUIConstantsEx.au3>

$input = _InputBox("Drag and drop example", "Try drag and drop!")
If Not @error Then
    MsgBox(4096, "drag drop file", $input )
EndIf


Func _InputBox($sTitle, $sPrompt)
    Local $file, $btnOk, $btnCancel, $msg, $ret
        
    GUICreate($sTitle, 320, 120, @DesktopWidth / 2 - 160, @DesktopHeight / 2 - 45, -1, 0x00000018); WS_EX_ACCEPTFILES
    GUICtrlCreateLabel($sPrompt, 10, 10)
    $file = GUICtrlCreateInput("", 10, 50, 300, 21)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    $btnOk = GUICtrlCreateButton("Ok", 100, 80, 100, 25)
    $btnCancel = GUICtrlCreateButton("Cancel", 210, 80, 100, 25)

    GUISetState()

    $msg = 0
    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Return SetError(1,0,0)
            Case $msg = $btnOk
                ExitLoop
            Case $msg = $btnCancel
                Return SetError(1,0,0)
        EndSelect
    WEnd

    $ret = GUICtrlRead($file)
    If ($ret == "") Then
        Return SetError(1,0,0)
    Else
        Return $ret
    EndIf
EndFunc

Thanks, manadar. Looks like good clean code, and I'm going to hold onto it for future reference.

Thanks!

Paul

Share this post


Link to post
Share on other sites

You get the last error code with the @error macro.

$URL = InputBox ("Add Social Media Linkbar", "Enter post's URL:", "", "", 400, 100, 0, 400)

If @error == 1 Then Exit

Perfect. I'm not sure why an error spec is needed for the inputbox but not for the msgbox, but obviously that's the case!

I also don't know why there's a lack of d&d support for these boxes. Probably a Windows issue, I'm suspect.

Thanks guys for your great help. I'm up and running!

Paul

Share this post


Link to post
Share on other sites

Perfect. I'm not sure why an error spec is needed for the inputbox but not for the msgbox, but obviously that's the case!

That's a simple question to answer.

Consider the following:

$URL = InputBox ("Add Social Media Linkbar", "Enter post's URL:", "", "", 400, 100, 0, 400)
if $URL==1 Then Exit

If I enter the number 1 into the InputBox, the application would exit because it thinks I pressed cancel. It's probably the same with MsgBox. (Haven't checked.) In other functions where @error really isn't needed it is used for consistency.

In fact, AutoIt does not need @error at all. InputBox could just return an array with 2 dimensions. The first dimension would contain the error flag, and the second one the result string (or "" in case of error.) Same with other functions.

Share this post


Link to post
Share on other sites

That's a simple question to answer.

Consider the following:

$URL = InputBox ("Add Social Media Linkbar", "Enter post's URL:", "", "", 400, 100, 0, 400)
if $URL==1 Then Exit

If I enter the number 1 into the InputBox, the application would exit because it thinks I pressed cancel. It's probably the same with MsgBox. (Haven't checked.) In other functions where @error really isn't needed it is used for consistency.

In fact, AutoIt does not need @error at all. InputBox could just return an array with 2 dimensions. The first dimension would contain the error flag, and the second one the result string (or "" in case of error.) Same with other functions.

Yes, that example makes sense. The rest of it I'm not totally up to, but get your drift.

Thanks again,

Paul

Share this post


Link to post
Share on other sites

In fact, AutoIt does not need @error at all. InputBox could just return an array with 2 dimensions. The first dimension would contain the error flag, and the second one the result string (or "" in case of error.) Same with other functions.

It may not need it, but then it becomes a pain because then you need to deal with arrays when you want to do something with the input from InputBox. And for a quick script for personal use, it may be allowable to not deal with error handling.

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Too much six-pack abs... more like:

Posted Image

:D

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0