GUI checkboxes to generate text in Edit control

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mwpriest
Posts: 20
Joined: 03 Oct 2022, 07:36

GUI checkboxes to generate text in Edit control

Post by mwpriest » 03 Oct 2022, 08:19

I'm a non-programmer who's been using AHK at its most basic concept (simple text replacement and keyboard actions). I've just discovered the world of GUI and its apparent potential, but still not up on the syntax/logic of it all. The concept I'm trying to build is for a university, when students submit a request that requires 5 documents they may be missing 1-5 of those documents, but there too many different possible outcomes (31? Somebody check my math) to create different outcomes for each. I imagine a GUI in which the user (me) can check a box of the documents which are missing, and then click a button which will generate the text for an email in a GUI Edit control based on the checkboxes that were checked. Here's what I've got so far based on what I could scrounge up from the forums, but it probably isn't very efficient and the text that generates sometimes duplicates if not all boxes are checked:

Code: Select all

Words=
(
Dear student,

Do this:


)

Gui, Add, Text,, Which documents are missing?
Gui, Add, CheckBox, vDoc1, Document 1
Gui, Add, CheckBox, vDoc2, Document 2
Gui, Add, CheckBox, vDoc3, Document 3
Gui, Add, CheckBox, vDoc4, Document 4
Gui, Add, CheckBox, vDoc5, Document 5
Gui, Add, Text,, Email Template:
Gui, Add, Edit, r9 w500 hwndhMyEdit vMyEdit, %Words%
Gui, Add, Button, gEmail, Click here to generate email
Gui, Show
Return

Email:

GuiControlGet, Doc1,, Doc1
if (Doc1 = 1)
   sAppend := "Complete Doc1!`r`n"
   AppendText(hMyEdit, &sAppend)

GuiControlGet, Doc2,, Doc2
if (Doc2 = 1)
   sAppend := "Complete Doc2!`r`n"
   AppendText(hMyEdit, &sAppend)

GuiControlGet, Doc3,, Doc3
if (Doc3 = 1)
   sAppend := "Complete Doc3!`r`n"
   AppendText(hMyEdit, &sAppend)

GuiControlGet, Doc4,, Doc4
if (Doc4 = 1)
   sAppend := "Complete Doc4!`r`n"
   AppendText(hMyEdit, &sAppend)

GuiControlGet, Doc5,, Doc5
if (Doc5 = 1)
   sAppend := "Complete Doc5!`r`n"
   AppendText(hMyEdit, &sAppend)
Return

GuiEscape:
GuiClose:
ExitApp

AppendText(hEdit, ptrText) {
   SendMessage, 0x000E, 0, 0,, ahk_id %hEdit%
   SendMessage, 0x00B1, ErrorLevel, ErrorLevel,, ahk_id %hEdit%
   SendMessage, 0x00C2, False, ptrText,, ahk_id %hEdit%
}
Last edited by joedf on 03 Oct 2022, 08:44, edited 1 time in total.
Reason: [code] tags

RussF
Posts: 1260
Joined: 05 Aug 2021, 06:36

Re: GUI checkboxes to generate text in Edit control

Post by RussF » 03 Oct 2022, 08:51

I didn't have time to analyze your entire code, but the first thing that pops out is that by default, an IF test only execute one statement following it unless you use curly braces around the code you want executed.

For example:

Code: Select all

if (Doc3 = 1)
   sAppend := "Complete Doc3!`r`n"
   AppendText(hMyEdit, &sAppend)
should be:

Code: Select all

if (Doc3 = 1) {
   sAppend := "Complete Doc3!`r`n"
   AppendText(hMyEdit, &sAppend)
   }
There may be other issues, but I will have to defer to another due to time constraints.

Russ

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: GUI checkboxes to generate text in Edit control

Post by mikeyww » 03 Oct 2022, 09:01

More ideas are below.

Code: Select all

doc := ["Doc1", "Doc2", "Doc3", "Doc4", "Doc5"]
Gui, Font, s10
Gui, Add, Text    ,, Which documents are missing?
For k, document in doc
 Gui, Add, CheckBox, w500 vd%k% gUpdate, %document%
Gui, Add, Text    ,  wp   y+20         , Email Template:
Gui, Add, Edit    ,  wp   vtext r10
Gui, Add, Button  ,  wp                , Copy
Gui, Show,, E-mail
Return

Update:
Gui, Submit, NoHide
text = Missing:`n
For k, document in doc
 text .= d%k% ? "`n" document : ""
text .= "`n`nFix it!"
GuiControl,, text, %text%
Return

ButtonCopy:
Gui, Submit
Clipboard := "", Clipboard := text
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
MsgBox, 64, Clipboard, %Clipboard%
Return

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: GUI checkboxes to generate text in Edit control

Post by flyingDman » 03 Oct 2022, 15:12

See here: viewtopic.php?f=76&t=108026&p=481257&hilit=passport#p481236
This not only allows you to use the check marks, it also formats the email body in HTML and sends the email when done (requires Outlook).
Adapted to your circumstance, something like this:

Code: Select all

arr := {1:["Doc1","Document 1"], 2:["Doc2","Document 2"], 3:["Doc3","Document 3"], 4:["Doc4","Document 4"], 5:["Doc5","Document 5"]}
gui, font, s12
gui, margin, 0, 0
for x,y in arr
	Gui, add, Checkbox, % "y+5 x3 vdoc" x " ggenHTML", % y.1
Gui, Add, ActiveX, vWB x220 y3 w600 h500, HTMLfile
Gui, add, button, x700 y+3 gGenEmail, Generate eMail
Gui, Show
style 	:= "<html><head><style>body, p {font-family: 'calibri'; font-size: '14pt'; overflow-y: 'auto';}"
style 	.= "dl {margin-left: '90px'; font-family: 'calibri'; font-size: '13pt';} </style></head>"
subject := "Your request"
grtng 	:= "<body><p>Thank you for your request. Your submission did not include the following required documents:"
slttn 	:= "The deadline for submission of these documents is October 3rd, 2022.<br><br>Regards,<br>Name<br>Title"
genHTML:
gui, submit, nohide
WB.close()
list 	:= ""
for x,y in arr
	list 	.=  doc%x% ? "<li>" y.2 "</li>" : ""
html 	:= style grtng  intro  "<h4><ul>" list " </ul></h4> " slttn "</body></html>"
WB.write(html)
return
GenEmail:
Ol := 					ComObjCreate("Outlook.Application").CreateItem(0)
Ol.BodyFormat := 		2 												; HTML
Ol.To := 				"[email protected]"
Ol.Subject := 			subject
Ol.HTMLBody := 			html
Ol.display
exitapp
14.3 & 1.3.7

mwpriest
Posts: 20
Joined: 03 Oct 2022, 07:36

Re: GUI checkboxes to generate text in Edit control

Post by mwpriest » 05 Oct 2022, 15:31

Every person that responded taught me something new, so I appreciate that! Thanks so much for the assistance, and I'll be in touch with any questions.

mwpriest
Posts: 20
Joined: 03 Oct 2022, 07:36

Re: GUI checkboxes to generate text in Edit control

Post by mwpriest » 06 Oct 2022, 13:36

I modified my original script, and it more closely aligns with mikeyww's version. It's not as efficient as mikeyww's script, but I opted to add the checkboxes separately so that it would be easier to add more later, and especially because I'm having it add 1-2 sentences when each checkmarks is checked and this seemed like the easier approach. Also, I have it automatically copying the text to the clipboard, but still providing a copy button in case I want to add text based on unique cases and still be able to copy the text. I'll post my current script below, but I'm wondering if there is a way to format the text in an Edit Control? I'd love to put bullet points, hyperlinks, and bolding, if possible. Thanks!

Code: Select all

Gui, Font, s10
Gui, Add , Text    ,, Which documents are missing?
Gui, Add , CheckBox, vSRC  gUpdate, Student Responsibility Checklist
Gui, Add , CheckBox, vI20  gUpdate, I-20
Gui, Add , CheckBox, vI94  gUpdate, I-94
Gui, Add , CheckBox, vVisa gUpdate, Visa
Gui, Add , CheckBox, vPPT  gUpdate, Passport
Gui, Add , CheckBox, vDep  gUpdate, Dependent Docs
Gui, Add , Text    , wp y+20 , Email Template:
Gui, Add , Edit    , w500 vtext r10
Gui, Add , Button  , wp, Copy
Gui, Show,         , E-mail
Return

Update:
Gui, Submit, NoHide
text = Hello student,`n`nI have reviewed your check in submission. Before I can accept your check in documents, please address the following:`n

text .= SRC ? "`nStudent Responsibility Checklist (SRC) – filled out during your CANVAS check-in course" document : ""
text .= I20 ? "`nI-20 page 1 (or DS-2019 page 1) – please physically sign at the bottom of page 1 in ink before scanning if you have not already done that" document : ""
text .= I94 ? "`nYour most recent I-94 (Go to the CBP website AFTER you arrive in the U.S. and choose Most Recent I-94, not Travel History" document : ""
text .= Visa ? "`nYour US visa showing F-1 or J-1 classification" document : ""
text .= PPT ? "`nYour passport picture page showing the expiration date of your passport" document : ""
text .= Dep ? "`nIf you have F-2 or J-2 dependents (spouse and/or children) who traveled with you to the US, scan their documents too and include them in the PDF that you submit." document : ""

text .= "`n`nPlease make sure to upload ALL FIVE required documents again and click SUBMIT. Your request is currently noted as Clarification Requested in MPassport and is available for you to edit. You can access this request from the Requests tab under the heading Pending Requests. Please do not begin a brand new request.`n`nTake Care,`n`nOfficer Name`nInternational Center"
GuiControl,, text, %text%
Clipboard := text
Return

ButtonCopy:
Gui, Submit, NoHide
Clipboard := text
Return

[Mod edit: Added [code][/code] tags. Please use them yourself when posting code.]

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: GUI checkboxes to generate text in Edit control

Post by flyingDman » 06 Oct 2022, 14:40

mwpriest wrote:
06 Oct 2022, 13:36
but I'm wondering if there is a way to format the text in an Edit Control?
Guess why I did it in HTML?
14.3 & 1.3.7

mwpriest
Posts: 20
Joined: 03 Oct 2022, 07:36

Re: GUI checkboxes to generate text in Edit control

Post by mwpriest » 06 Oct 2022, 15:22

flyingDman: Are you saying that it's not possible to format using Edit Control? I liked your script and the version you provided worked great, but when I tried to modify it to fit my needs I wasn't successful. We don't use Outlook, so I'm really just looking to take the text in the Edit or ActiveX box and copy it into our email ticketing client. Here was my unsuccessful (and probably to you, laughable) attempt at combining your idea with mikeyww's script and my original script (it initially displays fine, but the checkboxes aren't working):

Code: Select all

Gui, Font, s10
Gui, Add , Text ,, Which documents are missing?
Gui, Add , CheckBox, vSRC gUpdate, Student Responsibility Checklist
Gui, Add , CheckBox, vI20 gUpdate, I-20
Gui, Add , CheckBox, vI94 gUpdate, I-94
Gui, Add , CheckBox, vVisa gUpdate, Visa
Gui, Add , CheckBox, vPPT gUpdate, Passport
Gui, Add , CheckBox, vDep gUpdate, Dependent Docs
Gui, Add , Text , wp y+20 , Email Template:
Gui, Add , ActiveX , vtext x220 y3 w500 h500, HTMLfile
Gui, Add , Button , wp, Copy
Gui, Show
style := "<html><head><style>body, p {font-family: 'calibri'; font-size: '14pt'; overflow-y: 'auto';}"
style .= "dl {margin-left: '90px'; font-family: 'calibri'; font-size: '13pt';} </style></head>"
grtng := "<body><p>Hello student,<br><br>I have reviewed your check in submission. Before I can accept your check in documents, please address the following:<br>"
slttn := "<br><br>Please make sure to upload ALL FIVE required documents again and click SUBMIT. Your request is currently noted as Clarification Requested in MPassport and is available for you to edit. You can access this request from the Requests tab under the heading Pending Requests. Please do not begin a brand new request.<br><br>Take Care,<br><br>Officer Name<br>International Center"
Return

Update:
Gui, Submit, NoHide
text.close()

text .= SRC ? "<br><li>Student Responsibility Checklist (SRC) – filled out during your CANVAS check-in course</li>" document : ""
text .= I20 ? "<br><li>I-20 page 1 (or DS-2019 page 1) – please physically sign at the bottom of page 1 in ink before scanning if you have not already done that</li>" document : ""
text .= I94 ? "<br><li>Your most recent I-94 (Go to the CBP website AFTER you arrive in the U.S. and choose Most Recent I-94, not Travel History</li>" document : ""
text .= Visa ? "<br><li>Your US visa showing F-1 or J-1 classification</li>" document : ""
text .= PPT ? "<br><li>Your passport picture page showing the expiration date of your passport</li>" document : ""
text .= Dep ? "<br><li>If you have F-2 or J-2 dependents (spouse and/or children) who traveled with you to the US, scan their documents too and include them in the PDF that you submit.</li>" document : ""
html := style grtng intro "<h4><ul>" text " </ul></h4> " slttn "</body></html>"
text.write(html)
Clipboard := text
Return

ButtonCopy:
Gui, Submit, NoHide
Clipboard := text
Return

[Mod edit: Added [code][/code] tags. Please use them yourself when posting code.]

User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: GUI checkboxes to generate text in Edit control

Post by boiler » 06 Oct 2022, 15:37

@mwpriest - Please add code tags when posting code using the </> button above the reply edit box. Select your code then press the button to apply the tags around it. Thank you.

mwpriest
Posts: 20
Joined: 03 Oct 2022, 07:36

Re: GUI checkboxes to generate text in Edit control

Post by mwpriest » 06 Oct 2022, 15:48

boiler wrote: @mwpriest - Please add code tags when posting code using the </> button above the reply edit box. Select your code then press the button to apply the tags around it. Thank you.

Code: Select all

MsgBox You learn something new everyday. Understood!

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: GUI checkboxes to generate text in Edit control

Post by flyingDman » 06 Oct 2022, 16:06

@mwpriest If you're using gmail you can still embed the html (search the forum for gmail CDO). Other email clients, I am not aware. Yes, an editbox is very limited in terms of formatting.
14.3 & 1.3.7

mwpriest
Posts: 20
Joined: 03 Oct 2022, 07:36

Re: GUI checkboxes to generate text in Edit control

Post by mwpriest » 06 Oct 2022, 18:56

flyingDman wrote:
06 Oct 2022, 16:06
@mwpriest If you're using gmail you can still embed the html (search the forum for gmail CDO). Other email clients, I am not aware. Yes, an editbox is very limited in terms of formatting.
Nope, not using gmail either. It's basically a student portal through which we can send emails. Is there a way to format in HTML and then instead of having it go through an email client it can just copy the formatted text?

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: GUI checkboxes to generate text in Edit control

Post by flyingDman » 06 Oct 2022, 20:10

I am not aware of it but others might know. Can you write HTML code in that box?
14.3 & 1.3.7

docfizzle
Posts: 37
Joined: 08 Sep 2022, 21:09

Re: GUI checkboxes to generate text in Edit control

Post by docfizzle » 09 Oct 2022, 16:18

....
Last edited by docfizzle on 09 Oct 2022, 16:50, edited 1 time in total.

docfizzle
Posts: 37
Joined: 08 Sep 2022, 21:09

Re: GUI checkboxes to generate text in Edit control

Post by docfizzle » 09 Oct 2022, 16:49

Awesome tips here! Thank you!

I originally asked a question here and realized that I did not want to hijack the thread.

So....
I moved my tangential question here: viewtopic.php?style=1&f=76&t=109269

Post Reply

Return to “Ask for Help (v1)”