Results 1 to 11 of 11

Thread: VB.NET Proper Use of MsgBoxResult.Yes/No

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    VB.NET Proper Use of MsgBoxResult.Yes/No

    Hi All.. Looking for the 'proper' way to use the msgboxresult.YES/NO here in my case. I am either using openfiledialog to browse or I am loading the most recent file. After that, I am loading a bunch of textboxes from a file. I just can't determine how this needs to be so that I am not duplicating all of my textbox entries based on the YES or NO. Not sure if I am even writing it correctly. Having a serious brain-block here. I hope I am making sense... Thanks for any help.


    Code:
    Sub SelectFile()
    
           Dim ans As Integer
            ans = MsgBox("YES: Load Recent File.  NO: Browse for File.", MsgBoxStyle.YesNo, "Load File?")
    
            If ans = MsgBoxResult.Yes Then
                Dim newestfile As String = Directory.GetFiles(fpath, "*.txt") _
                .Select(Function(x) New FileInfo(x)) _
                .OrderByDescending(Function(x) x.LastWriteTime) _
                .Select(Function(x) x.FullName) _
                .First()
    
    
    ''' somewhere if ans = no then openfiledialog?
    
                Using dialog As New OpenFileDialog
    
                    dialog.Title = "Select Text File"
                    dialog.InitialDirectory = fpath
                    dialog.Filter = "Text Files|*.txt"
    
                    If dialog.ShowDialog() = DialogResult.OK Then
    
                        Dim sr As IO.StreamReader = IO.File.OpenText(dialog.FileName)
                        Dim line As Object
                        Dim data() As Object
    
                        Do Until sr.EndOfStream
                            line = sr.ReadToEnd
                            data = line.Split(",")
    
                        ' Load All TextBoxes Here...
    
                       Loop
    
                      sr.Close()
    
                   End If
               End Using
           End If
    
     End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: VB.NET Proper Use of MsgBoxResult.Yes/No

    You’re probably better off using the newer MessageBox.ShowDialog

    Code:
    Dim result As DialogResult = MessageBox.ShowDialog(‘etc
    Code:
    If result = DialogResult.Yes Then ‘etc

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: VB.NET Proper Use of MsgBoxResult.Yes/No

    If you insist on the MsgBox…

    Code:
    If ans = MsgBoxResult.Yes Then
        ‘ execute some code here
    ElseIf ans = MsgBoxResult.No Then
        ‘ execute some other code here
    End If
    You should also note, you’re using ans as integer, which should be ans as MsgBoxresult

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: VB.NET Proper Use of MsgBoxResult.Yes/No

    I would say that you are forcing the message box to do work that it isn't suited for. After all, if the message is just instructions on how to interpret the buttons, then the buttons aren't right.

    Building a tiny form that has the buttons you'd actually like on it, is pretty easy. You'd only be creating a form with a label and whatever buttons you want. You can then make the button captions show whatever is needed such that people don't have to be told how to use them (well, in theory anyways. In practice, people don't read, but that's a different problem). In fact, you probably don't even need a label on there, as the buttons could be self explanatory.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Re: VB.NET Proper Use of MsgBoxResult.Yes/No

    Thanks @paul... I get an error using this 'ShowDialog is not a member of MessageBox'.

    ShaggyHiker - You make a good point. This add in was an after thought for simply loading the newest file, so maybe a custom tiny form would be the way to go. Too funny, you are so right, people do not read anything
    Last edited by mikeg71; Nov 28th, 2023 at 12:04 PM.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: VB.NET Proper Use of MsgBoxResult.Yes/No

    Quote Originally Posted by mikeg71 View Post
    Thanks @paul... I get an error using this 'ShowDialog is not a member of MessageBox'.
    Try MessageBo.Show(‘etc

    https://learn.microsoft.com/en-us/do...rms.messagebox

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: VB.NET Proper Use of MsgBoxResult.Yes/No

    Have a look at my custom dialog example. This is what Shaggy meant…

    http://www.scproject.biz/Using%20Dialogs.php#bm11

    A custom dialog can have as much or as little complexity as you need…

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Re: VB.NET Proper Use of MsgBoxResult.Yes/No

    Paul - Thanks a bunch for this, it will be VERY useful to me when making attempts to use this type of stuff in my applications.

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: VB.NET Proper Use of MsgBoxResult.Yes/No

    Rather than

    Code:
    Dim result As DialogResult = MessageBox.Show("Coffee (Yes) or Tea (No)", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If result = DialogResult.Yes Then
        Text = "Coffee for me"
    Else
        Text = "I love Tea"
    End If
    Consider TaskDialog.

    Simple example
    Code:
    Public Module Dialogs
    	Public Function Question(owner As Control, caption As String, heading As String, yesText As String, noText As String, defaultButton As DialogResult) As Boolean
    
    		Dim yesButton = New TaskDialogButton(yesText) With {.Tag = DialogResult.Yes}
    		Dim noButton = New TaskDialogButton(noText) With {.Tag = DialogResult.No}
    
    		Dim buttons = New TaskDialogButtonCollection
    
    		If defaultButton = DialogResult.Yes Then
    			buttons.Add(yesButton)
    			buttons.Add(noButton)
    		Else
    			buttons.Add(noButton)
    			buttons.Add(yesButton)
    		End If
    
    
    		Dim page = New TaskDialogPage() With {
    				.Caption = caption,
    				.SizeToContent = True,
    				.Heading = heading,
    				.Buttons = buttons
    				}
    
    		Dim result = TaskDialog.ShowDialog(owner, page)
    		Return CType(result.Tag, DialogResult) = DialogResult.Yes
    
    	End Function
    End Module

    Usage

    Code:
    If Question(Me, "Question", "Favorite drink", "Coffee", "Tea", DialogResult.No) Then
        Text = "Coffee for me"
    Else
        Text = "I love Tea"
    End If

    • The first parameter indicates that the dialog always show in the form rather than screen center.
    • You control button text and position



    Name:  dialog.jpg
Views: 1604
Size:  12.2 KB

  10. #10
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: VB.NET Proper Use of MsgBoxResult.Yes/No

    TaskDialog is for .net Core, is there equivalent for Framework ?
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: VB.NET Proper Use of MsgBoxResult.Yes/No

    Not to my knowledge, I use to have code written in C# usable in .NET Framework but purged it when moving to .NET Core.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width