Share via


How is code written For Text Align in a msgBox

Question

Monday, December 10, 2012 4:33 PM

Hi Good People

How is vb code written to center the first line of text in a msgBox.

 MsgBox("Is that right" & vbCrLf & " 'You Have Chosen To Close This Programe'", MsgBoxStyle.YesNo, "Programe Closing")

Kind Regards

gary

Gary Simpson

All replies (7)

Monday, December 10, 2012 5:06 PM ✅Answered | 1 vote

Hi, you can not center a Text in a MsgBox. MessageBox is better to use, because it is .NET:

MessageBox.Show("Is that right" & vbCrLf & " 'You Have Chosen To Close This Programe'", "Programe Closing", MessageBoxButtons.YesNo)

You can add a MessageBoxOptions.RightAlign to make all the text right aligned. Centered is also not possible here.

Only way: create your own form.

Koopakiller [kuːpakɪllɐ] http://koopakiller.ko.ohost.de/


Monday, December 10, 2012 5:06 PM ✅Answered | 1 vote

I don't think you can centre text in a MsgBox or a MessageBox.  You could create your own simple form that has the same functionality and handle text alignment in the form's controls any way you like.


Monday, December 10, 2012 7:27 PM ✅Answered | 1 vote

Gary,

Mine isn't as fancy as some but it's easily customizable:

Option Strict OnOption Explicit OnPublic Class CustomMessageBox_Style1    Public Enum ButtonStyle        AbortRetryIgnore        Ok        OkCancel        RetryCancel        YesNo        YesNoCancel    End Enum    Public Enum CustomMessageBoxResult        Abort        Retry        Ignore        Ok        Cancel        Yes        No    End Enum    Public Enum TextAlignment        Left        Center        Right    End Enum    Public Class CustomMessageInfo        Public TitleToShow As String = "VB Net Forum Custom Message Box"        Public IconToDisplay As Image = Nothing        Public ButtonStyle As ButtonStyle        Public MessageToDisplay As String = "--"        Public MessageAlignment As TextAlignment = TextAlignment.Left        Public MessageFont As Font = New Font("Tahoma", 10, FontStyle.Regular)        Public MessageColor As Color = Color.Black        Public MessageShowVerticalScrollBar As Boolean = False        Public FormBackColor As Color = Color.MidnightBlue        Public UserAnswer As CustomMessageBoxResult = CustomMessageBoxResult.Cancel    End Class    Public messageInfo As New CustomMessageInfo    Private Sub CustomMessageBox_Style1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _        Handles MyBase.Load        Button1.Visible = False        Button2.Visible = False        Button3.Visible = False        With Me            .Text = messageInfo.TitleToShow            .BackColor = messageInfo.FormBackColor        End With        With TextBox_MessageToShow            .Text = messageInfo.MessageToDisplay            .Font = messageInfo.MessageFont            Select Case messageInfo.MessageAlignment                Case TextAlignment.Center                    .TextAlign = HorizontalAlignment.Center                Case TextAlignment.Left                    .TextAlign = HorizontalAlignment.Left                Case TextAlignment.Right                    .TextAlign = HorizontalAlignment.Right            End Select            .ForeColor = messageInfo.MessageColor            If messageInfo.MessageShowVerticalScrollBar Then                .ScrollBars = ScrollBars.Vertical            Else                .ScrollBars = ScrollBars.None            End If        End With        If messageInfo.IconToDisplay IsNot Nothing Then            Dim imgWidth As Integer = 0            Dim imgHeight As Integer = 0            Using bmp As New Bitmap(messageInfo.IconToDisplay)                imgWidth = bmp.Width                imgHeight = bmp.Height            End Using            If imgWidth > PictureBox_IconToShow.Width Or _               imgHeight > PictureBox_IconToShow.Height Then                PictureBox_IconToShow.SizeMode = PictureBoxSizeMode.Zoom            Else                PictureBox_IconToShow.SizeMode = PictureBoxSizeMode.AutoSize            End If            PictureBox_IconToShow.Image = messageInfo.IconToDisplay        Else            PictureBox_IconToShow.Image = Nothing        End If        Select Case messageInfo.ButtonStyle            Case ButtonStyle.AbortRetryIgnore                With Button1                    .Text = "Abort"                    .Visible = True                End With                With Button2                    .Text = "Retry"                    .Visible = True                End With                With Button3                    .Text = "Ignore"                    .Visible = True                End With            Case ButtonStyle.Ok                With Button2                    .Text = "OK"                    .Visible = True                End With            Case ButtonStyle.OkCancel                With Button1                    .Text = "OK"                    .Visible = True                End With                With Button3                    .Text = "Cancel"                    .Visible = True                End With            Case ButtonStyle.RetryCancel                With Button1                    .Text = "Retry"                    .Visible = True                End With                With Button3                    .Text = "Cancel"                    .Visible = True                End With            Case ButtonStyle.YesNo                With Button1                    .Text = "Yes"                    .Visible = True                End With                With Button3                    .Text = "No"                    .Visible = True                End With            Case ButtonStyle.YesNoCancel                With Button1                    .Text = "Yes"                    .Visible = True                End With                With Button2                    .Text = "No"                    .Visible = True                End With                With Button3                    .Text = "Cancel"                    .Visible = True                End With        End Select    End Sub    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _        Handles Button1.Click, Button2.Click, Button3.Click        Dim btn As Button = DirectCast(sender, Button)        With messageInfo            Select Case btn.Text                Case "Abort"                    .UserAnswer = CustomMessageBoxResult.Abort                Case "Retry"                    .UserAnswer = CustomMessageBoxResult.Retry                Case "Ignore"                    .UserAnswer = CustomMessageBoxResult.Ignore                Case "OK"                    .UserAnswer = CustomMessageBoxResult.Ok                Case "Cancel"                    .UserAnswer = CustomMessageBoxResult.Cancel                Case "Yes"                    .UserAnswer = CustomMessageBoxResult.Yes                Case "No"                    .UserAnswer = CustomMessageBoxResult.No            End Select        End With        Close()    End SubEnd Class

To use it (as for example, from Form1):

Option Strict OnOption Explicit OnPublic Class Form1    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _        Handles MyBase.Load        Using cmb As New CustomMessageBox_Style1            With cmb.messageInfo                .TitleToShow = "Test Title"                .IconToDisplay = My.Resources.Win                .ButtonStyle = CustomMessageBox_Style1.ButtonStyle.AbortRetryIgnore                .MessageToDisplay = "This is an example message that you might display."                .MessageAlignment = CustomMessageBox_Style1.TextAlignment.Center                .MessageFont = New Font("Arial", 12, FontStyle.Regular)                .MessageColor = Color.BurlyWood                .MessageShowVerticalScrollBar = False                .FormBackColor = Color.AntiqueWhite            End With            cmb.ShowDialog()            MessageBox.Show("The user's answer is shown below:" & _                            vbCrLf & vbCrLf & cmb.messageInfo.UserAnswer.ToString)        End Using    End SubEnd Class

Here's how my example looks:

If you or anyone wants me to upload it just let me know.

Please call me Frank :)


Monday, December 10, 2012 5:42 PM

I thought it could be something like

 Dim MessageBox As Integer        MessageBox = ContentAlignment.MiddleCenter

Gary Simpson


Monday, December 10, 2012 5:53 PM

If you create your own form, you can position text (e.g. in a label) like this:

Me.label1.TextAlign = ContentAlignment.MiddleCenter

To use your form like a MessageBox, display it using the Form.ShowDialog method.


Monday, December 10, 2012 5:57 PM | 1 vote

See the following link:
http://www.codeproject.com/Articles/19865/How-to-extend-the-native-MessageBox-dialog-in-NET

I hope, it can be help to create your own MessageBox class. I know, it is C#, but the classes are the same.

Koopakiller [kuːpakɪllɐ] http://koopakiller.ko.ohost.de/


Tuesday, December 11, 2012 7:33 PM | 1 vote

All,

If anyone is interested, I’ve modified the custom MessageBox that I created yesterday in a number of ways, including adding something that I can sure make use of – an option to have the buttons show “Continue” and “Stop”.

As an example of where I’d use it, I often make use a BackgroundWorker and if it’s running when the user closes the form, I have to deal with stopping the process. Using this I can give them the option.

I have the code shown on two pages of my website shown below:

Code For CustomMessageBox

Code For Typical Usage (Form1.vb)

If you’d like to download the project folder (zipped up), I have it here. When that page opens you can click on the zip file shown there to download it. If you run a version higher than me (I use VS 2008), then you’ll want to open it at least once so that it converts it to your version.

Following that, if you want to add it to a new project, do as follows (again this is in VS 2008 but I feel sure yours will be similar):

In Solution Explorer, right-click the project’s name at the top then Add >> Add Existing Item:

Now browse to where you have the project stored on your computer and select the file:

Be sure to select the one called “CustomMessageBox_Style1.vb”, not the designer. When you bring it into your project it will bring the designer in with it so don’t worry there.

Once you click OK then you’ll see it has been added to your project:

I hope someone finds this helpful. :)

Please call me Frank :)