How to set Dialog title and Editbox at runtime

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
Kiet
Newbie
Posts: 2
Joined: Wed Oct 21, 2009 10:43 pm
Location: Australia

How to set Dialog title and Editbox at runtime

Post by Kiet » Fri Oct 30, 2009 6:23 am

Hello Macro colleagues,
I want to perform these 3 tasks at runtime.
1. Change the Dialog title
2. Change the content in an Editbox
3. Have the above text values perserved (not change) when call to "ResetDialogAction>MyDialog"

/* Set the text in the Editbox at runtime works */
SetControlText>Change Window title,TEdit,1,%strTemp%
/* This doesn't work. I can't change the Dialog title */
SetControlText>Change Window title,TForm,1,%strTemp%

/* Windows API: Set the text in the Editbox at runtime works */
LibFunc>user32,SetWindowTextA,lfres,MyDialog.msEdit1.HANDLE,strTemp
/* Windows API: Set the Dialog title at runtime works */
LibFunc>user32,SetWindowTextA,lfres,MyDialog.HANDLE,strTemp

/* Reset the Dialog response. However it also reset the Editbox and Dialog title changes. */
ResetDialogAction>MyDialog

/* My code */
Dialog>MyDialog
Caption=Change Window title
Width=300
Height=185
Top=CENTER
Left=CENTER
Max=0
Min=0
Close=0
Resize=0
Button=Write,12,104,69,25,1
Button=Cancel,104,104,81,25,2
Edit=msEdit1,79,38,121,msEdit1
Button=Read,206,105,75,25,3
EndDialog>MyDialog

Show>MyDialog

Label>Loop
Wait>0.1
GetDialogAction>MyDialog,res1
If>res1=2
CloseDialog>MyDialog
Exit>0
EndIF

If>res1=1
Let>strTemp=Write Hello World
SetControlText>Change Window title,TEdit,1,%strTemp%
/*
SetControlText>Change Window title,TForm,1,%strTemp%
*/
/*
LibFunc>user32,SetWindowTextA,lfres,MyDialog.msEdit1.HANDLE,strTemp
LibFunc>user32,SetWindowTextA,lfres,MyDialog.HANDLE,strTemp
*/
MessageModal>Write msEdit1=%MyDialog.msEdit1%
ResetDialogAction>MyDialog
EndIf

If>res1=3
MessageModal>Read msEdit1=%MyDialog.msEdit1%
ResetDialogAction>MyDialog
EndIf
Goto>Loop

Thanks
Kiet

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Oct 30, 2009 1:40 pm

Don't use setcontroltext on your own dialog, its not needed. Set the editbox using Let>MyDialog.msedit1=What Ever then ResetDialogAction>Mydialog

The GetDialogAction> function is reverting the dialog title after you change it so avoid using GetDialogAction> by instead using Dialog_Event OnEvents.

Here's your script cobbled a bit but working, I think as you want it.

Code: Select all

OnEvent>Dialog_Event,MyDialog,1,UpDate
OnEvent>Dialog_Event,MyDialog,2,Close
OnEvent>Dialog_Event,MyDialog,3,Read

SRT>UpDate
  Let>strTemp=Write Hello World
  //SetControlText>Change Window title,TEdit,1,%strTemp%
  Let>MyDialog.msedit1=strTemp
  /*
  SetControlText>Change Window title,TForm,1,%strTemp%
  */
  //LibFunc>user32,SetWindowTextA,lfres,MyDialog.msEdit1.HANDLE,strTemp
  LibFunc>user32,SetWindowTextA,lfres,MyDialog.HANDLE,strTemp
  ResetDialogAction>MyDialog
  MessageModal>Write msEdit1=%MyDialog.msEdit1%
END>UpDate

SRT>Close
  Exit>0
END>Close

SRT>Read
  MessageModal>Read msEdit1=%MyDialog.msEdit1%
  ResetDialogAction>MyDialog
END>Read

Dialog>MyDialog
  Caption=Change Window title
  Width=300
  Height=185
  Top=0
  Left=CENTER
  Max=0
  Min=0
  Close=0
  Resize=0
  Button=Write,12,104,69,25,1
  Button=Cancel,104,104,81,25,2
  Edit=msEdit1,79,38,121,msEdit1
  Button=Read,206,105,75,25,3
EndDialog>MyDialog

Show>MyDialog

Label>Loop
  Wait>0.1
  //GetDialogAction>MyDialog,res1
/*
  If>res1=2
    CloseDialog>MyDialog
    Exit>0
  EndIF
*/

/*
  If>res1=1
    Let>strTemp=Write Hello World
    //SetControlText>Change Window title,TEdit,1,%strTemp%
    Let>MyDialog.msedit1=strTemp
  /*
  SetControlText>Change Window title,TForm,1,%strTemp%
  */
    /*
LibFunc>user32,SetWindowTextA,lfres,MyDialog.msEdit1.HANDLE,strTemp
    LibFunc>user32,SetWindowTextA,lfres,MyDialog.HANDLE,strTemp

  
    MessageModal>Write msEdit1=%MyDialog.msEdit1%
    ResetDialogAction>MyDialog
  EndIf
*/

/*
  If>res1=3
    MessageModal>Read msEdit1=%MyDialog.msEdit1%
    ResetDialogAction>MyDialog
  EndIf
*/
Goto>Loop
Here's your script cleaned up:

Code: Select all

OnEvent>Dialog_Event,MyDialog,1,UpDate
OnEvent>Dialog_Event,MyDialog,2,Close
OnEvent>Dialog_Event,MyDialog,3,Read

SRT>UpDate
  Let>strTemp=Write Hello World
  Let>MyDialog.msedit1=strTemp
  LibFunc>user32,SetWindowTextA,lfres,MyDialog.HANDLE,strTemp
  ResetDialogAction>MyDialog
  MessageModal>Write msEdit1=%MyDialog.msEdit1%
END>UpDate

SRT>Close
  Exit>0
END>Close

SRT>Read
  MessageModal>Read msEdit1=%MyDialog.msEdit1%
  ResetDialogAction>MyDialog
END>Read

Dialog>MyDialog
  Caption=Change Window title
  Width=300
  Height=185
  Top=0
  Left=CENTER
  Max=0
  Min=0
  Close=0
  Resize=0
  Button=Write,12,104,69,25,1
  Button=Cancel,104,104,81,25,2
  Edit=msEdit1,79,38,121,msEdit1
  Button=Read,206,105,75,25,3
EndDialog>MyDialog

Show>MyDialog

Label>Loop
  Wait>0.01
Goto>Loop

Kiet
Newbie
Posts: 2
Joined: Wed Oct 21, 2009 10:43 pm
Location: Australia

Post by Kiet » Mon Nov 02, 2009 12:34 am

A big Thank You JRL for your help.
Just one last question. For some reason I can't read the Editbox value by referencing MyDialog.msEdit1. I had to use the Windows API GetWindowTextA(). Is this the proper solution in Macro Scripting?

LibFunc>user32,GetWindowTextLengthA,wtlen,MyDialog.msEdit1.HANDLE
Let>buffer_SIZE=wtlen
Let>wtlen=wtlen+1
LibFunc>user32,GetWindowTextA,lfres,MyDialog.msEdit1.HANDLE,buffer,wtlen
MessageModal>Read msEdit1=%MyDialog.msEdit1% %CRLF% lfres_2=%lfres_2%

Code: Select all

OnEvent>Dialog_Event,MyDialog,1,UpDate
OnEvent>Dialog_Event,MyDialog,2,Close
OnEvent>Dialog_Event,MyDialog,3,Read

SRT>UpDate
  Let>strTemp=Write Hello World
  Let>MyDialog.msedit1=strTemp
  LibFunc>user32,SetWindowTextA,lfres,MyDialog.HANDLE,strTemp
  ResetDialogAction>MyDialog
  MessageModal>Write msEdit1=%MyDialog.msEdit1%
END>UpDate

SRT>Close
  Exit>0
END>Close

SRT>Read
  LibFunc>user32,GetWindowTextLengthA,wtlen,MyDialog.msEdit1.HANDLE
  Let>buffer_SIZE=wtlen
  Let>wtlen=wtlen+1
  LibFunc>user32,GetWindowTextA,lfres,MyDialog.msEdit1.HANDLE,buffer,wtlen

  MessageModal>Read msEdit1=%MyDialog.msEdit1% %CRLF% lfres_2=%lfres_2%
  ResetDialogAction>MyDialog
END>Read

Dialog>MyDialog
  Caption=Change Window title
  Width=300
  Height=185
  Top=CENTER
  Left=CENTER
  Max=0
  Min=0
  Close=0
  Resize=0
  Button=Write,12,104,69,25,1
  Button=Cancel,104,104,81,25,2
  Edit=msEdit1,79,38,121,msEdit1
  Button=Read,206,105,75,25,3
EndDialog>MyDialog

Show>MyDialog

Label>Loop
  Wait>0.01
Goto>Loop

zabros2020
Pro Scripter
Posts: 70
Joined: Sun May 03, 2009 11:49 pm
Location: AU

Post by zabros2020 » Mon Nov 02, 2009 1:48 am

An interesting thing i noticed when running Kiet's code is that

Length>%lfres_2%,length

makes length=255

regardless of what you type in the edit box. Can anyone please explain?

After fiddling around with this for a while i have another question/thought. How would one go about getting data from a combo box. I have tried adding a combo box with options Yes and No, when i check up the initial value of the box, it is always Yes%CRLF%No
I have to physically choose Yes or No, before it changes.
Loving MS's Capabilities!!!

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Nov 02, 2009 3:40 am

Kiet wrote:Just one last question. For some reason I can't read the Editbox value by referencing MyDialog.msEdit1
Oops. that's because in order to keep the dialog title change we turned off GetDialogAction>. As its name implies, that is the function that reassigns all of the dialog variables to the value they've been changed to. If you put GetDialogAction> back in the idle Loop, the variable would contain the test from the edit box. Very clever how you acquired the text via a win API.
zabros2020 wrote:An interesting thing i noticed when running Kiet's code is that

Length>%lfres_2%,length

makes length=255

regardless of what you type in the edit box. Can anyone please explain?
I don't get that result. The "Write Hello World" text reports to be 17 characters. Which I believe is correct.
zabros2020 wrote:How would one go about getting data from a combo box.
I haven't tried it but I suspect its the GetDailgoAction> function having been removed that creating your problem. Add the line back into the loop and see if it makes a difference.

zabros2020
Pro Scripter
Posts: 70
Joined: Sun May 03, 2009 11:49 pm
Location: AU

Post by zabros2020 » Mon Nov 02, 2009 7:23 am

Thanks JRL great work.
I don't quite grasp how ms deals with these things, but i finally have my code working thanks to you

On another note. If you use Kiet's code but omit the lines:
Max=0
Min=0
Close=0
Resize=0

And allow the user to click X, then it never enters the subroutine for close.

Is there a reason for this? I thought macro scheduler allows for X to default to 2 as a modal value
Loving MS's Capabilities!!!

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Nov 02, 2009 3:51 pm

I discovered the same thing a couple of days ago. See THIS THREAD.

zabros2020
Pro Scripter
Posts: 70
Joined: Sun May 03, 2009 11:49 pm
Location: AU

Post by zabros2020 » Fri Nov 06, 2009 6:04 am

Thanks so much, makes more sense. I think now we will just disable the X button and go on from there.

JRL, thank you for all your help.
Loving MS's Capabilities!!!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts