Sign in to follow this  

What does this mean? (VB.net)

This topic is 4218 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This code:
'display the messagebox dialog and capture the DialogResult returned...
intResult = MessageBox.Show(txtMessage.Text, txtCaption.Text, intButton, _
            intIcon, intDefaultButton)
Get's this message: Overload resolution failed because no accessible 'Show' can be called without a narrowing conversion: 'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons, icon As System.Windows.Forms.MessageBoxIcon) As System.Windows.Forms.DialogResult': Argument matching parameter 'owner' narrows from 'String' to 'System.Windows.Forms.IWin32Window'. 'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons, icon As System.Windows.Forms.MessageBoxIcon) As System.Windows.Forms.DialogResult': Argument matching parameter 'caption' narrows from 'Integer' to 'String'. 'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons, icon As System.Windows.Forms.MessageBoxIcon) As System.Windows.Forms.DialogResult': Argument matching parameter 'buttons' narrows from 'Integer' to 'System.Windows.Forms.MessageBoxButtons'. 'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons, icon As System.Windows.Forms.MessageBoxIcon) As System.Windows.Forms.DialogResult': Argument matching parameter 'icon' narrows from 'Integer' to 'System.Windows.Forms.MessageBoxIcon'. 'Public Shared Function Show(text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons, icon As System.Windows.Forms.MessageBoxIcon, defaultButton As System.Windows.Forms.MessageBoxDefaultButton) As System.Windows.Forms.DialogResult': Argument matching parameter 'buttons' narrows from 'Integer' to 'System.Windows.Forms.MessageBoxButtons'. 'Public Shared Function Show(text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons, icon As System.Windows.Forms.MessageBoxIcon, defaultButton As System.Windows.Forms.MessageBoxDefaultButton) As System.Windows.Forms.DialogResult': Argument matching parameter 'icon' narrows from 'Integer' to 'System.Windows.Forms.MessageBoxIcon'. 'Public Shared Function Show(text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons, icon As System.Windows.Forms.MessageBoxIcon, defaultButton As System.Windows.Forms.MessageBoxDefaultButton) As System.Windows.Forms.DialogResult': Argument matching parameter 'defaultButton' narrows from 'Integer' to 'System.Windows.Forms.MessageBoxDefaultButton'. What does this mean? To me it's just '??'.

Share this post


Link to post
Share on other sites
Two things come to mind. First, what values are held in the intButton, intIcon, and intDefaultButton variables? If they are real ints then they need to be casted to the correct type or just use the actual values(ie. MessageBoxButtons.OK). The other thing is that MSDN says this function is new in .NET 2.0 so if you're using 1.1 then it won't work. Based on the error messages, though, I'd say it's the first one, so give that a try and see what happens. Hope this helps.

-AJ

Share this post


Link to post
Share on other sites
Okay, the entire code looks like this (copied from a book):
Public Class frmMessageBox

Public Sub New()
MyBase.New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.

'add event handlers for icon radio buttons...
AddHandler rdbAsterisk.Click, AddressOf Icon_Click
AddHandler rdbError.Click, AddressOf Icon_Click
AddHandler rdbExclamation.Click, AddressOf Icon_Click
AddHandler rdbHand.Click, AddressOf Icon_Click
AddHandler rdbInformation.Click, AddressOf Icon_Click
AddHandler rdbQuestion.Click, AddressOf Icon_Click
AddHandler rdbStop.Click, AddressOf Icon_Click
AddHandler rdbWarning.Click, AddressOf Icon_Click

'add event handlers for button radio buttons...
AddHandler rdbAbortRetryIgnore.Click, AddressOf Icon_Click
AddHandler rdbOK.Click, AddressOf Icon_Click
AddHandler rdbOKCancel.Click, AddressOf Icon_Click
AddHandler rdbRetryCancel.Click, AddressOf Icon_Click
AddHandler rdbYesNo.Click, AddressOf Icon_Click
AddHandler rdbYesNoCancel.Click, AddressOf Icon_Click

'add event handler for defaultbutton radio buttons...
AddHandler rdbDefaultButton1.Click, AddressOf Icon_Click
AddHandler rdbDefaultButton2.Click, AddressOf Icon_Click
AddHandler rdbDefaultButton3.Click, AddressOf Icon_Click

End Sub

'Declare variables...
Private intIcon As Integer = MessageBoxIcon.Asterisk, _
intButton As Integer = MessageBoxButtons.OKCancel, _
intDefaultButton As Integer = MessageBoxDefaultButton.Button1

Private Sub Icon_Click(ByVal sender As Object, ByVal e As System.EventArgs)

'select the icon radio button that we checked and set the intIcon variable...
Select Case sender.Name
Case "rdbAsterisk"
intIcon = MessageBoxIcon.Asterisk
Case "rdbError"
intIcon = MessageBoxIcon.Error
Case "rdbExclamation"
intIcon = MessageBoxIcon.Exclamation
Case "rdbHand"
intIcon = MessageBoxIcon.Hand
Case "rdbInformation"
intIcon = MessageBoxIcon.Information
Case "rdbQuestion"
intIcon = MessageBoxIcon.Question
Case "rdbStop"
intIcon = MessageBoxIcon.Stop
Case "rdbWarning"
intIcon = MessageBoxIcon.Warning
End Select

End Sub

Private Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)

'select the radio button that was checked and set the intButton variable...
Select Case sender.Name
Case "rdbAbortRetryIgnore"
intButton = MessageBoxButtons.AbortRetryIgnore
Case "rdbOK"
intButton = MessageBoxButtons.OK
Case "rdbOKCancel"
intButton = MessageBoxButtons.OKCancel
Case "rdbRetryCancel"
intButton = MessageBoxButtons.RetryCancel
Case "rdbYesNo"
intButton = MessageBoxButtons.YesNo
Case "rdbYesNoCancel"
intButton = MessageBoxButtons.YesNoCancel

End Select

End Sub

Private Sub DefaultButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

'select the defaultbutton radio button that was checked and set the intDefaultButton variable...
Select Case sender.Name
Case "rdbDefaultButton1"
intButton = MessageBoxDefaultButton.Button1
Case "rdbDefaultButton2"
intButton = MessageBoxDefaultButton.Button2
Case "rdbDefaultButton3"
intButton = MessageBoxDefaultButton.Button3
End Select

End Sub

Private Sub btnShow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShow.Click

'declare variable for DialogResult...
Dim intResult As Integer

'display the messagebox dialog and capture the DialogResult returned...
intResult = MessageBox.Show(txtMessage.Text, txtCaption.Text, intButton, _
intIcon, intDefaultButton)

'select the apropriate result returned and display a message...
Select Case intResult
Case DialogResult.Abort
txtResult.Text = "Abort Clicked"
Case DialogResult.Retry
txtResult.Text = "Retry Clicked"
Case DialogResult.Ignore
txtResult.Text = "Ignore Clicked"
Case DialogResult.OK
txtResult.Text = "OK Clicked"
Case DialogResult.Cancel
txtResult.Text = "Cancel Clicked"
Case DialogResult.Yes
txtResult.Text = "Yes Clicked"
Case DialogResult.No
txtResult.Text = "No Clicked"
End Select

End Sub
End Class


I wrote this code in VS 2005, so I guess it uses .NET 2.0 ;)

Share this post


Link to post
Share on other sites
So intButton, intIcon, and intDefaultButton are all Integers? I haven't done a whole lot in VB.NET, but I know in C# if you do something like that, you need to cast the Integers to the appropriate enum type. Also, regarding the errors on the strings for the message and caption, I don't know why, but it looks like the compiler is treating them as Integers. Try passing CStr(txtMessage.Text) and CStr(txtCaption.Text) instead. I an assuming that textMessage and txtCaption are textboxes on your form. Hope that helps.

-AJ

Share this post


Link to post
Share on other sites
At most VB would throw up a warning but it would allow it since they're enums, not really types, and enums are by default integers anyways.

You didn't accidentally make something else named messagebox did you?

What about writing a normal call without variable based parameters above the erroring code as a test?

Share this post


Link to post
Share on other sites
After writing that I realized, I bet one of the enums isn't an integer. Change your variables to the explicit types that the MessageBox.Show functiohn asks for instead of generic integers.

Share this post


Link to post
Share on other sites
Quote:
Original post by u235
So intButton, intIcon, and intDefaultButton are all Integers? I haven't done a whole lot in VB.NET, but I know in C# if you do something like that, you need to cast the Integers to the appropriate enum type. Also, regarding the errors on the strings for the message and caption, I don't know why, but it looks like the compiler is treating them as Integers. Try passing CStr(txtMessage.Text) and CStr(txtCaption.Text) instead. I an assuming that textMessage and txtCaption are textboxes on your form. Hope that helps.

-AJ


That didn't work, CStr is aparantly not a valid expression in VB.net... :/

Share this post


Link to post
Share on other sites
Quote:
Original post by Zyndrof
Quote:
Original post by u235
So intButton, intIcon, and intDefaultButton are all Integers? I haven't done a whole lot in VB.NET, but I know in C# if you do something like that, you need to cast the Integers to the appropriate enum type. Also, regarding the errors on the strings for the message and caption, I don't know why, but it looks like the compiler is treating them as Integers. Try passing CStr(txtMessage.Text) and CStr(txtCaption.Text) instead. I an assuming that textMessage and txtCaption are textboxes on your form. Hope that helps.

-AJ


That didn't work, CStr is aparantly not a valid expression in VB.net... :/


It should work. I've used it before in VB .NET projects with no trouble. Are those the only errors you're getting now, the ones regarding the strings?

-AJ

Share this post


Link to post
Share on other sites
Quote:
Original post by u235
It should work. I've used it before in VB .NET projects with no trouble. Are those the only errors you're getting now, the ones regarding the strings?

-AJ


Nope, the old error's still there.

Share this post


Link to post
Share on other sites
Well, I'm all out of ideas, then, sorry. But I will try your code out when I get back to my computer at home and see if I can't figure out what's going on.

-AJ

Share this post


Link to post
Share on other sites
Quote:
Original post by ItsDoh
You didn't accidentally make something else named messagebox did you?

What about writing a normal call without variable based parameters above the erroring code as a test?


No, I didn't, I've even double-checked that. The code is copied from a book I'm learning from, which makes it even odder that it doesn't work...

Share this post


Link to post
Share on other sites
Quote:
Original post by Nik02
If you have "Option Strict" on, have you tried to disable it (from project configuration)?


Could you tell me more about this "Option Strict". What does it do and why should it be disabled?

Share this post


Link to post
Share on other sites
Option Strict requires that all variables be the exact type requested, without it VB is a little more flexible. For example assigning an Integer to a String is valid without Strict on because VB knows how to convert an integer to a string. With Strict on, that would produce a warning.

Have you tried making just a normal messagebox call without the variables, just a simple line as a test?

Share this post


Link to post
Share on other sites
Quote:
Original post by ItsDoh
Have you tried making just a normal messagebox call without the variables, just a simple line as a test?


No, but I will try it... Then try that Otion thing :)

Share this post


Link to post
Share on other sites

This topic is 4218 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this