none
Public Function Help with MessageBox RRS feed

  • Question

  • Can someone help me understand why this function doesn't show my message box? The function works all besides the

    MessageBox.Show("Apple Mobile Device USB Driver = True", "My Application",
               MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)

    Thanks.

    Public Function IsUserlandConnected()
            Dim forever As Boolean = True
            Dim USBName As String = String.Empty
            Dim USBSearcher As New ManagementObjectSearcher(
                      "root\CIMV2",
                      "SELECT * FROM Win32_PnPEntity WHERE Description = 'Apple Mobile Device USB Driver'")
            For Each queryObj As ManagementObject In USBSearcher.Get()
                USBName += (queryObj("Description"))
            Next
            If USBName = "Apple Mobile Device USB Driver" Then
                Return True
                MessageBox.Show("Apple Mobile Device USB Driver = True", "My Application",
               MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
            Else
                Return False
                MessageBox.Show("Apple Mobile Device USB Driver = False", "My Application",
               MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
            End If
        End Function

    Thursday, March 30, 2017 10:04 PM

All replies

  • You set

    Return True

    or

    Return False

    just before...

    Thursday, March 30, 2017 10:07 PM
  • Still doesn't work? I tried removing

    Return True

    and

    Return False

    but it still doesn't work.. I also tried this.

    Public Function IsUserlandConnected()
            Dim forever As Boolean = True
            Dim USBName As String = String.Empty
            Dim USBSearcher As New ManagementObjectSearcher(
                      "root\CIMV2",
                      "SELECT * FROM Win32_PnPEntity WHERE Description = 'Apple Mobile Device USB Driver'")
            For Each queryObj As ManagementObject In USBSearcher.Get()
                USBName += (queryObj("Description"))
            Next
            If USBName = "Apple Mobile Device USB Driver" Then
                Return True
                If IsUserlandConnected.Equals(True) Then
                    MessageBox.Show("Apple Mobile Device USB Driver = True", "My Application",
               MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
                End If
            Else
                Return False
                If IsUserlandConnected.Equals(False) Then
                    MessageBox.Show("Apple Mobile Device USB Driver = False", "My Application",
               MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
                End If
            End If
        End Function

    Thursday, March 30, 2017 11:02 PM
  • Hi

    Which part of the post from Castorix31 did you not understand. When the code reaches any of the Return commands, it exits the function (with the appropriate value), and never reaches your MessageBox lines.


    Regards Les, Livingston, Scotland

    Thursday, March 30, 2017 11:09 PM
  • Still doesn't work?

    You still have your return statements before the message box statements. That means the code can never get to the message box statement.  The return statement must appear as the last statement that is executed in the function.  See: https://msdn.microsoft.com/en-us/library/2e34641s.aspx?

    After you move the return statements to the correct location, insert a breakpoint at the start of the function.  Step the code through the function one line at a time, noting the values of the relevant variables at each step, and noting the execution path that the code follows through the function.

    Thursday, March 30, 2017 11:10 PM
  • Hi TimeFluid,

    Based on your description, I modify your code below, please refer to.

    Public Function IsUserlandConnected()
            Dim forever As Boolean = True
            Dim USBName As String = String.Empty
            Dim USBSearcher As New ManagementObjectSearcher(
                      "root\CIMV2",
                      "SELECT * FROM Win32_PnPEntity WHERE Description = 'Apple Mobile Device USB Driver'")
            For Each queryObj As ManagementObject In USBSearcher.Get()
                USBName += (queryObj("Description"))
            Next
            If USBName = "Apple Mobile Device USB Driver" Then
                'Return True
                MessageBox.Show("Apple Mobile Device USB Driver = True", "My Application",
               MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
            Else
                'Return False
                MessageBox.Show("Apple Mobile Device USB Driver = False", "My Application",
               MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
            End If
        End Function
    

    You just remove return true and return false, and then the messagebox will be triggered.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 31, 2017 2:37 AM
    Moderator
  • Based on your description, I modify your code below, please refer to.

    Your function does not have a type.  It does not return a result.  It is same code that OP has already tried, which did not work. It should not be provided as an example.

    Friday, March 31, 2017 3:55 AM
  • Replace this:

            For Each queryObj As ManagementObject In USBSearcher.Get()
                USBName += (queryObj("Description"))
            Next

    with this:

            For Each queryObj As ManagementObject In USBSearcher.Get()
                USBName &= (queryObj("Description"))
            Next
            MessageBox.Show(USBName)
    Maybe that will shed some light on this issue. It is possible also that the query is returning a string with some "invisible"unicode characters.

    Monday, April 3, 2017 5:15 AM