1

我正在学习 C#,作为其中的一部分,我编写了一个小应用程序,该应用程序由带有两组按钮的表单组成,每组两个按钮。还添加了关闭应用程序的按钮。一切正常。还有MessageBox,显示应用程序将要关闭。这里有个小问题困扰着我:MessageBox 中的 OK 按钮不是水平居中的。我想有一种方法可以对齐该按钮,但令我困惑的是为什么它默认不居中?如图所示,这里是截图:

带有单选按钮的表单

带有确定按钮的消息框

这里也是代码:

using System;
using System.Drawing;
using System.Windows.Forms;

public class myForm : Form
{
    private GroupBox gboxGrp1;
    private GroupBox gboxGrp2;

    private RadioButton butn1a;
    private RadioButton butn1b;
    private RadioButton butn2a;
    private RadioButton butn2b;
    private Button btnClose;

    public myForm()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        this.btnClose = new Button();

        this.gboxGrp1 = new GroupBox();
        this.gboxGrp2 = new GroupBox();

        this.butn1a = new RadioButton();
        this.butn1b = new RadioButton();
        this.butn2a = new RadioButton();
        this.butn2b = new RadioButton();

        //myForm
        this.Text = "My Form";
        this.StartPosition = FormStartPosition.CenterScreen;
        this.Height = 350;
        this.Width = 200;

        //btnClose
        this.Controls.Add(btnClose);
        this.btnClose.Text = "Close";
        this.btnClose.Location = new Point(60, 260);
        this.btnClose.Click += new EventHandler(btnClose_Click);

        //gboxgrp1
        this.gboxGrp1.Location = new Point(20, 20);
        this.gboxGrp1.Text = "Group Box 1";
        this.gboxGrp1.Width = 150;
        this.gboxGrp1.Height = 100;

        //gboxgrp2
        this.gboxGrp2.Text = "Group Box 2";
        this.gboxGrp2.Location = new Point(20, 130);
        this.gboxGrp2.Width = 150;
        this.gboxGrp2.Height = 100;

        //Radio buttons
        this.butn1a.Text = "Radio 1a";
        this.butn1a.Location = new Point(30, 30);
        this.butn1a.Size = new Size(90, 15);

        this.butn1b.Text = "Radio 1b";
        this.butn1b.Location = new Point(30, 60);
        this.butn1b.Size = new Size(90, 15);

        this.butn2a.Text = "Radio 2a";
        this.butn2a.Location = new Point(30, 30);
        this.butn2a.Size = new Size(90, 15);

        this.butn2b.Text = "Radio 2b";
        this.butn2b.Location = new Point(30, 70);
        this.butn2b.Size = new Size(90, 15);

        //Controls
        this.Controls.Add(gboxGrp1);
        this.Controls.Add(gboxGrp2);

        this.gboxGrp1.Controls.Add(butn1a);
        this.gboxGrp1.Controls.Add(butn1b);
        this.gboxGrp2.Controls.Add(butn2a);
        this.gboxGrp2.Controls.Add(butn2b);
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Closing Application");
        Application.Exit();
    }
}

public class MyApp
{
    public static void Main()
    {
        Application.Run(new myForm());
    }
}
4

1 回答 1

2

您不能重新设置默认 MessageBox 的样式,因为这完全取决于 Windows 操作系统主题。但是,您可以通过简单地创建一个新表单来创建自己的消息框,然后使用 newMessagebox.ShowDialog(); 调用它。

于 2013-05-02T15:29:54.100 回答