The msgbox widget is really useful. However the font limitations of the default msgbox can be brutal if your trying to deliver a message with considerable information. Has anyone coded an improved msgbox class that would take the same arguements + font?

Dani AI

Generated

A small custom dialog is the simplest, most flexible fix. As noted, a Form lets you control font, wrapping, icons and layout. Build a dialog that exposes a compact API so callers can request text, a button set, a caption and a Font (or a property that accepts a Font). Follow 's suggestion to inspect the style/flags you pass and show only the buttons you need.

A convenient pattern is a single Shared (static) helper that configures a disposable dialog instance and returns the caller's result. For example:

Public Shared Function ShowFancy(text As String,
                               buttons As MessageBoxButtons,
                               caption As String,
                               textFont As Font) As DialogResult
    Using dlg As New FancyMessageForm()
        dlg.MessageText = text
        dlg.CaptionText = caption
        dlg.DisplayFont = textFont
        dlg.ConfigureButtons(buttons)
        Return dlg.ShowDialog()
    End Using
End Function

Practical tips not covered in earlier replies: use a RichTextBox (read-only) when you need wrapping or mixed formatting. Set AcceptButton and CancelButton to support Enter/Escape. Assign DialogResult values on button click handlers so ShowDialog returns a clear result. Call the dialog with an Owner to keep it centered and modal to that window. Be mindful of DPI: set AutoScaleMode appropriately and test larger fonts. If you need to call from a background thread, marshal to the UI thread before showing the form.

For details about modal dialogs and return values see the docs for Form.ShowDialog and the DialogResult enum: Form.ShowDialog and DialogResult.

Recommended Answers

All 6 Replies

It is so easy to create one yourself with a form, why are you looking for someone's elses code?


Good Luck

It is so easy to create one yourself with a form, why are you looking for someone's elses code?


Good Luck

Ok then, How do I pass a subroutine parameter to a new form instance? For example, msgbox constructors allow:
a string in the box: "Hello World!"
a messagebox type IE: MsgBoxStyle.YesNo
a string for the title: "Howdy"

So that I can call my improved msgbox like thus:
dim msg as newFangledMessageBox
msg = new newFangledMessageBox("You're on fire!", newFangledMessageBox.Exclamation, "Mega Danger!")

... having it show the Ok button only, etc.

or ...

msg = new newFangledMessageBox("Would you a bucket of water?", newFangledMessageBox.YesNo, "Self Preservation Option")

so it will call a sub to show just the yes and no buttons.

Look at the MsgBoxStyle.xxx value from the parameters and show the buttons specified.

The msgbox widget is really useful. However the font limitations of the default msgbox can be brutal if your trying to deliver a message with considerable information. Has anyone coded an improved msgbox class that would take the same arguements + font?

Dear JdTSC,

What you are looking for is probably a form constructor. See, a form is just another class and all classes need constructors. What you may have failed to mention is that you are using vb.net with studio '05. Most of what happens here is a behind the scenes code build by studio.

So any generated form class has it's constructors hidden off in the depths where you'll have to dig to find it, but no matter, just build a new one as such.

'within the form class code...
Public Sub New(ByVal strMsg As String, _
byval strType as string, _
byval strTitle as string)


MyBase.New()
InitializeComponent()
'insert anything else you might care for as a default value.
'or load some of those strings into a text hole.
'as this part supercedes even form.load

End Sub

In this way you can pass all kinds of arguements to a form. Just like you can pass to a function or what not.

'So declare a variable of the form type...
dim var = formWidget

'then declare it as new and put in some parameters...
var = new formWidget("Hello World", "OkOnly", "Greeting")

Tada, the form has the passed variables... but wait! That's not all!

Like the msgbox() widget you might like a return value. This is accomplished with a little something called dialog.

You can set it as such...
'put this mess in the load part of your form
btnOk.DialogResult = Windows.Forms.DialogResult.OK
btnYes.DialogResult = Windows.Forms.DialogResult.Yes
btnCancel.DialogResult = Windows.Forms.DialogResult.Cancel

...or you can use your good properties tool thing in studio to set dialog there for you buttons.

Then your calling form can do something like...
dim choice as string = var.showdialog().tostring
If choice = "Ok" then

'they pushed ok, execute code here

End If

or you can use a select case for dialog returns. Remember it returns a variable of type DialogResult. But .tostring can fix that wagon.

So use var.show() if you don't care about the dialog and

var.showdialog() if you're interested in getting a return type of dialogResult.
Also note that .showdialog disallows any other application action until the dialog has been completed. Which can be nice.

Alrighty, so then you need some kind of select case contraption to handle that middle string which will tell the form what buttons to show, etc.

As such...
'put this in the load aspect also
Select Case strType
Case "OkOnly"
'make visable the ok button only
case "YesNo"
'make visable the Yes and No buttons only
case "OkCancel"
'make visable the ok and cancel buttons
End Select

And do that to your hearts content. Or change text of existing buttons and change their dialogresult return type by case.

Add a label or txb or richtextbox or whatever to take the strMsg from load.
Add me.text = strTitle in form load to make the 3rd arguement string your title of form.

Then you can add whatever font, color, .bmp's, or anything to your new fancy msgForm.

Oh yeah, be sure to thank all the guy's that +1'd your post. Super helpful.

Can someone move this to the vb.net forum where it should be?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.