-
Jul 18th, 2024, 08:42 AM
#1
Thread Starter
Junior Member
Adjusting Code
I've been looking for a way to create a MessageBox dialog with custom buttons (vs. the usual Yes/No/Cancel, etc.) verbiage. As has been amply stated, the stock MessageBox will not do this, leaving us to come up with substitutes. There are plenty of solutions out there and I actually found one on StackOverflow that works really well save a few cosmetics.
Here is the code from the above post:
Code:
Public Module CustomMessageBox
Private result As String
Public Function Show(options As IEnumerable(Of String), Optional message As String = "", Optional title As String = "") As String
result = "Cancel"
Dim myForm As New Form With {.Text = title}
Dim tlp As New TableLayoutPanel With {.ColumnCount = 1, .RowCount = 2}
Dim flp As New FlowLayoutPanel()
Dim l As New Label With {.Text = message}
myForm.Controls.Add(tlp)
tlp.Dock = DockStyle.Fill
tlp.Controls.Add(l)
l.Dock = DockStyle.Fill
tlp.Controls.Add(flp)
flp.Dock = DockStyle.Fill
For Each o In options
Dim b As New Button With {.Text = o}
flp.Controls.Add(b)
AddHandler b.Click,
Sub(sender As Object, e As EventArgs)
result = DirectCast(sender, Button).Text
myForm.Close()
End Sub
Next
myForm.FormBorderStyle = FormBorderStyle.FixedDialog
myForm.Height = 100
myForm.ShowDialog()
Return result
End Function
End Module
Code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim result = CustomMessageBox.Show(
{"Right", "Left"},
"Do you want to go right or left?",
"Confirm Direction")
MessageBox.Show(result)
End Sub
End Class
The cosmetic issues are:
- The buttons end up on the left side of the dialog instead of the right, which I fixed by changing flp.Dock = DockStyle.Fill to flp.Dock = DockStyle.Right.
- The dialog box has next to no padding; it's substantially more compact than a standard MessageBox.
- There is only enough room for one line of text, and it does not wrap around; I am looking to drop dynamically generated content here.
I have tried inserting various properties to increase the padding to get the same appearance as a MessageBox, but it didn't work as expected. As for increasing the room for the text, I've also been unsuccessful at creating more space in the top row of the dialog; I can get the whole dialog to grow height-wise, but not the row. Obviously I'm doing the wrong thing in the wrong place. As for the label object in the top row: I'll assume that if I give the label object a set width, AutoSize will increase the height. That height can then be applied to the top row in the dialog as well (i.e. LabelObjectHeight+PaddingValue=RowHeight). However, I'm not sure where or how to do that (all of this is scraping the limits of my VB.net knowhow, which has been acquired on-the-fly.
And just to explain the dynamic text: I present the user with a few prompts where a Yes/No[/Cancel] format just doesn't work as well. For example, right now I'm using the following:
Due to the implications of each route, I would much rather prompt the following instead of "Do you want to exclude this file?": 'frame_0070.jpg' is classified as a Blank Frame. Do you wish to include
it in the Image Library, or exclude it?"
[Include] [Exclude]
That's because if a flag is attached to the instance, there may be a third option (Defer). It's a lot more efficient and easier to throw the requisite number of buttons into a reusable function using custom button text and CASEing the results, than to code multiple prompts and handlers. To that end I tweaked the way the Button1.Click event calls the function so a button list is a variable instead of hard-coded, which in turn gives me the freedom to put as many buttons as I want in the dialog (but I'm not a sadomasochist and cap that count at 4... LOL). All in all, this works beautifully except for the sizing part.
This example prompt includes a filename, and its length can vary. Therefore the dialog needs to be able to size the same way MessageBox does. Can anyone tell me the width of a standard MessageBox so I can code it in?
Of course, if Micro$oft allowed us to customize MessageBox button texts we wouldn't be having this conversation...
Last edited by Erlkoenig; Jul 18th, 2024 at 10:57 AM.
-
Jul 18th, 2024, 09:48 AM
#2
Re: Adjusting Code
A messagebox is just a form. You can make a form that looks however you want within the limits of your artistic ability, creativity...and to some extent the geometry, so long as you stick with WinForms. The replacement you found is one possible way to do this, but there are as many different ways to do this as there are forms you can design.
So, what is it about a messagebox that gives it the "messagebox...ness" in your view? Is it necessary that you can have a function to display it? The buttons? The text? All of those are easy enough to replicate, but if you start trying to fiddle with every piece of it, then you'd be better off just making a class that people can customize however they choose as far as width, font, and behavior of text, number of buttons, any other control they might need, such as a picturebox to display an icon, or something else. Of course, that's just a form.
The whole point of messagebox is that you are trading off versatility for ease of use. Add in all the versatility, and the ease of use is going to go away. So what? Let it go. Make an interface that does what you want it to do, don't feel that you have to make it a certain way because Microsoft only added certain built in tools.
My usual boring signature: Nothing
-
Jul 18th, 2024, 10:03 AM
#3
Re: Adjusting Code
Here’s a very simple example for creating and using your own custom dialog…
http://www.scproject.biz/Using%20Dialogs.php#bm11
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 18th, 2024, 10:20 AM
#4
-
Jul 18th, 2024, 11:01 AM
#5
Thread Starter
Junior Member
Re: Adjusting Code
Originally Posted by Shaggy Hiker
A messagebox is just a form. You can make a form that looks however you want within the limits of your artistic ability, creativity...and to some extent the geometry, so long as you stick with WinForms. The replacement you found is one possible way to do this, but there are as many different ways to do this as there are forms you can design.
Right, and what I posted is the example that works best for me, except for the cosmetic parts I mentioned, which are the ones I'm trying to fix.
-
Jul 18th, 2024, 11:03 AM
#6
Thread Starter
Junior Member
Re: Adjusting Code
Originally Posted by .paul.
I did try this one; didn't work as well as the one I'm using...
-
Jul 18th, 2024, 11:10 AM
#7
Thread Starter
Junior Member
Re: Adjusting Code
Originally Posted by PlausiblyDamp
I don't know that custom button text is allowed, which is all I need. But also: Reading the docs it says C/C# required. I don't know either...
-
Jul 18th, 2024, 01:19 PM
#8
Re: Adjusting Code
https://www.vbforums.com/showthread....-to-MessageBox shows how you can do things like customise the button text etc.
Although the samples are in C# there is absolutely no reason why you can't use it from VB.Net though.
-
Jul 18th, 2024, 01:53 PM
#9
Re: Adjusting Code
This is fairly versatile...
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim result As String = CustomMessageBox.Show( _
New String() {"Left", "Right"}, _
"Do you want to go right or left?", _
"Confirm Direction")
MessageBox.Show(result)
End Sub
End Class
Code:
Public Module CustomMessageBox
Private result As String
Private frm As Form
Private tlp As TableLayoutPanel
Public Function Show(ByVal options As IEnumerable(Of String), Optional ByVal message As String = "", Optional ByVal title As String = "") As String
result = "Cancel"
frm = New Form With {.Text = title}
tlp = New TableLayoutPanel
tlp.RowStyles.Add(New RowStyle(SizeType.Percent, 100))
tlp.RowStyles.Add(New RowStyle(SizeType.Absolute, 6))
tlp.RowStyles.Add(New RowStyle(SizeType.Absolute, 28))
tlp.RowStyles.Add(New RowStyle(SizeType.Absolute, 12))
tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 6))
tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 100))
Dim colSum As Integer = 12 + 75 + 6
Dim colIndex As Integer = 2
For Each o In options
tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 100))
colSum += tlp.ColumnStyles(colIndex).Width + 6
Dim b As New Button With {.Text = o}
tlp.Controls.Add(b, colIndex, 2)
tlp.SetColumnSpan(b, 1)
tlp.SetRowSpan(b, 1)
b.Dock = DockStyle.Fill
colIndex += 1
AddHandler b.Click, AddressOf b_Click
Next
tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 12))
Dim leftPanel As New Panel
tlp.Controls.Add(leftPanel, 0, 0)
tlp.SetRowSpan(leftPanel, 3)
leftPanel.Dock = DockStyle.Fill
Dim rightPanel As New Panel
tlp.Controls.Add(rightPanel, tlp.ColumnStyles.Count - 1, 0)
tlp.SetRowSpan(rightPanel, 3)
rightPanel.Dock = DockStyle.Fill
Dim bottomPanel As New Panel
tlp.Controls.Add(bottomPanel, 0, 3)
tlp.SetColumnSpan(bottomPanel, tlp.ColumnStyles.Count)
bottomPanel.Dock = DockStyle.Fill
Dim l As New Label With {.Text = message, .TextAlign = ContentAlignment.MiddleCenter}
frm.Controls.Add(tlp)
tlp.Dock = DockStyle.Fill
tlp.Controls.Add(l, 0, 0)
tlp.SetColumnSpan(l, 5)
l.Dock = DockStyle.Fill
frm.FormBorderStyle = FormBorderStyle.FixedDialog
frm.Height = 200
frm.Width = colSum + 12
If frm.ShowDialog() = DialogResult.OK Then
Return result
End If
Return ""
End Function
Public Sub b_Click(ByVal sender As Object, ByVal e As EventArgs)
result = DirectCast(sender, Button).Text
frm.DialogResult = DialogResult.OK
End Sub
End Module
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 18th, 2024, 04:29 PM
#10
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 19th, 2024, 11:47 AM
#11
Re: Adjusting Code
See my GitHub repository which has several working samples.
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
|