MsgBox Ran Out of Space? Try DotNetFactory MessageBox instead.
Did you know that the VBScript MsgBox that we usually use in QTP can display a string of maximum 1023 characters only? If the length of the string is more than 1023 characters, the MsgBox displays only the initial 1023 characters, leaving out the remaining ones.
Example:
sMsg = “”
‘For Loop to create a string of 1021 characters
For i = 1 to 1021
sMsg = sMsg & “*”
Next
sMsg = sMsg & “END”
‘Display the text in MsgBox
msgbox sMsg |
Now when we run the above code, we’ll get a popup box as displayed in the below image –
Here, you can observe that since the length of the string was 1024 characters, the message box skipped the last character ‘D’ in order to display 1023 characters.
The next obvious question that comes in our minds is – Is there any other way using which we can display more than 1023 characters in QTP using a message box? Is there any workaround? And the answer is.. YES!!
Although the VBScript MsgBox displays only maximum 1023 characters in a popup box, you can use Microsoft’s Windows Forms ‘MessageBox’ in QTP to display strings having much more characters. QTP provides a utility object named “DotNetFactory’, using which, you can access Windows Forms MessageBox and many other Form objects in your script.
sMsg = “” ‘For Loop to create a string of say, 3000 characters For i = 1 to 3000 sMsg = sMsg & “*” Next sMsg = sMsg & “END” ‘Display the text in Windows Forms MessageBox Set MsgBox = DotNetFactory.CreateInstance(“System.Windows.Forms.MessageBox”, “System.Windows.Forms”) MyMsgBox.Show sMsg |
If you run the above code, you get a popup message as shown below –
Here you can see that the text ‘END’ is completely visible in the MessageBox even after it displays a string of 3000 asterisks (*) . This shows that you can use Windows Forms MessageBox whenever you want more than 1023 characters to be displayed in your popup box.
Note: Even though you can display thousands of characters in a popup box using Windows Forms MessageBox, but it really doesn’t make sense to display more than 8K-9K characters in a MessageBox. This is because if the characters exceed a certain limit, the boundaries of the message box would exceed that the size of the screen rendering many portions of the text unreadable.
Happy Reading… :–)
If you enjoyed this article, you can join our blog to get free email updates directly in your inbox.