Share via


Custom DialogResults

Question

Wednesday, November 11, 2009 11:33 AM

Can I create new dialog results for my dialog forms? And then use them with Form.ShowDialog method?

All replies (13)

Wednesday, November 11, 2009 6:46 PM ✅Answered | 1 vote

Hi again Justin,

If you are using any version of Vb.Net 2008 or 2010 then try this with one button on a FORM.

I hope you can follow the idea but I am using a 10 item ENUMERATION here.

PASTE the entire lot into a FORM code window to try it.

Regards,

John

Option Strict On
Imports System.Runtime.CompilerServices
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Set the TAG.
        Me.Tag = "4"
        'Then set the CustomDialogResult using this extension method.>>
        Me.CustomDialogResult = Me.GetMyDialogResult()

        'Check the value.>>
        If Me.CustomDialogResult = MyExtensions.CustomDialogResult.YouMustBeJoking Then

            MessageBox.Show("This code works!!")

        End If

    End Sub

    Private CustomResult As MyExtensions.CustomDialogResult

    Public Property CustomDialogResult() As MyExtensions.CustomDialogResult
        Get
            Return Me.CustomResult
        End Get
        Set(ByVal value As MyExtensions.CustomDialogResult)
            Me.CustomResult = value
        End Set
    End Property

End Class

Public Module MyExtensions

    Public Enum CustomDialogResult
        No
        Yes
        Maybe
        YouReallyWantToDoThat
        YouMustBeJoking
        FairEnough
        DoingItToday
        DoingItTomorrow
        DoingItNextWeek
        Okay
    End Enum

    <Extension()> _
    Public Function GetMyDialogResult(ByVal MyForm As System.Windows.Forms.Form) As CustomDialogResult

        Select Case MyForm.Tag.ToString
            Case "0"
                Return CustomDialogResult.No
            Case "1"
                Return CustomDialogResult.Yes
            Case "2"
                Return CustomDialogResult.Maybe
            Case "3"
                Return CustomDialogResult.YouReallyWantToDoThat
            Case "4"
                Return CustomDialogResult.YouMustBeJoking
            Case "5"
                Return CustomDialogResult.FairEnough
            Case "6"
                Return CustomDialogResult.DoingItToday
            Case "7"
                Return CustomDialogResult.DoingItTomorrow
            Case "8"
                Return CustomDialogResult.DoingItNextWeek
            Case "9"
                Return CustomDialogResult.Okay
        End Select
    End Function

End Module

I'm currently looking for work in Vb.Net software development. :-)


Wednesday, November 11, 2009 7:54 PM ✅Answered | 1 vote

John, thanks for trying out the extention methods.  Good question why it doesn't work.

The conclusion that John is coming to is what I suggested from the start.  Here's my take on it borrowing from his code.  Obviously you would split these two Form class codes into their own files...
 
 
 

Public Class Form1

    Private Sub btnRunDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRunDialog.Click

        Dim frm As New frmDialog
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            Select Case frm.SpecialDialogResult
                Case frmDialog.CustomDialogResult.Okay
                    MsgBox("You chose Okay!")
                Case Else
                    MsgBox("You Chose something other than Okay.")
            End Select
        Else
            MsgBox("You have cancelled the dialog")
        End If
    End Sub
End Class


Public Class frmDialog

    Public Enum CustomDialogResult
        No
        Yes
        Maybe
        YouReallyWantToDoThat
        YouMustBeJoking
        FairEnough
        DoingItToday
        DoingItTomorrow
        DoingItNextWeek
        Okay
    End Enum

    Private m_CustomResult As CustomDialogResult

    Public Property SpecialDialogResult() As CustomDialogResult
        Get
            Return Me.m_CustomResult
        End Get
        Set(ByVal value As CustomDialogResult)
            Me.m_CustomResult = value
        End Set
    End Property


    Private Sub btnOkay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOkay.Click
        SpecialDialogResult = CustomDialogResult.Okay
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
End Class

 
 
 


Wednesday, November 11, 2009 8:15 PM ✅Answered | 1 vote

The general way of passing information back to the caller of a dialog is through the use of properties - remember, a form is a class like any other.  Something like the following air code:

Class MyDialog
   Inherits System.Windows.Forms.Form
   
    private _myResult As MyResultEnum
    
   Public ReadOnly Property MyResult() As MyResultEnum
       Get
           return _myResult
       End Get
   End Property

   private sub ok_click(byval sender as object, byval e as eventargs)
         ' set the result
   end sub
End class


Using m As New MyDialog()
   If m.ShowDialog(Me) == DialogResult.OK AndAlso m.MyResult = MyResultEnum.WhatEver Then
        ' do cool stuff
  End If
End Using

Anyway, just an idea.Tom Shelton


Wednesday, November 11, 2009 1:27 PM

Should I assume that you do not want to just create your own enumeration and then have a property on your form that is set just before the dialog result is set?  In other words, do you need to extend DialogResult, or can you just use OK and Cancel to know if there is a "result" or not, and then use a custom enumeration property to define your specific needs?

If the answer is no then you may be able to extend the DialogResult enumeration through class extentions (.NET 3.5 and up).  I've never messed with it (still stuck on VS2005) so I'm useless for an example, but if it interests you then I'm sure someone else can provide an example.

If you are interested in my alternative approach then let me know and I'll provide an example of that.


Wednesday, November 11, 2009 4:13 PM

When I first created this thread, I was wanting to know how can I extend Windows.Forms.DialogResult. But now, I don't. If it needs .NET 3.5 and up and also it's a bit complex, then there is no need to use it. Anyway, it isn't important already.

I can take a look at your alternative example.


Wednesday, November 11, 2009 4:22 PM

Hello Justin,

I will have a go at an example extension for you, if it can indeed be extended.

Happy coding from,

Tattooed Bloke
 


Wednesday, November 11, 2009 4:26 PM | 1 vote

Hi Justin,

Here is code for a Custom Message Box I called CustomMsgBox.>>

http://social.msdn.microsoft.com/forums/en-US/vblanguage/thread/8b13784d-8cd7-40ff-b8f7-b83fbc48f179/

It could be adapted to have any text in as many buttons as you want.

Regards,

John


I'm currently looking for work in Vb.Net software development. :-)


Wednesday, November 11, 2009 5:35 PM

Hi again Justin,

When using ShowDialog you can use the default values.

Even using Extension methods I can not see any way to add any other return values other than

ABORT, CANCEL, OK, NO, NONE, YES, IGNORE, RETRY

 

 

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Simply set the Dialog Result for which ever button you
        'assign it to like this.>>
        Me.DialogResult = Windows.Forms.DialogResult.OK
        'Choose between; ABORT, CANCEL, OK, NO, NONE, YES, IGNORE, RETRY

    End Sub
End Class

Regards,

John


I'm currently looking for work in Vb.Net software development. :-)


Wednesday, November 11, 2009 5:42 PM

Hi again Justin,

When using ShowDialog you can use the default values.>>

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Simply set the Dialog Result for which ever button you
        'assign it to like this.>>
        Me.DialogResult = Windows.Forms.DialogResult.OK
        'Choose between; ABORT, CANCEL, OK, NO, NONE, YES, IGNORE, RETRY
    End Sub
End Class

Regards,

John

Thanks but why did we create enumerations if we will use same results?


Wednesday, November 11, 2009 5:52 PM

Thanks but why did we create enumerations if we will use same results?

Hi Justin,

I have deleted my earlier post showing a custom enumeration.

When posting code please use the </> and select VB.Net as the language.

Unless someone else knows differently, I can not seem to add any more keywords to the default

ABORT, CANCEL, OK, NO, NONE, YES, IGNORE, RETRY

for Windows.Forms.DialogResult , sorry.

I've even tried using extension methods.

I think someone else asked about adding color names to the Color enumeration in another thread. I think only Microsoft could do this in the next version of VB.Net.

If you want to suggest any additions please go to.>>

http://connect.microsoft.com/default.aspx

Regards,

John


Wednesday, November 11, 2009 6:09 PM

Hi again Justin,

The only work-around I can currently think of is;

Set the TAG property of a FORM based on the Button that is clicked.

So if you have 10 buttons then set the TAG to "10" or 10 if Button10 is clicked on.

Regards,

John
I'm currently looking for work in Vb.Net software development. :-)


Thursday, November 12, 2009 12:39 AM

Hi Dig-Boy,

In your last post if you just had.>>

    Private Sub btnOkay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOkay.Click

        SpecialDialogResult = CustomDialogResult.YouMustBeJoking

    End Sub

for btnOkay then frmDialog does not close and return. :-(

I have been trying to Shadow or Overload the ShowDialog Function to get a RETURNed result.

I hope you see what I'm getting at here. But the Funtion would have to wait for one of the buttons to be clicked?

    Public Shadows Function ShowDialog() As CustomDialogResult

        Return Me.SpecialDialogResult

    End Function

Regards,

John


I'm currently looking for work in Vb.Net software development. :-)


Thursday, November 12, 2009 12:51 AM

Hi Dig-Boy,

Never mind I have just realised you have to set the DialogResult property to something so that the frmDialog closes.

I guess I just made it more complicated with an extension method example.

I actually like your solution better :-)
I guess you can follow what my code does even if you don't have a 2008 or a 2010 version of Vb.Net installed to try extension methods.

By the way if you have Visual Studio 2005 installed you will be okay to also install
the Express Edition of 2008 as it installs to a separate directory.

>> http://www.microsoft.com/express/vb/Default.aspx

Regards,

John


I'm currently looking for work in Vb.Net software development. :-)