1

I am learning C#, and as a part of it I wrote small app that consists of form with two groups of buttons, two buttons each. Button for closing of app is added as well. And everything works ok. Also there is MessageBox, showing that the app is going to be closed. And here is little problem that bugs me: OK button within that MessageBox is NOT centered horizontally. I guess there is a method to align that button, but what puzzles me is why it's is not centered by default? As illustration here are screenshots:

Form with radio buttons

MessageBox with OK button

Here is code as well:

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());
    }
}
Nenad Bulatović
  • 7,238
  • 14
  • 83
  • 113
  • 1
    the `MessageBox` class internally calls some Windows APIs which you have no control over. I don't think there's a way to change that. – Federico Berasategui May 02 '13 at 15:17
  • 1
    I think,If you were to run your program in Windows XP it would be centered. I think (may be wrong) but if you were to change the .Net framework your application targets to an older version it will center. Its basically governed on what microsoft likes the look of – Sayse May 02 '13 at 15:19
  • 1
    Check [How to customize message box](http://stackoverflow.com/questions/3734796/how-to-customize-message-box) – Damith May 02 '13 at 15:23
  • Edit: I just tested my above comment with Framework 3.0 but I'm also on WinXP.. – Sayse May 02 '13 at 15:25
  • @Sayse and how does it look on WindowsXP? Does it center OK? – Nenad Bulatović May 02 '13 at 15:28
  • 1
    If you google 'messagebox' and look at images you'll see what it looks like – Sayse May 02 '13 at 15:55

1 Answers1

2

You can't restyle the default MessageBox as that's completely depends on the windows OS theme. However you could create your own message box by simply creating a new form and then call it by using newMessagebox.ShowDialog();

JayH
  • 194
  • 1
  • 3
  • 17