How to customize message box

后端 未结 3 1723
一生所求
一生所求 2020-12-08 21:35

I am doing C# application, and I want to change the style of a message box. Is it possible or not?

Example: change button style, fore color, etc.

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 22:05

    You can't restyle the default MessageBox as that's dependant on the current Windows OS theme, however you can easily create your own MessageBox. Just add a new form (i.e. MyNewMessageBox) to your project with these settings:

    FormBorderStyle    FixedToolWindow
    ShowInTaskBar      False
    StartPosition      CenterScreen
    

    To show it use myNewMessageBoxInstance.ShowDialog();. And add a label and buttons to your form, such as OK and Cancel and set their DialogResults appropriately, i.e. add a button to MyNewMessageBox and call it btnOK. Set the DialogResult property in the property window to DialogResult.OK. When that button is pressed it would return the OK result:

    MyNewMessageBox myNewMessageBoxInstance = new MyNewMessageBox();
    DialogResult result = myNewMessageBoxInstance.ShowDialog();
    if (result == DialogResult.OK)
    {
        // etc
    }
    

    It would be advisable to add your own Show method that takes the text and other options you require:

    public DialogResult Show(string text, Color foreColour)
    {
         lblText.Text = text;
         lblText.ForeColor = foreColour;
         return this.ShowDialog();
    }
    

提交回复
热议问题