|
-
Nov 28th, 2023, 11:01 AM
#1
Thread Starter
Addicted Member
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
-
Nov 28th, 2023, 11:39 AM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 28th, 2023, 11:44 AM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 28th, 2023, 11:54 AM
#4
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
 
-
Nov 28th, 2023, 11:59 AM
#5
Thread Starter
Addicted Member
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.
-
Nov 28th, 2023, 12:25 PM
#6
Re: VB.NET Proper Use of MsgBoxResult.Yes/No
 Originally Posted by mikeg71
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 28th, 2023, 01:29 PM
#7
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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 28th, 2023, 01:50 PM
#8
Thread Starter
Addicted Member
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.
-
Nov 30th, 2023, 06:01 AM
#9
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
-
Nov 30th, 2023, 10:23 AM
#10
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)
-
Dec 1st, 2023, 05:19 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|