vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
//! # `MsgBox` Function
//!
//! Displays a message in a dialog box, waits for the user to click a button, and returns an Integer indicating which button the user clicked.
//!
//! ## Syntax
//!
//! ```vb
//! MsgBox(prompt, [buttons], [title], [helpfile], [context])
//! ```
//!
//! ## Parameters
//!
//! - **prompt** (Required) - String expression displayed as the message in the dialog box. Maximum length is approximately 1024 characters, depending on the width of the characters used. If prompt consists of more than one line, you can separate the lines using a carriage return character (Chr(13)), a linefeed character (Chr(10)), or a carriage return-linefeed character combination (vbCrLf) between each line.
//! - **buttons** (Optional) - Numeric expression that is the sum of values specifying the number and type of buttons to display, the icon style to use, the identity of the default button, and the modality of the message box. If omitted, the default value for buttons is 0.
//! - **title** (Optional) - String expression displayed in the title bar of the dialog box. If you omit title, the application name is placed in the title bar.
//! - **helpfile** (Optional) - String expression that identifies the Help file to use to provide context-sensitive Help for the dialog box. If helpfile is provided, context must also be provided.
//! - **context** (Optional) - Numeric expression that is the Help context number assigned to the appropriate Help topic by the Help author. If context is provided, helpfile must also be provided.
//!
//! ## Return Value
//!
//! Returns an **Integer** representing which button was clicked:
//! - **vbOK (1)** - OK button was clicked
//! - **vbCancel (2)** - Cancel button was clicked
//! - **vbAbort (3)** - Abort button was clicked
//! - **vbRetry (4)** - Retry button was clicked
//! - **vbIgnore (5)** - Ignore button was clicked
//! - **vbYes (6)** - Yes button was clicked
//! - **vbNo (7)** - No button was clicked
//!
//! ## Remarks
//!
//! The `MsgBox` function is one of the most commonly used VB6 functions for user interaction and debugging.
//! It provides a simple way to display messages, warnings, errors, and questions to the user.
//!
//! ### Button Constants (First Group - Buttons):
//! - **vbOKOnly (0)** - Display OK button only (default)
//! - **vbOKCancel (1)** - Display OK and Cancel buttons
//! - **vbAbortRetryIgnore (2)** - Display Abort, Retry, and Ignore buttons
//! - **vbYesNoCancel (3)** - Display Yes, No, and Cancel buttons
//! - **vbYesNo (4)** - Display Yes and No buttons
//! - **vbRetryCancel (5)** - Display Retry and Cancel buttons
//!
//! ### Icon Constants (Second Group - Icons):
//! - **vbCritical (16)** - Display Critical Message icon (red X)
//! - **vbQuestion (32)** - Display Warning Query icon (question mark)
//! - **vbExclamation (48)** - Display Warning Message icon (exclamation point)
//! - **vbInformation (64)** - Display Information Message icon (lowercase i)
//!
//! ### Default Button Constants (Third Group - Default Button):
//! - **vbDefaultButton1 (0)** - First button is default (default)
//! - **vbDefaultButton2 (256)** - Second button is default
//! - **vbDefaultButton3 (512)** - Third button is default
//! - **vbDefaultButton4 (768)** - Fourth button is default
//!
//! ### Modality Constants (Fourth Group - Modality):
//! - **vbApplicationModal (0)** - Application modal; the user must respond to the message box before continuing work in the current application (default)
//! - **vbSystemModal (4096)** - System modal; all applications are suspended until the user responds to the message box
//!
//! ### Other Constants (Fifth Group - Other):
//! - **vbMsgBoxHelpButton (16384)** - Adds Help button to the message box
//! - **vbMsgBoxSetForeground (65536)** - Specifies the message box window as the foreground window
//! - **vbMsgBoxRight (524288)** - Text is right-aligned
//! - **vbMsgBoxRtlReading (1048576)** - Specifies text should appear as right-to-left reading on Hebrew and Arabic systems
//!
//! ### Key Characteristics:
//! - Blocks execution until user responds (modal dialog)
//! - Return value can be ignored if not needed
//! - Can combine button constants using addition or Or operator
//! - Maximum prompt length is approximately 1024 characters
//! - Can display multi-line messages using vbCrLf
//! - If user presses Escape key, acts as clicking Cancel button
//! - Can invoke help file if helpfile and context parameters provided
//! - Common for debugging with Debug.Print alternative
//!
//! ### Common Use Cases:
//! - Display informational messages to users
//! - Show error messages and warnings
//! - Ask yes/no questions for user confirmation
//! - Debugging by displaying variable values
//! - Alert users of important events
//! - Confirm destructive operations
//! - Display results of calculations
//! - Provide feedback on operation completion
//!
//! ## Typical Uses
//!
//! 1. **Simple Messages** - Display information to the user
//! 2. **Error Handling** - Show error messages with appropriate icons
//! 3. **User Confirmation** - Ask yes/no questions before proceeding
//! 4. **Debugging** - Display variable values during development
//! 5. **Operation Feedback** - Inform user of completion or status
//! 6. **Validation Warnings** - Alert user to invalid input
//! 7. **Save Prompts** - Ask user to save changes before closing
//! 8. **Delete Confirmations** - Verify user wants to delete items
//!
//! ## Basic Examples
//!
//! ```vb
//! ' Example 1: Simple message
//! MsgBox "Operation completed successfully!"
//! ```
//!
//! ```vb
//! ' Example 2: Message with title and icon
//! MsgBox "File not found!", vbExclamation, "Error"
//! ```
//!
//! ```vb
//! ' Example 3: Yes/No question
//! Dim result As Integer
//! result = MsgBox("Do you want to save changes?", vbYesNo + vbQuestion, "Confirm")
//! If result = vbYes Then
//!     ' Save changes
//! End If
//! ```
//!
//! ```vb
//! ' Example 4: Multi-line message
//! MsgBox "Line 1" & vbCrLf & "Line 2" & vbCrLf & "Line 3", vbInformation, "Multi-line"
//! ```
//!
//! ## Common Patterns
//!
//! ```vb
//! ' Pattern 1: Simple error message
//! Sub ShowError(message As String)
//!     MsgBox message, vbCritical, "Error"
//! End Sub
//! ```
//!
//! ```vb
//! ' Pattern 2: Confirmation with default No
//! Function ConfirmAction(message As String) As Boolean
//!     Dim result As Integer
//!     result = MsgBox(message, vbYesNo + vbQuestion + vbDefaultButton2, "Confirm")
//!     ConfirmAction = (result = vbYes)
//! End Function
//! ```
//!
//! ```vb
//! ' Pattern 3: Save prompt
//! Function PromptToSave(fileName As String) As Integer
//!     Dim msg As String
//!     msg = "Save changes to " & fileName & "?"
//!     PromptToSave = MsgBox(msg, vbYesNoCancel + vbQuestion, "Save Changes")
//! End Function
//! ```
//!
//! ```vb
//! ' Pattern 4: Display detailed error
//! Sub ShowDetailedError(errorMsg As String, errorNumber As Long)
//!     Dim msg As String
//!     msg = "Error #" & errorNumber & vbCrLf & vbCrLf & errorMsg
//!     MsgBox msg, vbCritical + vbOKOnly, "Application Error"
//! End Sub
//! ```
//!
//! ```vb
//! ' Pattern 5: Information with sound
//! Sub ShowInfo(message As String)
//!     MsgBox message, vbInformation + vbOKOnly, "Information"
//!     ' vbInformation plays the system information sound
//! End Sub
//! ```
//!
//! ```vb
//! ' Pattern 6: Retry/Cancel operation
//! Function RetryOperation(operation As String) As Boolean
//!     Dim result As Integer
//!     Dim msg As String
//!     
//!     msg = "Failed to " & operation & "." & vbCrLf & "Would you like to retry?"
//!     result = MsgBox(msg, vbRetryCancel + vbExclamation, "Operation Failed")
//!     RetryOperation = (result = vbRetry)
//! End Function
//! ```
//!
//! ```vb
//! ' Pattern 7: Debug variable display
//! Sub DebugShow(variableName As String, value As Variant)
//!     #If DEBUG_MODE Then
//!         MsgBox variableName & " = " & CStr(value), vbInformation, "Debug"
//!     #End If
//! End Sub
//! ```
//!
//! ```vb
//! ' Pattern 8: Abort/Retry/Ignore pattern
//! Function HandleError(errorMsg As String) As Integer
//!     Dim msg As String
//!     msg = "An error occurred:" & vbCrLf & vbCrLf & errorMsg & vbCrLf & vbCrLf & _
//!           "Abort: Stop operation" & vbCrLf & _
//!           "Retry: Try again" & vbCrLf & _
//!           "Ignore: Continue anyway"
//!     HandleError = MsgBox(msg, vbAbortRetryIgnore + vbCritical, "Error")
//! End Function
//! ```
//!
//! ```vb
//! ' Pattern 9: Formatted message with details
//! Sub ShowOperationResult(operation As String, recordCount As Long, elapsedTime As Double)
//!     Dim msg As String
//!     msg = "Operation: " & operation & vbCrLf & _
//!           "Records processed: " & recordCount & vbCrLf & _
//!           "Time elapsed: " & Format(elapsedTime, "0.00") & " seconds"
//!     MsgBox msg, vbInformation, "Operation Complete"
//! End Sub
//! ```
//!
//! ```vb
//! ' Pattern 10: Custom button selection handler
//! Sub ProcessUserChoice(prompt As String)
//!     Dim result As Integer
//!     result = MsgBox(prompt, vbYesNoCancel + vbQuestion, "Choose Option")
//!     
//!     Select Case result
//!         Case vbYes
//!             ' Handle Yes
//!         Case vbNo
//!             ' Handle No
//!         Case vbCancel
//!             ' Handle Cancel
//!     End Select
//! End Sub
//! ```
//!
//! ## Advanced Usage
//!
//! ### Example 1: Smart Message Box Wrapper
//!
//! ```vb
//! ' Module: SmartMessageBox
//! ' Provides enhanced message box functionality with logging and customization
//!
//! Option Explicit
//!
//! Private m_logEnabled As Boolean
//! Private m_defaultTitle As String
//!
//! Public Sub Initialize(appName As String, enableLogging As Boolean)
//!     m_defaultTitle = appName
//!     m_logEnabled = enableLogging
//! End Sub
//!
//! Public Function ShowMessage(message As String, _
//!                            Optional msgType As VbMsgBoxStyle = vbInformation, _
//!                            Optional title As String = "") As Integer
//!     Dim actualTitle As String
//!     
//!     If Len(title) = 0 Then
//!         actualTitle = m_defaultTitle
//!     Else
//!         actualTitle = title
//!     End If
//!     
//!     If m_logEnabled Then
//!         LogMessage message, msgType
//!     End If
//!     
//!     ShowMessage = MsgBox(message, msgType, actualTitle)
//! End Function
//!
//! Public Function Confirm(message As String, _
//!                        Optional defaultToNo As Boolean = False) As Boolean
//!     Dim buttons As VbMsgBoxStyle
//!     Dim result As Integer
//!     
//!     buttons = vbYesNo + vbQuestion
//!     
//!     If defaultToNo Then
//!         buttons = buttons + vbDefaultButton2
//!     End If
//!     
//!     result = MsgBox(message, buttons, m_defaultTitle)
//!     Confirm = (result = vbYes)
//! End Function
//!
//! Public Sub ShowError(message As String, Optional errorNumber As Long = 0)
//!     Dim msg As String
//!     
//!     If errorNumber <> 0 Then
//!         msg = "Error #" & errorNumber & vbCrLf & vbCrLf & message
//!     Else
//!         msg = message
//!     End If
//!     
//!     If m_logEnabled Then
//!         LogMessage "ERROR: " & msg, vbCritical
//!     End If
//!     
//!     MsgBox msg, vbCritical, m_defaultTitle
//! End Sub
//!
//! Public Sub ShowWarning(message As String)
//!     If m_logEnabled Then
//!         LogMessage "WARNING: " & message, vbExclamation
//!     End If
//!     
//!     MsgBox message, vbExclamation, m_defaultTitle
//! End Sub
//!
//! Public Sub ShowInfo(message As String)
//!     If m_logEnabled Then
//!         LogMessage "INFO: " & message, vbInformation
//!     End If
//!     
//!     MsgBox message, vbInformation, m_defaultTitle
//! End Sub
//!
//! Private Sub LogMessage(message As String, msgType As VbMsgBoxStyle)
//!     ' Log to file or debug window
//!     Debug.Print Now & " - " & GetMessageTypeString(msgType) & ": " & message
//! End Sub
//!
//! Private Function GetMessageTypeString(msgType As VbMsgBoxStyle) As String
//!     If (msgType And vbCritical) = vbCritical Then
//!         GetMessageTypeString = "ERROR"
//!     ElseIf (msgType And vbExclamation) = vbExclamation Then
//!         GetMessageTypeString = "WARNING"
//!     ElseIf (msgType And vbQuestion) = vbQuestion Then
//!         GetMessageTypeString = "QUESTION"
//!     Else
//!         GetMessageTypeString = "INFO"
//!     End If
//! End Function
//! ```
//!
//! ### Example 2: Message Box Builder Class
//!
//! ```vb
//! ' Class: MessageBoxBuilder
//! ' Fluent interface for building complex message boxes
//!
//! Option Explicit
//!
//! Private m_prompt As String
//! Private m_title As String
//! Private m_buttons As VbMsgBoxStyle
//! Private m_icon As VbMsgBoxStyle
//! Private m_defaultButton As VbMsgBoxStyle
//!
//! Private Sub Class_Initialize()
//!     m_prompt = ""
//!     m_title = ""
//!     m_buttons = vbOKOnly
//!     m_icon = 0
//!     m_defaultButton = vbDefaultButton1
//! End Sub
//!
//! Public Function WithPrompt(prompt As String) As MessageBoxBuilder
//!     m_prompt = prompt
//!     Set WithPrompt = Me
//! End Function
//!
//! Public Function WithTitle(title As String) As MessageBoxBuilder
//!     m_title = title
//!     Set WithTitle = Me
//! End Function
//!
//! Public Function WithButtons(buttons As VbMsgBoxStyle) As MessageBoxBuilder
//!     m_buttons = buttons
//!     Set WithButtons = Me
//! End Function
//!
//! Public Function WithIcon(icon As VbMsgBoxStyle) As MessageBoxBuilder
//!     m_icon = icon
//!     Set WithIcon = Me
//! End Function
//!
//! Public Function WithDefaultButton(defaultButton As VbMsgBoxStyle) As MessageBoxBuilder
//!     m_defaultButton = defaultButton
//!     Set WithDefaultButton = Me
//! End Function
//!
//! Public Function AddLine(text As String) As MessageBoxBuilder
//!     If Len(m_prompt) > 0 Then
//!         m_prompt = m_prompt & vbCrLf
//!     End If
//!     m_prompt = m_prompt & text
//!     Set AddLine = Me
//! End Function
//!
//! Public Function Show() As Integer
//!     Dim style As VbMsgBoxStyle
//!     style = m_buttons Or m_icon Or m_defaultButton
//!     Show = MsgBox(m_prompt, style, m_title)
//! End Function
//!
//! ' Convenience methods
//! Public Function ShowError(errorMsg As String) As Integer
//!     Set WithPrompt(errorMsg)
//!     Set WithIcon(vbCritical)
//!     ShowError = Show()
//! End Function
//!
//! Public Function AskYesNo(question As String) As Boolean
//!     Set WithPrompt(question)
//!     Set WithButtons(vbYesNo)
//!     Set WithIcon(vbQuestion)
//!     AskYesNo = (Show() = vbYes)
//! End Function
//! ```
//!
//! ### Example 3: Message Queue Manager
//!
//! ```vb
//! ' Class: MessageQueueManager
//! ' Manages queued messages to avoid overwhelming the user
//!
//! Option Explicit
//!
//! Private Type QueuedMessage
//!     prompt As String
//!     buttons As VbMsgBoxStyle
//!     title As String
//!     timestamp As Date
//! End Type
//!
//! Private m_queue As Collection
//! Private m_maxQueueSize As Long
//! Private m_autoShowDelay As Long ' milliseconds
//!
//! Private Sub Class_Initialize()
//!     Set m_queue = New Collection
//!     m_maxQueueSize = 10
//!     m_autoShowDelay = 1000
//! End Sub
//!
//! Public Sub QueueMessage(prompt As String, _
//!                        Optional buttons As VbMsgBoxStyle = vbOKOnly, _
//!                        Optional title As String = "Message")
//!     Dim msg As QueuedMessage
//!     
//!     msg.prompt = prompt
//!     msg.buttons = buttons
//!     msg.title = title
//!     msg.timestamp = Now
//!     
//!     If m_queue.Count >= m_maxQueueSize Then
//!         ' Remove oldest message
//!         m_queue.Remove 1
//!     End If
//!     
//!     m_queue.Add msg
//! End Sub
//!
//! Public Sub ShowNextMessage() As Integer
//!     Dim msg As QueuedMessage
//!     
//!     If m_queue.Count > 0 Then
//!         msg = m_queue(1)
//!         m_queue.Remove 1
//!         ShowNextMessage = MsgBox(msg.prompt, msg.buttons, msg.title)
//!     End If
//! End Sub
//!
//! Public Sub ShowAllMessages()
//!     Dim msg As QueuedMessage
//!     Dim i As Long
//!     
//!     For i = 1 To m_queue.Count
//!         msg = m_queue(i)
//!         MsgBox msg.prompt, msg.buttons, msg.title
//!     Next i
//!     
//!     Set m_queue = New Collection
//! End Sub
//!
//! Public Function GetQueuedCount() As Long
//!     GetQueuedCount = m_queue.Count
//! End Function
//!
//! Public Sub ShowSummary()
//!     Dim msg As String
//!     Dim qMsg As QueuedMessage
//!     Dim i As Long
//!     
//!     If m_queue.Count = 0 Then
//!         MsgBox "No queued messages", vbInformation
//!         Exit Sub
//!     End If
//!     
//!     msg = "Queued Messages (" & m_queue.Count & "):" & vbCrLf & vbCrLf
//!     
//!     For i = 1 To m_queue.Count
//!         qMsg = m_queue(i)
//!         msg = msg & i & ". " & Left(qMsg.prompt, 50)
//!         If Len(qMsg.prompt) > 50 Then msg = msg & "..."
//!         msg = msg & vbCrLf
//!     Next i
//!     
//!     MsgBox msg, vbInformation, "Message Queue"
//! End Sub
//! ```
//!
//! ### Example 4: Auto-Dismissing Message Box (Timer-based)
//!
//! ```vb
//! ' Module: TimedMessageBox
//! ' Shows message boxes that auto-dismiss after timeout
//!
//! Option Explicit
//!
//! #If Win32 Then
//!     Private Declare Function MessageBoxTimeout Lib "user32" Alias "MessageBoxTimeoutA" ( _
//!         ByVal hwnd As Long, _
//!         ByVal lpText As String, _
//!         ByVal lpCaption As String, _
//!         ByVal uType As Long, _
//!         ByVal wLanguageId As Long, _
//!         ByVal dwMilliseconds As Long) As Long
//! #End If
//!
//! Public Function MsgBoxTimed(prompt As String, _
//!                            Optional buttons As VbMsgBoxStyle = vbOKOnly, _
//!                            Optional title As String = "", _
//!                            Optional timeout As Long = 5000) As Integer
//!     #If Win32 Then
//!         ' Use Windows API for timed message box
//!         MsgBoxTimed = MessageBoxTimeout(0, prompt, title, buttons, 0, timeout)
//!     #Else
//!         ' Fallback to regular MsgBox
//!         MsgBoxTimed = MsgBox(prompt, buttons, title)
//!     #End If
//! End Function
//!
//! Public Sub ShowTimedInfo(message As String, Optional seconds As Long = 3)
//!     MsgBoxTimed message, vbInformation, "Information", seconds * 1000
//! End Sub
//!
//! Public Sub ShowTimedWarning(message As String, Optional seconds As Long = 5)
//!     MsgBoxTimed message, vbExclamation, "Warning", seconds * 1000
//! End Sub
//!
//! Public Function ConfirmTimed(message As String, Optional seconds As Long = 10) As Boolean
//!     Dim result As Integer
//!     result = MsgBoxTimed(message, vbYesNo + vbQuestion, "Confirm", seconds * 1000)
//!     ConfirmTimed = (result = vbYes)
//! End Function
//! ```
//!
//! ## Error Handling
//!
//! ```vb
//! ' MsgBox rarely fails, but handle potential issues:
//! On Error Resume Next
//! result = MsgBox(userInput, vbOKOnly, "Message")
//! If Err.Number <> 0 Then
//!     Debug.Print "MsgBox error: " & Err.Description
//!     ' Possibly userInput was too long or contained invalid characters
//! End If
//! On Error GoTo 0
//! ```
//!
//! ## Performance Considerations
//!
//! - `MsgBox` blocks execution - not suitable for background operations
//! - For debugging, consider Debug.Print as faster alternative
//! - Avoid `MsgBox` in loops - very slow and annoying to users
//! - For status updates, use status bar or progress form instead
//! - System modal (vbSystemModal) blocks ALL applications - use sparingly
//! - Long prompts may be truncated - keep messages concise
//! - Each `MsgBox` call creates and destroys a window - has overhead
//!
//! ## Best Practices
//!
//! 1. **Keep messages concise** - Users don't read long messages
//! 2. **Use appropriate icons** - Help users understand message severity
//! 3. **Provide clear actions** - Button choices should be obvious
//! 4. **Use meaningful titles** - Don't just say "Error" or "Message"
//! 5. **Avoid `MsgBox` in loops** - Queue messages or use alternative feedback
//! 6. **Handle all return values** - Check what button user clicked
//! 7. **Use vbCrLf for readability** - Multi-line messages are easier to read
//! 8. **Consider default button** - Make safe choice the default
//! 9. **Test message length** - Very long messages may not display well
//! 10. **Use for errors, not debugging** - Prefer Debug.Print for development
//!
//! ## Comparison with Alternatives
//!
//! | Approach | Pros | Cons |
//! |----------|------|------|
//! | **`MsgBox`** | Simple, built-in, modal | Blocks execution, limited customization |
//! | **Custom Form** | Full control, rich UI | More code, more complex |
//! | **Debug.Print** | Non-blocking, fast | Not visible to end users |
//! | **Status Bar** | Non-blocking, professional | Limited message length, less visible |
//! | **`InputBox`** | Gets user input | Only single-line text input |
//! | **Notification API** | Modern, non-blocking | Requires Windows 10+, more complex |
//!
//! ## Statement vs Function
//!
//! `MsgBox` can be used as both a statement (no parentheses, no return value) and a function (with parentheses, returns value):
//!
//! ```vb
//! ' As a statement (no return value needed)
//! MsgBox "Hello, World!"
//!
//! ' As a function (capture return value)
//! result = MsgBox("Continue?", vbYesNo)
//! ```
//!
//! ## Platform Notes
//!
//! - Available in VBA (Excel, Access, Word, etc.)
//! - Available in VB6
//! - Available in `VBScript` (limited - no named constants in `VBScript`)
//! - Uses Windows common dialog
//! - Appearance varies by Windows version
//! - Icons and sounds defined by Windows theme
//! - Right-to-left reading supported on appropriate systems
//!
//! ## Limitations
//!
//! - Blocks execution (modal)
//! - Limited to approximately 1024 characters in prompt
//! - Cannot customize button text (e.g., can't change "Yes" to "Accept")
//! - Cannot add custom icons
//! - Cannot resize or reposition the dialog
//! - No timeout capability (without API calls)
//! - No checkboxes or radio buttons
//! - Cannot validate input (use `InputBox` or custom form for that)
//!
//! ## Related Functions
//!
//! - **`InputBox`** - Gets text input from user
//! - **Debug.Print** - Prints to Immediate window (development only)
//! - **Shell** - Runs external programs
//! - **Beep** - Plays system beep sound
//! - **`SendKeys`** - Sends keystrokes to active window
//!
//! ## VB6 Parser Notes
//!
//! `MsgBox` can be used as both a statement and a function. When used as a function (with parentheses
//! and capturing return value), it is parsed as a `CallExpression`. When used as a statement (without
//! parentheses), it may be parsed differently. This module exists primarily for documentation purposes
//! to provide comprehensive reference material for VB6 developers working with user interaction and
//! message display operations.

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn msgbox_basic() {
        let source = r#"
Dim result As Integer
result = MsgBox("Hello")
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_with_buttons() {
        let source = r#"
Dim answer As Integer
answer = MsgBox("Continue?", vbYesNo)
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_with_title() {
        let source = r#"
MsgBox "Error occurred", vbCritical, "Error"
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_if_statement() {
        let source = r#"
If MsgBox("Save changes?", vbYesNo) = vbYes Then
    SaveFile
End If
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_select_case() {
        let source = r#"
Select Case MsgBox("Choose action", vbAbortRetryIgnore)
    Case vbAbort
        Exit Sub
    Case vbRetry
        Retry
    Case vbIgnore
        Continue
End Select
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_concatenation() {
        let source = r#"
MsgBox "Value: " & x & vbCrLf & "Status: " & status
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_function_return() {
        let source = r#"
Function AskUser() As Integer
    AskUser = MsgBox("Proceed?", vbYesNo + vbQuestion)
End Function
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_class_usage() {
        let source = r#"
Private Sub ShowMessage()
    Dim result As Integer
    result = MsgBox("Operation complete", vbInformation)
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_elseif() {
        let source = r#"
If x > 0 Then
    y = 1
ElseIf MsgBox("Try again?", vbRetryCancel) = vbRetry Then
    y = 2
End If
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_do_while() {
        let source = r#"
Do While MsgBox("Continue loop?", vbYesNo) = vbYes
    ProcessItem
Loop
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_do_until() {
        let source = r#"
Do Until MsgBox("Stop?", vbYesNo) = vbYes
    ProcessData
Loop
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_with_statement() {
        let source = r#"
With currentRecord
    MsgBox "Name: " & .Name & vbCrLf & "ID: " & .ID
End With
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_parentheses() {
        let source = r#"
Dim result As Integer
result = (MsgBox("Confirm?", vbOKCancel))
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_iif() {
        let source = r#"
Dim msg As String
msg = IIf(MsgBox("Yes or No?", vbYesNo) = vbYes, "Yes clicked", "No clicked")
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_array_context() {
        let source = r#"
Dim responses(5) As Integer
responses(i) = MsgBox("Question " & i, vbYesNo)
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_comparison() {
        let source = r#"
If MsgBox("Delete?", vbYesNo) = vbYes And confirmed Then
    DeleteRecord
End If
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_error_handling() {
        let source = r#"
On Error Resume Next
MsgBox "Error: " & Err.Description, vbCritical
On Error GoTo 0
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_multiline() {
        let source = r#"
MsgBox "Line 1" & vbCrLf & "Line 2" & vbCrLf & "Line 3", vbInformation, "Multi-line"
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_combined_flags() {
        let source = r#"
result = MsgBox("Warning!", vbYesNo + vbExclamation + vbDefaultButton2, "Confirm")
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_for_loop() {
        let source = r#"
For i = 1 To 5
    If MsgBox("Continue?", vbYesNo) = vbNo Then Exit For
Next i
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_while_wend() {
        let source = r#"
While MsgBox("Keep going?", vbYesNo) = vbYes
    count = count + 1
Wend
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_call_statement() {
        let source = r#"
Call ProcessResponse(MsgBox("Accept?", vbYesNo))
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_property_assignment() {
        let source = r#"
Set obj = New Dialog
obj.UserResponse = MsgBox("Confirm action?", vbOKCancel)
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_debug_context() {
        let source = r#"
#If DEBUG_MODE Then
    MsgBox "Debug: x = " & x
#End If
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_variable_buttons() {
        let source = r#"
Dim buttons As VbMsgBoxStyle
buttons = vbYesNo + vbQuestion
result = MsgBox("Proceed?", buttons)
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_function_argument() {
        let source = r#"
Call LogUserChoice(MsgBox("Save changes?", vbYesNoCancel))
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn msgbox_or_operator() {
        let source = r#"
result = MsgBox("Warning", vbYesNo Or vbExclamation, "Alert")
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/interaction/msgbox",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}