Customize MessageBox in C #

5

I have the following MessageBox

MessageBox.Show("Deseja iniciar a forma automática?", "Atenção", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question)

Can I customize it?

Instead of being the YesNo buttons, put two buttons with% custom%

    
asked by anonymous 04.11.2016 / 17:35

2 answers

6

This is not possible, it was created to do something very strict indeed. This is a case where you need to create your own component.

Not to leave without any reference has something ready in CodeProject , but never used and not I know if it's good. Maybe it will not fit you, but it's a base for you to build yours.

If it does not even work then you will have to construct it as a properly configured normal form to have similar semantics (it will probably be modal).

    
04.11.2016 / 18:21
5

You need to create your own Window , with Label and two Buttons

public partial class MeuMsgBox : Form
 {
        public MeuMsgBox()
        {
            InitializeComponent(); 
        }

        public DialogResult Resultado { get; private set; }

        public static DialogResult Mostrar(string mensagem, string textoSim, string textoNao)
        {
            var msgBox = new MeuMsgBox();
            msgBox.lblMensagem.Text = mensagem;
            msgBox.btnSim.Text = textoSim;
            msgBox.btnNao.Text = textoNao;
            msgBox.ShowDialog();
            return msgBox.Resultado;
        }

        private void btnSim_Click(object sender, EventArgs e)
        {
            Resultado = DialogResult.Yes;
            Close();
        }

        private void btnNao_Click(object sender, EventArgs e)
        {
            Resultado = DialogResult.No;
            Close();
        }
}

Usage:

DialogResult resultado = MeuMsgBox.Mostrar("Minha pergunta?", "Clique em Sim", "Clique em não");

It's up to you to make the layout look beautiful.

    
04.11.2016 / 18:51