Click here to Skip to main content
16,022,054 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created my own Message Box ('myMB') and have used it 1000's of times successfully. It worked fine during debugging this AM.
However now, when my message box returns the integer 1 (for example, I pressed the "Yes" button in the myMB Window) and the returned value is compared to 1, the debugger does not move to the code that it should... This sounds crazy and so my explanation may not seem to make plausible sense; so here is some example code.

VB
If myMB.DialogResult = 1 Then
    Stop
End If

The above example is exact (test) code I've used. When run step by step (or Run w/o stepping), the program DOES NOT 'Stop.' I have confirmed that

myMB.DialogResult = 1

and that

myMB.DialogResult = 1 evaluates to 'True'

but the code does not move to the 'Stop' statement.

Again, similar code worked an hour ago.

What is going on?!?!


IMPROVED QUESTION
As I mentioned I'd used my own message box 1000's of time (years) w/o a problem. Back when I wrote it, I was a relatively new programmer. I'm still not an expert, obviously, or I would have understood 'DialogResult' as I do now. Thanks to Ralf's patient explanation that it was an 'Enum' caused me to re-study its use. If I understood it back then as I do now, I would have written myMB much differently.

All that said, and while I am past my programming issue, my question as to why I'm seeing what I'm seeing remains.

Originally, and still now, using 'Shadows' with DialogResult I was/am able to assign integer values to DialogResult without Errors or Exceptions occuring in development or at Runtime. But at runtime in the class using myMB I cannot use DialogResult as expected.

So I wrote a Test Class to demonstrate this dilemna. In doing so, I taught myself what is, apparently, the correct way to use DialogResult. Commented out in the listing are various optional 'correct' values for DialogResult (as the Enum it was designed to be). The commented code represents my learning curve towards proper use of DialogResult.

Note: The Test Class is a form with a single button in it.

But using 'Shadows DialogResult as Integer = 0' in the Test Class (and in my original code) compiles/runs and ~should~ therefore work, yes? But it does not work as expected. Note: Using 'Shadows' and unCommenting those LOC ~does~ cause each of the Commented LOC to show and error....

Here below is the code for the Test Class. Below it, is code which uses that class and demonstrates the dilema.

My Public variable 'Response' (e.g., myMB.Response) gives me back an integer value set to the value of the button pushed in myMB. This works as expected. However, when DialogResult is 'Shadowed' to become an integer (w/o error), DialogResult is allowed to exist as ~~both~~ an Enum Value and as an integer in the using class. Despite this, when the code is stepped through in debug mode it does not stop at any of the offered If/ElseIf's where it seems as though it should stop, but only stops when it gets to the final ElseIf.

Can anyone explain what is happening... and not happening?

**Test Class Code**

VB.NET
Option Strict On
Option Explicit On
Imports System.Windows.Forms

Public Class frmMyMBDilema
    Public Response As Integer = 999
    Shadows DialogResult As Integer = 0

    Private Sub frmMyMBDilema_Load(sender As Object, e As EventArgs) Handles Me.Load
        'btnDialogResultDilema.DialogResult = DialogResult.OK
        'btnDialogResultDilema.DialogResult = DialogResult.No
        'btnDialogResultDilema.DialogResult = DialogResult.Cancel
        'btnDialogResultDilema.DialogResult = DialogResult.Abort
        'btnDialogResultDilema.DialogResult = DialogResult.Ignore
        'btnDialogResultDilema.DialogResult = DialogResult.None
        'btnDialogResultDilema.DialogResult = DialogResult.Yes
        'btnDialogResultDilema.DialogResult = DialogResult.Retry
    End Sub

    Private Sub btnDialogResultDilema_Click(sender As Object, e As EventArgs) Handles btnDialogResultDilema.Click
        Response = 1
        DialogResult = 1
        Me.Close()
    End Sub

    Public Sub ShowIt()
        Me.ShowDialog()
    End Sub

End Class


**Code That Uses the Above Class**

VB
Dim myMBTest As New frmMyMBDilema
myMBTest.ShowIt()
If myMBTest.DialogResult = DialogResult.OK Then
	Stop
ElseIf CType(myMBTest.DialogResult, Integer) = 1 Then
    Stop
ElseIf myMBTest.DialogResult = DialogResult.Yes Then
    Stop
ElseIf myMBTest.DialogResult = DialogResult.OK Then
	Stop
ElseIf myMBTest.Response = 1 Then
    Stop
End If


What I have tried:

1) Written test code elsewhere in the same Sub
2) Deleted and rewrote the initial offending code
3) Wrote simple test code
4) Closed this program within Visual Studio and reloaded it
5) ReCompiled the DLL within which my Message Box exists
6) Closed Visual Studio and reloaded it and the program
7) Shut down my computer. The reloaded it, VS and the program
8) Googled for answers and (not surprisingly) no directly related search results were found
Posted
Updated 5 days ago
v5
Comments
Ralf Meier 2-Oct-24 17:04pm    
I don't know if your Problem still exists - but if you should do the following to get help :
- show the code (not a Screenshot) which shows how the DialogResult is build inside your Messagebox
- show the code (not a Screenshot) which shows how the DialogResult is evaluated inside the calling code
- DialogResult is an Enum and not an Integer - correct that
- what DialogResult do you get inside the calling code
and last (perhaps not least) - don't post your answer as a Solution - use the "improve question" widget to complete your original question ... AND ... if you want to give someone an answer (perhaps to Dave) use the "Question or Comment" widget to Reply to his statement (in that case he is notified and perhaps would react)
Ralf Meier yesterday    
what DialogResult do you (really) get inside the calling code ???
If you want to get help you should answer this questions - I don't have your code and can't test it ... but you have the DEBUGGER - you could see what really happens !!!
brownpeteg 21hrs ago    
Ralf, Thanks for your willingness to continue trying to help! But please re-read my Question and especially my revision to it (the "Improved Question"). I have added code which exactly matches the results I see within my own 'myMB.' And this code results, through the debugger, exactly what I see when I use myMB. As I mentioned, I now have (and always had) a work around to the issue I see with 'DialogResult' however, the issue still exists and I am trying to understand why it exists. It is simply this - when I declare "Shawdows DialogResult as Integer," the code compiles and runs without an exception or any complaint whatsoever. And looking at the Debugger, usage of myMB returns DialogResult as equal to the integer value of '1' as witnessed in the Debugger (and not as an Enum) and yet the code does not Stop at the appropriate LOC in the usage code (see the usage code I've inserted above). That's it, it compiles correctly, returns the correct value, but the code does not recognize the integer value of 1 as 1. The issue, to me anyway, seems to be systemic, not usage or logic based.

1 solution

You said you made your own MessageBox. OK, but why does it seem you're returning arbitrary integer values and not the DialogResult enum values you should be returning?

The integer 1 you're returning is DialogResult.OK. So, why are you comparing to an evil "magic number"?

The code is comparing properly and executing correctly. I guarantee it. The problem is in the code you're writing and your understanding of it.

What does the code look like that is setting the DialogResult? What does the rest of the code look like that's creating the dialog, showing it, and getting the DialogResult?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900