Customize Messagebox in C#

Asked

Viewed 2,900 times

5

I got the following MessageBox

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

I can personalize it?

Instead of being the buttons YesNo, put two buttons with labels customized

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

6

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

Not to leave without any reference has something ready in Codeproject, but I’ve never used it and I don’t know if it’s good. I may not answer you, but it’s a basis for you to build your.

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

6

You need to create your own Window, with a 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();
        }
}

Utilizing:

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

It’s up to you to work on the layout to look beautiful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.