• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Events handling query

I currently havea form with one button. The form contains the code below. The code displays up to 5 message boxes each with OK Cancel buttons.
If the user clicks OK, the button NEXT msg is shown.
If the user clicks Cancel, then no more msgs appear.
Finally, the "The End" message is shown.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If MessageBox.Show("Blah Blah One.", "", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.Cancel Then
        ElseIf MessageBox.Show("Blah Blah Two.", "", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.Cancel Then
        ElseIf MessageBox.Show("Blah Blah Three.", "", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.Cancel Then
        ElseIf MessageBox.Show("Blah Blah Four.", "", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.Cancel Then
        End If
        MessageBox.Show("The End")
End Sub

The code is not easy to read.understand. Each line has too much code related to the result of the messagebox.

I would prefer to have something like the following:
        MessageBox.Show("Blah Blah One. ")
        MessageBox.Show("Blah Blah Two. ")
        MessageBox.Show("Blah Blah Three. ")
        MessageBox.Show("Blah Blah Four. ")
        MessageBox.Show("Blah Blah Five. ")

        ' if any of the MessageBoxes above ever returns DialogResult.Cancel
        ' then jump to this spot.
        MessageBox.Show("The End")

Is there a way to do this???

Thanks
Stu

0
stuart_dent
Asked:
stuart_dent
  • 2
  • 2
1 Solution
 
adam_pedleyCommented:
Well this way is going to add more code but is easier to read, im not sure if there is an easier way to do this

Public Sub Load()

        Dim messages() As String = New String() {"Message1", "Message2"}

        For Each msg As String In messages
            If Not DisplayMessage(msg) Then
                Exit For
            End If
        Next

    End Sub

    Public Function DisplayMessage(ByVal message As String) As Boolean

        If MessageBox.Show(message, "", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.Cancel) then
            Return False
        Else : Return True
        End If

    End Function

But before you  implement that I would consider revising your GUI, as an application that performs 5 checks before proceeding will be quite annoying for users

Maybe checkboxes that need to be checked before the continue button is enabled?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
(I like adams approach and comments...)

Otherwise, you could just restructure it a bit?

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If MessageBox.Show("Blah Blah One.", "", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
            If MessageBox.Show("Blah Blah Two.", "", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
                If MessageBox.Show("Blah Blah Three.", "", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
                    If MessageBox.Show("Blah Blah Four.", "", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
                        ' All selections were "OK"
                    End If
                End If
            End If
        End If
        MessageBox.Show("The End")
    End Sub

Not that much of an improvement though!
0
 
adam_pedleyCommented:
Sorry, a bit off topic

Hi Idle_mind, its being a while since we crossed paths in a question. Congrats on the MVP.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hi adam...good to see you again and thanks!  The MVP took me completely by surprise.  To be honest, I really didn't know what it was and how few get selected for it before I received that award.  =)
0
 
stuart_dentAuthor Commented:
Thanks for the input guys.

I'm sorry I lied a bit Adam. I have a few additional lines to construct each of the messages before I display them. The message is quite different each time so it's not something that easily fits into a for next loop. (Although you have got me wondering about making my own message objects which I add to your suggested arraylist).

I've actually done the code below since writing my first message.
It uses exception handling.
It's OK but clicking the cancel isn't really an exception.
I just want some way of handling the 'event??' that the user chose the cancel option of the messagebox without have to rewrite all those iF statements.

Any suggestions???
 
Try
   MyShowMsg("one")   'my actual code for the message text is about 3-5 lines of actual code
   MyShowMsg("two")  
   MyShowMsg("three")
Catch ex As Exception
   MsgBox("Ahh you pressed cancel")
End Try
MyShowMsg("The End")

Public Sub MyShowMsg(ByVal str As String)
        Dim result As DialogResult
        result =MessageBox.Show(vstr)
        If result  = Windows.Forms.DialogResult.Cancel Then
            Throw New ArgumentException
        End If
End Sub
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now