Custom DialogResult

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
If i want the DialogResult to say something other than Yes and No, like "I am great" on the button. Do i need to create a custom messageBox class?
 
You can set a button's DialogResult property to whatever you want it to return, doesn't matter what the button's text property is set to.

Also, you can make your own ShowDialog function and return whatever you want, doesn't need to be a DialogResult either, you can make your own Enum.
 
Other than None, there are seven different DialogResult values. Under what circumstances would you need more than that? Each of those values has no specific meaning; they are just numbers. You can use any of them in any situation you want. Obviously the labels indicate what they are intended to be used for but they are not limited to that. For instance, you could have a dialogue with OK and Cancel Buttons but have ShowDialog return Abort and Ignore for those Buttons respectively. As long as you test for the those values and perform the appropriate action based on the Button that was clicked then your application will work as it should. The user will have no knowledge at all about what DialogResult values you used.

So, exactly what do you think that you can achieve with an extra DialogResult value that you can't do with the current seven that are available?
 
What i am saying is, i want the button to say something other than Yes and No. I want to change the button text of the messagebox to any text i want. Instead of asking, do you want to replace this file, Yes, No, Cancel. yes for replace, no for make duplicate files, cancel to exit. The button text could say, Replace, Duplicate, and Cancel
 
So, in actual fact, this question has nothing whatsoever to do with DialogResult values and is actually about the text on the buttons of a message box. In that case, you simply need to create your own form and call its ShowDialog method. The DialogResult values you attribute to each Button are not irrelevant.
 
can i send arguments in toe showdialog?
No, but why would you need to? If you want to pass data to the form when you create it then you do as you do for all objects: pass arguments to the constructor or else set properties or call methods and pass arguments. A form is just an object like any other so you treat it like any other object.

If you want to mimic the MessageBox class and its Show method then you need to be aware that MessageBox is NOT a form. It's a class that creates a form internally. You could create such a class with its own Show methods and allow button text to be passed as additional arguments.
 
can i send arguments in toe showdialog?
If you really want this kind of flexibility you can always make your own form and with the buttons you want and all the info as you see fit, then just use the DialogResult however you want. If you want simplicity when using your own dialog window you can wrap it in a NonInheritable class giving it the shared functions with the different signatures you want, just like the MessageBox class and the different parameters it has for Show():
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text

Namespace JBDialogs

    Public NotInheritable Class FileConfirmDialog

        Public Shared Function Show(ParentForm As Form, FileName As String, SourceFolder As String, DestinationFolder As String) As DupFileResult
            Return Show(ParentForm, FileName, SourceFolder, DestinationFolder, "MM/dd/yyyy hh:mm tt")
        End Function

        Public Shared Function Show(ParentForm As Form, FileName As String, SourceFolder As String, DestinationFolder As String, DatePattern As String) As DupFileResult
            Dim Output As DupFileResult = DupFileResult.Cancel

            Using frm As New ConfirmForm With {.DateTimePattern = DatePattern}
                Select Case frm.ShowDialog(ParentForm, FileName, SourceFolder, DestinationFolder)
                    Case DialogResult.Yes : Output = DupFileResult.Overwrite
                    Case DialogResult.No : Output = DupFileResult.Duplicate
                    Case Else : Output = DupFileResult.Cancel
                End Select
            End Using

            Return Output
        End Function

#Region " Dialog Window "

        Private Class ConfirmForm
            Inherits System.Windows.Forms.Form

#Region " Form Controls "

            Private WithEvents myCancelButton As System.Windows.Forms.Button
            Private WithEvents DuplicateButton As System.Windows.Forms.Button
            Private WithEvents OverwriteButton As System.Windows.Forms.Button
            Private WithEvents SourceFileGroupBox As System.Windows.Forms.GroupBox
            Private WithEvents SourceSizeLabel As System.Windows.Forms.Label
            Private WithEvents DestinationGroupBox As System.Windows.Forms.GroupBox
            Private WithEvents DestSizeLabel As System.Windows.Forms.Label
            Private WithEvents DuplicateLabel As System.Windows.Forms.Label
            Private WithEvents SourceModifiedLabel As System.Windows.Forms.Label
            Private WithEvents SourceLocationLabel As System.Windows.Forms.Label
            Private WithEvents FileNameLabel As System.Windows.Forms.Label
            Private WithEvents DestModifiedLabel As System.Windows.Forms.Label
            Private WithEvents DestLocationLabel As System.Windows.Forms.Label

            Private components As System.ComponentModel.IContainer
#End Region

            <DllImport("Shlwapi.dll", CharSet:=CharSet.Auto)> _
            Public Shared Function StrFormatByteSize(fileSize As Long, <MarshalAs(UnmanagedType.LPTStr)> buffer As StringBuilder, bufferSize As Integer) As Long
            End Function

            Private m_UserChoice As DialogResult = DialogResult.Cancel

            Public Overloads Function ShowDialog(Frm As Form, FileName As String, SourceFolder As String, DestFolder As String) As DialogResult
                Dim SrcFile As String = Path.Combine(SourceFolder, FileName)
                Dim DestFile As String = Path.Combine(DestFolder, FileName)
                If File.Exists(SrcFile) AndAlso File.Exists(DestFile) Then

                    Me.Text = String.Format("Duplicate File: {0}", FileName)

                    Dim SrcFI As New FileInfo(SrcFile)
                    Dim DestFI As New FileInfo(DestFile)

                    FileNameLabel.Text = FileName

                    SourceLocationLabel.Text = SourceFolder
                    DestLocationLabel.Text = DestFolder

                    SourceSizeLabel.Text = String.Format(SourceSizeLabel.Text, StrFormatByteSize(SrcFI.Length))
                    DestSizeLabel.Text = String.Format(DestSizeLabel.Text, StrFormatByteSize(DestFI.Length))

                    SourceModifiedLabel.Text = String.Format(SourceModifiedLabel.Text, SrcFI.LastWriteTime.ToString(Me.DateTimePattern))
                    DestModifiedLabel.Text = String.Format(DestModifiedLabel.Text, DestFI.LastWriteTime.ToString(Me.DateTimePattern))

                    Me.ShowDialog(Frm)
                    Return m_UserChoice
                Else
                    Return DialogResult.Cancel
                End If
            End Function

            Public Property DateTimePattern As String = "MM/dd/yyyy hh:mm tt"

            Private Sub OverwriteButton_Click(sender As System.Object, e As System.EventArgs) Handles OverwriteButton.Click
                m_UserChoice = DialogResult.Yes
                Me.Close()
            End Sub

            Private Sub DuplicateButton_Click(sender As System.Object, e As System.EventArgs) Handles DuplicateButton.Click
                m_UserChoice = DialogResult.No
                Me.Close()
            End Sub

            Private Sub myCancelButton_Click(sender As System.Object, e As System.EventArgs) Handles myCancelButton.Click
                Me.Close()
            End Sub

            Public Shared Function StrFormatByteSize(filesize As Long) As String
                Dim sb As New StringBuilder(11I)
                StrFormatByteSize(filesize, sb, sb.Capacity)
                Return sb.ToString()
            End Function

            Public Sub New()
                InitializeComponent()
            End Sub

            'Form overrides dispose to clean up the component list.
            <System.Diagnostics.DebuggerNonUserCode()> _
            Protected Overrides Sub Dispose(ByVal disposing As Boolean)
                Try
                    If disposing AndAlso components IsNot Nothing Then
                        components.Dispose()
                    End If
                Finally
                    MyBase.Dispose(disposing)
                End Try
            End Sub

#Region " InitializeComponent "

            <System.Diagnostics.DebuggerStepThrough()> _
            Private Sub InitializeComponent()
                Me.myCancelButton = New System.Windows.Forms.Button()
                Me.DuplicateButton = New System.Windows.Forms.Button()
                Me.OverwriteButton = New System.Windows.Forms.Button()
                Me.DuplicateLabel = New System.Windows.Forms.Label()
                Me.SourceFileGroupBox = New System.Windows.Forms.GroupBox()
                Me.SourceModifiedLabel = New System.Windows.Forms.Label()
                Me.SourceSizeLabel = New System.Windows.Forms.Label()
                Me.SourceLocationLabel = New System.Windows.Forms.Label()
                Me.FileNameLabel = New System.Windows.Forms.Label()
                Me.DestinationGroupBox = New System.Windows.Forms.GroupBox()
                Me.DestModifiedLabel = New System.Windows.Forms.Label()
                Me.DestSizeLabel = New System.Windows.Forms.Label()
                Me.DestLocationLabel = New System.Windows.Forms.Label()
                Me.SourceFileGroupBox.SuspendLayout()
                Me.DestinationGroupBox.SuspendLayout()
                Me.SuspendLayout()
                '
                'myCancelButton
                '
                Me.myCancelButton.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
                Me.myCancelButton.Location = New System.Drawing.Point(313, 218)
                Me.myCancelButton.Name = "myCancelButton"
                Me.myCancelButton.Size = New System.Drawing.Size(75, 23)
                Me.myCancelButton.TabIndex = 6
                Me.myCancelButton.Text = "Cancel"
                Me.myCancelButton.UseVisualStyleBackColor = True
                '
                'DuplicateButton
                '
                Me.DuplicateButton.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
                Me.DuplicateButton.Location = New System.Drawing.Point(209, 218)
                Me.DuplicateButton.Name = "DuplicateButton"
                Me.DuplicateButton.Size = New System.Drawing.Size(75, 23)
                Me.DuplicateButton.TabIndex = 5
                Me.DuplicateButton.Text = "Duplicate"
                Me.DuplicateButton.UseVisualStyleBackColor = True
                '
                'OverwriteButton
                '
                Me.OverwriteButton.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
                Me.OverwriteButton.Location = New System.Drawing.Point(113, 218)
                Me.OverwriteButton.Name = "OverwriteButton"
                Me.OverwriteButton.Size = New System.Drawing.Size(75, 23)
                Me.OverwriteButton.TabIndex = 4
                Me.OverwriteButton.Text = "Overwrite"
                Me.OverwriteButton.UseVisualStyleBackColor = True
                '
                'DuplicateLabel
                '
                Me.DuplicateLabel.AutoSize = True
                Me.DuplicateLabel.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                Me.DuplicateLabel.Location = New System.Drawing.Point(12, 9)
                Me.DuplicateLabel.Name = "DuplicateLabel"
                Me.DuplicateLabel.Size = New System.Drawing.Size(346, 16)
                Me.DuplicateLabel.TabIndex = 0
                Me.DuplicateLabel.Text = "There is already a file with the same name in this location."
                '
                'SourceFileGroupBox
                '
                Me.SourceFileGroupBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
                Me.SourceFileGroupBox.Controls.Add(Me.SourceModifiedLabel)
                Me.SourceFileGroupBox.Controls.Add(Me.SourceSizeLabel)
                Me.SourceFileGroupBox.Controls.Add(Me.SourceLocationLabel)
                Me.SourceFileGroupBox.Location = New System.Drawing.Point(12, 51)
                Me.SourceFileGroupBox.Name = "SourceFileGroupBox"
                Me.SourceFileGroupBox.Size = New System.Drawing.Size(376, 75)
                Me.SourceFileGroupBox.TabIndex = 2
                Me.SourceFileGroupBox.TabStop = False
                Me.SourceFileGroupBox.Text = "Source File Info"
                '
                'SourceModifiedLabel
                '
                Me.SourceModifiedLabel.AutoSize = True
                Me.SourceModifiedLabel.Location = New System.Drawing.Point(6, 54)
                Me.SourceModifiedLabel.Name = "SourceModifiedLabel"
                Me.SourceModifiedLabel.Size = New System.Drawing.Size(67, 13)
                Me.SourceModifiedLabel.TabIndex = 2
                Me.SourceModifiedLabel.Text = "Modified: {0}"
                '
                'SourceSizeLabel
                '
                Me.SourceSizeLabel.AutoSize = True
                Me.SourceSizeLabel.Location = New System.Drawing.Point(6, 36)
                Me.SourceSizeLabel.Name = "SourceSizeLabel"
                Me.SourceSizeLabel.Size = New System.Drawing.Size(47, 13)
                Me.SourceSizeLabel.TabIndex = 1
                Me.SourceSizeLabel.Text = "Size: {0}"
                '
                'SourceLocationLabel
                '
                Me.SourceLocationLabel.AutoSize = True
                Me.SourceLocationLabel.Location = New System.Drawing.Point(6, 18)
                Me.SourceLocationLabel.Name = "SourceLocationLabel"
                Me.SourceLocationLabel.Size = New System.Drawing.Size(75, 13)
                Me.SourceLocationLabel.TabIndex = 0
                Me.SourceLocationLabel.Text = "{File Location}"
                '
                'FileNameLabel
                '
                Me.FileNameLabel.AutoSize = True
                Me.FileNameLabel.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                Me.FileNameLabel.Location = New System.Drawing.Point(12, 28)
                Me.FileNameLabel.Name = "FileNameLabel"
                Me.FileNameLabel.Size = New System.Drawing.Size(77, 16)
                Me.FileNameLabel.TabIndex = 1
                Me.FileNameLabel.Text = "{FileName}"
                '
                'DestinationGroupBox
                '
                Me.DestinationGroupBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
                Me.DestinationGroupBox.Controls.Add(Me.DestModifiedLabel)
                Me.DestinationGroupBox.Controls.Add(Me.DestSizeLabel)
                Me.DestinationGroupBox.Controls.Add(Me.DestLocationLabel)
                Me.DestinationGroupBox.Location = New System.Drawing.Point(12, 132)
                Me.DestinationGroupBox.Name = "DestinationGroupBox"
                Me.DestinationGroupBox.Size = New System.Drawing.Size(376, 75)
                Me.DestinationGroupBox.TabIndex = 3
                Me.DestinationGroupBox.TabStop = False
                Me.DestinationGroupBox.Text = "Destination File Info"
                '
                'DestModifiedLabel
                '
                Me.DestModifiedLabel.AutoSize = True
                Me.DestModifiedLabel.Location = New System.Drawing.Point(6, 54)
                Me.DestModifiedLabel.Name = "DestModifiedLabel"
                Me.DestModifiedLabel.Size = New System.Drawing.Size(67, 13)
                Me.DestModifiedLabel.TabIndex = 2
                Me.DestModifiedLabel.Text = "Modified: {0}"
                '
                'DestSizeLabel
                '
                Me.DestSizeLabel.AutoSize = True
                Me.DestSizeLabel.Location = New System.Drawing.Point(6, 36)
                Me.DestSizeLabel.Name = "DestSizeLabel"
                Me.DestSizeLabel.Size = New System.Drawing.Size(47, 13)
                Me.DestSizeLabel.TabIndex = 1
                Me.DestSizeLabel.Text = "Size: {0}"
                '
                'DestLocationLabel
                '
                Me.DestLocationLabel.AutoSize = True
                Me.DestLocationLabel.Location = New System.Drawing.Point(6, 18)
                Me.DestLocationLabel.Name = "DestLocationLabel"
                Me.DestLocationLabel.Size = New System.Drawing.Size(75, 13)
                Me.DestLocationLabel.TabIndex = 0
                Me.DestLocationLabel.Text = "{File Location}"
                '
                'ConfirmForm
                '
                Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
                Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
                Me.ClientSize = New System.Drawing.Size(400, 253)
                Me.Controls.Add(Me.DestinationGroupBox)
                Me.Controls.Add(Me.FileNameLabel)
                Me.Controls.Add(Me.SourceFileGroupBox)
                Me.Controls.Add(Me.DuplicateLabel)
                Me.Controls.Add(Me.OverwriteButton)
                Me.Controls.Add(Me.DuplicateButton)
                Me.Controls.Add(Me.myCancelButton)
                Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
                Me.MaximizeBox = False
                Me.MinimizeBox = False
                Me.Name = "ConfirmForm"
                Me.ShowIcon = False
                Me.ShowInTaskbar = False
                Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide
                Me.Text = "ConfirmForm"
                Me.SourceFileGroupBox.ResumeLayout(False)
                Me.SourceFileGroupBox.PerformLayout()
                Me.DestinationGroupBox.ResumeLayout(False)
                Me.DestinationGroupBox.PerformLayout()
                Me.ResumeLayout(False)
                Me.PerformLayout()

            End Sub

#End Region

        End Class

#End Region

    End Class

    Public Enum DupFileResult
        Overwrite = 2I
        Duplicate = 1I
        Cancel = 0I
    End Enum

End Namespace

DupFile.png
Just an example dialog I pulled from one of my old projects.
 

Attachments

  • FileConfirmDialog.vb.txt
    16 KB · Views: 43
Hi everyone. I am new on this forum.
I want to create custom Form but i dont want use the designer of VS2010 express.
So i loaded your code in my projet.

So.. how i can use it ? I want to learn and it is a good example for learn. :)

but i dont know how to use it. (I added Imports System.Windows.Forms in your vb.)

in my project :
Dim na As New ConfirmForm()
na.ShowDialog(Me.ParentForm, "1.txt", "c:\", "c:\")

Works fine. i used me.parentform of my Form. but how i can use it if i dont have a form on start. (I use VB.net on Autocad.)
I dont know the best first parameter of your ShowDialog





thanks.
 
Last edited:
Hi everyone. I am new on this forum.
I want to create custom Form but i dont want use the designer of VS2010 express.
So i loaded your code in my projet.

So.. how i can use it ? I want to learn and it is a good example for learn. :)

but i dont know how to use it. (I added Imports System.Windows.Forms in your vb.)

in my project :
Dim na As New ConfirmForm()
na.ShowDialog(Me.ParentForm, "1.txt", "c:\", "c:\")

Works fine. i used me.parentform of my Form. but how i can use it if i dont have a form on start. (I use VB.net on Autocad.)
I dont know the best first parameter of your ShowDialog





thanks.
I used the VS form designer to make that confirm form, then I moved the code from the Form.Designer.vb file into the Form.vb file so it's all together, then built the class around it to use the form rather than let anything use it.

In order to use the Me.ParentForm, be sure to make sure it's not set to Nothing:
If Me.ParentForm IsNot Nothing Then
...
End If
 
Back
Top