שתף באמצעות


Message box being displayed then automatically closed!

Question

Tuesday, March 11, 2014 10:46 AM

I hope someone can explain this behaviour to me...

I added the following simple code to the end of a Public Sub after the try catch that encloses the main body of this process (the rest of which has been working fine for some time) but the program isn't stopping when the error message box is displayed.

The box is drawn, but then disappears, as if it was automatically being closed.

But even more strange, when I added a test text box (the one showing the text ‘Problem’) this behaved the same as the error text, but then the messages in the case statement worked as expected!

Any idea what could cause a message box to automatically close like this?

...


        Catch ex As Exception
            Dim oError As New ErrorHandler
            Dim sMessage As String = oError.BuildErrorMessage("clsDisgorgeList", "Read", ex)
            logMonkey.Error(sMessage + " " + ex.ToString())
            Throw New Exception(sMessage)
        End Try


        MessageBox.Show("Problem")
        'Hault the program to show popup messages for out of scope barrels
        Select Case lIncidentCode
            Case 2
                MessageBox.Show("This cask does not meet Minimum Blend Age." & Environment.NewLine & Environment.NewLine & "Please inform the administrator before continuing.")
            Case 3
                MessageBox.Show("This cask does not meet Minimum Legal Age." & Environment.NewLine & Environment.NewLine & "Please inform the administrator before continuing.")
        End Select
    End Sub

All replies (11)

Tuesday, March 11, 2014 4:31 PM ✅Answered | 1 vote

Thanks for the help. I think I've figured out the problem - if not a solution...

It looks like the scanner has an auto post-amble 'ENTER' (carriage return) symbol set, which it appends to the string - hence this is being held in the input stream and  'fired' when I open a message box. Effectively closing the box with a 'return' key press. I'll look on the phone forum as well... Thanks.

I've sort-of fixed this, by removing the post-amble string from the scanner. It's hardly the best solution though!

Is there no way of clearing the 'ENTER' character from the input buffer?


Tuesday, March 11, 2014 12:39 PM

I've since moved this message box code to different classes and forms and it is doing the same thing no matter where I put it...

E.g. the first message box is always opened and then closed mediately, with the next message box behaving normally, i.e. displaying and halting the program as expected!

This application is running on a hand-held CE device. I'm using .net 3.5 with Windows Mobile 6 Professional SDK. But surely that shouldn't make this difference to a message box!  

Can it be anything to do with the input buffer? The parent form scans in a bar-code, the results of which are used with all the affected functions.


Tuesday, March 11, 2014 2:50 PM

Hello Garry,

We need to know more about code which is being called but not being shown i.e. ErrorHandler and the class for LogMonkey. Lastly, for VB.NET use & for string concatenation or as shown here

logMonkey.Error(String.Concat(sMessage, " ", ex.ToString()))

Lastly set Option Strict On under project properties

Make sure there is no line reading Option Strict Off in any code.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Tuesday, March 11, 2014 3:08 PM

Hello,

The rest of the code shows in the example is irrelevant because it is doing the same thing anywhare called from the scanning form.

I set Option strict to on in the project options.

There is no 'Option Strict Off' in any of the code.

The problem message box is now in a procedure in the forms code-behind that is called after a scanner has been used to input data into a text field on the form...

        Private Sub tbx_Scan_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbx_Scan.TextChanged
        'Takes the barcode text direct from the reader - (one character at a time)
        'used for i.roc Ci170 -Ex : As this dosn't read using the 'bcr.BarcodeRead' handler

        If Len(Me.tbx_Scan.Text) >= 3 Then
            If (Len(Me.tbx_Scan.Text) = 24) And (Me.tbx_Scan.Text.Substring(0, 3) = "]C0") Then
                'Update the scan code...
                logwriter.Info("Read the barcode: " & Me.tbx_Scan.Text)
                Me.Label_ShowText.Text = Me.tbx_Scan.Text.Remove(0, 3) 'remove leader ']C0'
                HandleData(Me.tbx_Scan.Text)
                Me.tbx_Scan.Text = ""
            Else
                If (Len(Me.tbx_Scan.Text) > 24) Then
                    Me.tbx_Scan.Text = ""                               'resets if too much data is receieved
                    'Make some noise!
                    Beep()
                    Beep()
                    Beep()
                    Beep()
                End If
            End If
        End If

        ' Enable the timer to Clear scanned text of < 24 characters from the input after a given time
        If (Len(Me.tbx_Scan.Text) < 24) And Me.tbx_Scan.Text <> "" Then
            Me.lowScanTimer.Enabled = True
        Else
            Me.lowScanTimer.Enabled = False
        End If
    End Sub    Private Sub HandleData(ByVal ReaderData As String)
        Dim lErrorNo As Integer = 0
        '_bProcessScan = False
        'Logwriter start
        logwriter.Info("Enabling the scanner.")
        logwriter.Info("Read the barcode: " & ReaderData)


        Dim cDisgorge As clsDisgorgeList = New clsDisgorgeList

        'Run Checks...
        cDisgorge.validateScan(ReaderData, CInt(Me.cbx_LineNo.SelectedValue), Me.cbx_LineNo.Text, Me.Label_UID.Text, lErrorNo)

        'Show the OK or Error messages
        If cDisgorge.ScannError = "None" Then
            Me.label_LastCask.Text = cDisgorge.ValidDataString 'Update the previous value as the scan is valid
            Me.panel_ERROR.Visible = False
            Me.panel_OK.Visible = True
            Me.BackColor = Color.FromArgb(191, 191, 191)
            Me.label1.ForeColor = Color.Black
            Me.label5.ForeColor = Color.Black
            Me.label10.ForeColor = Color.Black
            Me.label11.ForeColor = Color.Black
            Me.label_CurrentAllocation.ForeColor = Color.Black
            Me.label_CasksLeft.ForeColor = Color.Black
            Me.label_LastCask.ForeColor = Color.Black
        Else
            Me.Label_Errors.Text = cDisgorge.ScannError
            Me.panel_OK.Visible = False
            Me.panel_ERROR.Visible = True
            Me.BackColor = Color.Black
            Me.label1.ForeColor = Color.White
            Me.label5.ForeColor = Color.White
            Me.label10.ForeColor = Color.White
            Me.label11.ForeColor = Color.White
            Me.label_CurrentAllocation.ForeColor = Color.White
            Me.label_CasksLeft.ForeColor = Color.White
            Me.label_LastCask.ForeColor = Color.White
        End If

        'Hault the program and show popup messages if this is an 'out of scope' barrel
        MessageBox.Show("Problem") ' <- Still closes as soon as it is opened...
        Select Case lErrorNo
            Case 2
                Me.BackColor = Color.DarkSalmon
                MessageBox.Show("This cask does not meet Minimum Blend Age." & Environment.NewLine & Environment.NewLine & "Please inform the administrator before continuing.")
                Me.BackColor = Color.Black
            Case 3
                Me.BackColor = Color.DarkRed
                MessageBox.Show("This cask does not meet Minimum Legal Age." & Environment.NewLine & Environment.NewLine & "Please inform the administrator before continuing.")
                Me.BackColor = Color.Black
        End Select

        'Logwriter End
        logwriter.Info("Disabling the scanner.")
    End Sub

Tuesday, March 11, 2014 3:30 PM

Garry,

In the way you describe it, this code in a new project of you should close the messagebox.

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MessageBox.Show("I become invisible without a click")
    End Sub
End Class

However, I see that you throw a new exception in the catch, can it be that the box is not close but hidden behind the form when the thrown exception is handled?

Success
Cor


Tuesday, March 11, 2014 3:36 PM

Another (almost identical) manual entry process works fine, the only difference is the scanner used for input to the one! But I'm at a loss as to why this could cause the problem!

Is there a way to clear the keyboard/mouse input buffer, just to check?

I think this may be being cause by there still being input left in the scanner stream, after the 24 character limit was found. A non-printable return character maybe?

Does anyone know how to clear this type of com stream??


Tuesday, March 11, 2014 3:40 PM | 1 vote

Hello,

I see no issue that would cause the MessageBox problem in the code you have shown. Perhaps you should ask present this issue under the Windows phone forum.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Tuesday, March 11, 2014 4:43 PM

Good you are on the right track. Personally I have not seen this behavior from a MessageBox.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Thursday, March 13, 2014 7:10 AM | 1 vote

Thanks for the help. I think I've figured out the problem - if not a solution...

It looks like the scanner has an auto post-amble 'ENTER' (carriage return) symbol set, which it appends to the string - hence this is being held in the input stream and  'fired' when I open a message box. Effectively closing the box with a 'return' key press. I'll look on the phone forum as well... Thanks.

I've sort-of fixed this, by removing the post-amble string from the scanner. It's hardly the best solution though!

Is there no way of clearing the 'ENTER' character from the input buffer?

I'm not sure what the input buffer would have to do with anything.

Regardless if the Okay button of the MessageBox is receiving focus and then enter then perhaps this code will resolve the issue although it doesn't clear a buffer. It just displays the MessageBox again if the first MessageBox DialogResult = OK.

If MessageBox.Show("Problem") = Windows.Forms.DialogResult.OK Then
            MessageBox.Show("Problem")
End If

I also attempted to use the below code to stop processing System.Windows.Forms.Messages for the MessageBox temporarily until the time between the MessageBox showing and the first Enter occurs. But I couldn't get it to work. Probably because the Form doesn't receive messages meant for the MessageBox when closing the MessageBox when enter occurs with the MessageBox showing.

I looked at the WM (Constants) after having saved all of the m.Tostring messages to a file. So before the messagebox showed I set an integer to 1 and after it closed back to 0. The messages received by the Form, after the messagebox shows begin with 1 .. and if its not showing then 0 .. in the codeblock below this one. So I tried If Statements in the Overrides Sub so MyBase.WndProc(m) would not process the first message for closing the MessageBox thinking one of those messages starting with 1.. were for closing the MessageBox.

If Counter = 1 AndAlso m.ToString.Contains("WM_SOMEMESSAGE") Then

Counter = 0

Else

MyBase.WndProc(m)

End If

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
End Sub
0 .. msg=0xe (WM_GETTEXTLENGTH) hwnd=0x1304ca wparam=0x0 lparam=0x0 result=0x0
0 .. msg=0xd (WM_GETTEXT) hwnd=0x1304ca wparam=0x6 lparam=0x1c7bbb00 result=0x0
1 .. msg=0x1f (WM_CANCELMODE) hwnd=0x1304ca wparam=0x0 lparam=0x0 result=0x0
1 .. msg=0xa (WM_ENABLE) hwnd=0x1304ca wparam=0x0 lparam=0x0 result=0x0
1 .. msg=0x1f (WM_CANCELMODE) hwnd=0x1304ca wparam=0x0 lparam=0x0 result=0x0
1 .. msg=0x86 (WM_NCACTIVATE) hwnd=0x1304ca wparam=0x0 lparam=0x3a058a result=0x0
1 .. msg=0x6 (WM_ACTIVATE) hwnd=0x1304ca wparam=0x0 lparam=0x3a058a result=0x0
1 .. msg=0x46 (WM_WINDOWPOSCHANGING) hwnd=0x1304ca wparam=0x0 lparam=0x1c7bce40 result=0x0
1 .. msg=0x14 (WM_ERASEBKGND) hwnd=0x1304ca wparam=0x1d010f61 lparam=0x0 result=0x0
1 .. msg=0xe (WM_GETTEXTLENGTH) hwnd=0x1304ca wparam=0x0 lparam=0x0 result=0x0
1 .. msg=0xd (WM_GETTEXT) hwnd=0x1304ca wparam=0x6 lparam=0x1c7bb540 result=0x0
1 .. msg=0x318 (WM_PRINTCLIENT) hwnd=0x1304ca wparam=0x1d010f61 lparam=0x4 result=0x0
1 .. msg=0xe (WM_GETTEXTLENGTH) hwnd=0x1304ca wparam=0x0 lparam=0x0 result=0x0
1 .. msg=0xd (WM_GETTEXT) hwnd=0x1304ca wparam=0x6 lparam=0x1c7bb440 result=0x0
1 .. msg=0xe (WM_GETTEXTLENGTH) hwnd=0x1304ca wparam=0x0 lparam=0x0 result=0x0
1 .. msg=0xd (WM_GETTEXT) hwnd=0x1304ca wparam=0x6 lparam=0x1c7bb440 result=0x0
1 .. msg=0x46 (WM_WINDOWPOSCHANGING) hwnd=0x1304ca wparam=0x0 lparam=0x1c7bcd30 result=0x0
1 .. msg=0x86 (WM_NCACTIVATE) hwnd=0x1304ca wparam=0x1 lparam=0x3a058a result=0x0
1 .. msg=0x6 (WM_ACTIVATE) hwnd=0x1304ca wparam=0x1 lparam=0x3a058a result=0x0
1 .. msg=0x281 (WM_IME_SETCONTEXT) hwnd=0x1304ca wparam=0x1 lparam=0xc000000f result=0x0
1 .. msg=0x282 (WM_IME_NOTIFY) hwnd=0x1304ca wparam=0x2 lparam=0x0 result=0x0
1 .. msg=0x7 (WM_SETFOCUS) hwnd=0x1304ca wparam=0x3a058a lparam=0x0 result=0x0
1 .. msg=0xa (WM_ENABLE) hwnd=0x1304ca wparam=0x1 lparam=0x0 result=0x0
1 .. msg=0x8 (WM_KILLFOCUS) hwnd=0x1304ca wparam=0x2604ea lparam=0x0 result=0x0
1 .. msg=0x281 (WM_IME_SETCONTEXT) hwnd=0x1304ca wparam=0x0 lparam=0xc000000f result=0x0
1 .. msg=0x135 (WM_CTLCOLORBTN) hwnd=0x1304ca wparam=0x5e010dc1 lparam=0x2604ea result=0x0
1 .. msg=0x2b (WM_DRAWITEM) hwnd=0x1304ca wparam=0x2604ea lparam=0x1c7bc8e0 result=0x0
1 .. msg=0x7 (WM_SETFOCUS) hwnd=0x1304ca wparam=0x0 lparam=0x0 result=0x0
0 .. msg=0x14 (WM_ERASEBKGND) hwnd=0x1304ca wparam=0xffffffffe2011161 lparam=0x0 result=0x0
0 .. msg=0xe (WM_GETTEXTLENGTH) hwnd=0x1304ca wparam=0x0 lparam=0x0 result=0x0
0 .. msg=0xd (WM_GETTEXT) hwnd=0x1304ca wparam=0x6 lparam=0x1c7bbfa0 result=0x0

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


Wednesday, March 19, 2014 9:48 AM

Thanks Mr.Monkeyboy,

The problem seems to be coming from the way I'm reading the input string (which is built up from a scanner one character at a time, and ended with a new-line character.

        If Len(Me.tbx_Scan.Text) >= 3 Then
            If (Len(Me.tbx_Scan.Text) = 24) And (Me.tbx_Scan.Text.Substring(0, 3) = "]C0") Then

I only ever need the first 24 characters, so anything above that is ignored (including the new-line). The problem with reading in the entire string is that there is no easy (reliable) way to say when it has finished. I can't rely on the new-line character always being at the end on the input string, as erroneous input isn't that uncommon from the scanner; especially when the user gets board and scans their lunch to see what happens!

I've marked your post as answered because I used the OK trap suggestion to get around the problem, but I still haven't really 'solved' it!

Thanks,


Thursday, March 20, 2014 2:13 AM

Thanks even though it doesn't really correct the situation.

Although this "especially when the user gets board and scans their lunch to see what happens!" is too funny!

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.