Search code examples
c#htmlmessageboxhorizontal-line

Is there something like a horizontal line (i.e. <hr>) that can be used for C# Message Box?


For example instead of doing the ("\n-----------------"); for a MessageBox.Show(); function, is there an in-build horizontal line generator similar to the HTML <hr>?


Solution

  • A messagebox is a simple element for showing plain text. It does not have much design features.

    However you can easily create a new form, put in its text and show it using Form.ShowDialog. Now you can add any design-element you want by chosing it in the toolbox, e.g. by using a 2px label as shown here.

    You could also implement the syntax you know from MessageBox by using a static method:

    public class CustomMessageBox : Form
    {
        private readonly static instance = new CustomMessageBox();
        private DialogResult result = DialogResult.No;
    
        private CustomMessageBox()
        {
            btnOK.DialogResult = DialogResult.OK;
            btnCancel.DialogResult = DialogResult.Cancel;
            this.AcceptButton = btnOK;
            this.CancelButton = btnCancel;
        }
        public static DialogResult Show(string text)
        {
            return instance.ShowDialog();
        }
    }