Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Message Box Problems

Status
Not open for further replies.
Aug 17, 2008
28
I am the creator of a (very popular) program that allows users to create their own Windows-based message boxes. In order to make said program better, I am updating it to allow the user to create messages using icons from a certain list I've created (thus, I am using a .RES file).

The following code creates the message box:

Code:
MsgBoxEx((msgtext), (msgtype), (msgtitle), , lstCustomIcons.ListIndex)

msgtext = Text in message box.
msgtype = Depends on what type of box the user wants to create. Example: vbInformation or vbOKonly.
msgtitle = Obvious.

lstCustomIcons.ListIndex refers to the ID number of the icon in the .RES file.

msgtext, msgtype and msgtitle are in brackets because every time I tried to compile the program, I got a message saying 'ByRef argument type mismatch'.

So, I wrapped them in the brackets, and that fixed it, but now the custom icons are not showing. What am I doing wrong? I followed the instructions set out by Hypetia ( but it's just not working.

GeodesicDragon
curquhart.co.uk
 

You have a procedure, a SUB, calles MsgBoxEx. This Sub takes some arguments, like msgtext, msgtype and msgtitle.

How many arguments does your Sub need? You are passing 4, but you have a space between two commas.

What's the Type of msgtext, msgtype and msgtitle? A String? What's the Type of the 4th arument?

It would help to see the code of your Sub MsgBoxEx



Have fun.

---- Andy
 
Try
Code:
MsgBoxEx msgtext, msgtype, msgtitle, , _
         CLng(lstCustomIcons.ListIndex)
The fourth and fifth arguments in Hypetia's code are longs but the default datatype for the ListIndex property of a listbox is Integer.
 
@ Andrzejek

MsgBoxEX is a Function. It begins like so:

Code:
Private Function MsgBoxEx(Prompt As String, Optional Buttons As VbMsgBoxStyle, Optional Title As String, Optional hResource As Long, Optional IconResource As Long) As VbMsgBoxResult

These are the variables it expects.

@ Golom

I tried:

Code:
MsgBoxEx(msgtext, msgtype, msgtitle, , CLng(lstCustomIcons.ListIndex))

But I am getting the 'ByRef argument type mismatch' error when I try and compile the application.
 
I don't think you can send listindex as reference. Try

dim lstIndex as integer
lstindex=lstCustomIcons.ListIndex
MsgBoxEx(msgtext, msgtype, msgtitle, , lstIndex)

Haven't tried it, though, just a though.
 
Disferente

I tried what you suggested, but I am still getting this 'ByRef argument type mismatch' error.

I have a feeling that if I fix that, the whole program will do what I am wanting it to do.

GeodesicDragon
curquhart.co.uk
 
You need to show us more of your code. For example, it would be good to see the declarations of msgtext, msgtype, and msgtitle
 
1) Change

Dim msgtype, msgtext, msgtext2, msgtitle, lstIndex As Integer

to

Dim msgtext As String
Dim msgtext2 As String
Dim msgtitle As String
Dim msgtype As VbMsgBoxStyle
Dim lstIndex As Integer



 
strongm

I did as you suggested, but I am still getting that 'ByRef' error. When I get it and click 'OK', it highlights 'lstIndex' in

Code:
MsgBoxEx(msgtext, msgtype, msgtitle, , lstIndex)

lstCustomIcons has the 'Dropdown List' Style property, if that is any use.
 
You've already had it explained to you why passing lstIndex causes a problem, and one of the ways of solving it.

So, you can either use CLng or parenthesis on lstIndex as previously advised - or declare it as a Long (in two cases you get an implicit cast from one type to another, in the other the cxast is explicit)

 
And I have already explained that it doesn't work.

No matter what I try, I am always getting the error.
 
>And I have already explained that it doesn't work

That's because a) it is appears you have only really tried one of the advised solutions for lstIndex; and, more importantly, b) misunderstood that advice

The solution you appear to have tried is the CLng solution, but in your application you have used it in completely the wrong place:

lstIndex = CLng(lstCustomIcons.ListIndex)

Since lstIndex is declared as an Integer all that happens here is that Clng converst the value to a Long and then it is cast back to an Integer. And it is that Integer that you try to pass to the function when it is expecting a Long. Hence you get the error. Golom showed you exactly where to use the Clng conversion - in the function call (and Golom at that point couldn't know that you had misdeclared every single one of the variables that you were passing as parameters - and it is those which was caused the 'ByRef argument type mismatch' error(s) that you got when you tried his solution, and not the CLng which works fine.

And if you try either of the other suggestions (parenthesis around lstIndex - again, to be used in the function call rather than anywhere else, exactly as you had found worked on the other parameters - or declaring lstIndex as a Long) you should find that they also both resolve the problem.
 
I have done what Golom said, and it's still not working.

I've decided to just scrap the entire project.

Thanks anyway for helping.
 
I don't understand. Any one of the three options I have given you cause your code to start working. Why would you give up now?

 
Don't give up!

I hope you realise that when the code DOES run it will only work (with your custom icons from its resource) when compiled into an exe file; in the IDE the VB6.exe file's resources will be used as per Hypetias previous example.

I've modified your uploaded zip code to make it work for the single line message with custom icon, see my comments;
For some reason the 'Nuclear' icon will not appear for me though.

Private Sub cmdCreateCustom_Click()

If txtMessage.Text = "" Then
MsgBox "Message Line 1 must contain a value.", vbExclamation Or vbOKOnly, "Error"
Exit Sub
Else

'Set up variables

Dim msgtype As VbMsgBoxStyle, msgtext As String, msgtext2 As String, msgtitle As String, lstindex As Long

msgtype = cboMsgType.ListIndex
msgtext = txtMessage.Text
msgtext2 = txtMessage2.Text
msgtitle = txtMsgTitle.Text
lstindex = lstCustomIcons.ListIndex

'I know this is a lot of 'Ifs', but I don't know any other
'way to do it.

'You will need to redevelop this block because specifying vbQuestion,vbInformation, vbcritical, vbexclamation icons here will prevent your custom icon being set
If msgtype = 0 Then
msgtype = vbQuestion Or vbYesNo
ElseIf msgtype = 1 Then
msgtype = vbQuestion Or vbYesNoCancel
ElseIf msgtype = 2 Then
msgtype = vbInformation Or vbOKOnly
ElseIf msgtype = 3 Then
msgtype = vbCritical Or vbOKOnly
ElseIf msgtype = 4 Then
msgtype = vbExclamation Or vbOKOnly
ElseIf msgtype = 5 Then
msgtype = vbInformation Or vbMsgBoxHelpButton
ElseIf msgtype = 6 Then
msgtype = vbExclamation Or vbMsgBoxHelpButton
ElseIf msgtype = 7 Then
msgtype = vbCritical Or vbMsgBoxHelpButton
ElseIf msgtype = 8 Then
msgtype = vbInformation Or vbAbortRetryIgnore
ElseIf msgtype = 9 Then
msgtype = vbExclamation Or vbAbortRetryIgnore
ElseIf msgtype = 10 Then
msgtype = vbCritical Or vbAbortRetryIgnore
ElseIf msgtype = 11 Then
msgtype = vbInformation Or vbRetryCancel
ElseIf msgtype = 12 Then
msgtype = vbExclamation Or vbRetryCancel
ElseIf msgtype = 13 Then
msgtype = vbCritical Or vbRetryCancel
End If

'Hide the form, so the victim doesn't see it.
Form1.Hide

'Create a one-line message box, with custom icon.
If msgtext2 = "" Then
'note vbYesNo has been used here to make the code work, replace vbYesNo with msgtype when you have sorted out the code for it above
If MsgBoxEx(msgtext, vbYesNo, msgtitle, , lstindex) = vbAbort Or vbCancel Or vbIgnore Or vbNo Or vbOK Or vbRetry Or vbYes Then
Form1.Show
If chkSucker = vbChecked Then
MsgBox "SUCKER!", vbInformation Or vbOKOnly, "Gotcha!"
End If
End If
Else

'Create a two-line message box, with custom icon.
If MsgBoxEx((msgtext), (msgtype), (msgtitle), , (lstindex)) = vbAbort Or vbCancel Or vbIgnore Or vbNo Or vbOK Or vbRetry Or vbYes Then
Form1.Show
If chkSucker = vbChecked Then
MsgBox "SUCKER!", vbInformation Or vbOKOnly, "Gotcha!"
End If
End If
End If
End If

End Sub
 
I have struggled long and hard in the early days coming to grips with variable types and exactly these types of errors. since those early days, I now use "Option Explicit" in EVERY module to help prevent type errors. Also, when declaring variables, I use Hunagrian Notation, e.g. Dim lngIndex as Long, Dim strMsg as String, etc. That way I know precisely what the Type of a variable is when using it. Furthermore, I ALWAYS make sure that when passing variables into/out of a function that they are of the SAME TYPE. The Hungarian notation helps immensely in this way. Finally, I declare every single variable individually on its own line so that I don't accidentally get a Variant instead of the type I really want.
Don't give, use this as an experience to improve your programming technique. It WILL work once you get your types correct.
Good Luck
Walter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top