Reputation: 3063
Because I want a nicer looking message box that actually appears where I want it to appear, I have to make my own custom message box.
How do I make one that returns a value?
The built in MessageBox.Show can return a DialogResult. I'm guessing I create something like that DialogResult class or can I use that class?
Upvotes: 1
Views: 5892
Reputation: 3063
I appreciate James' answer, however I wanted to post an answer that provides the full result of what I did to solve this problem to create a more functional custom message box tool.
See the code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace MyProgram
{
class CustomMessageBox
{
Label txtMsg = new Label();
Button btnOK = new Button();
Button btnCancel = new Button();
Form newForm = new Form();
private DialogResult spawnForm(string title, string text, MessageBoxButtons type)
{
if(type == MessageBoxButtons.OKCancel)
{
newForm.Text = title;
newForm.Controls.Add(txtMsg);
txtMsg.AutoSize = true;
txtMsg.Text = text;
newForm.Width = txtMsg.Width + 125;
newForm.Height = txtMsg.Height + 125;
newForm.MaximumSize = new Size(newForm.Width, newForm.Height);
newForm.MinimumSize = new Size(newForm.Width, newForm.Height);
txtMsg.Location = new Point(newForm.Width / 2 - txtMsg.Width / 2, newForm.Height / 2 - 40);
newForm.Controls.Add(btnOK);
newForm.Controls.Add(btnCancel);
btnOK.Text = "OK";
btnCancel.Text = "Cancel";
btnOK.Location = new Point(newForm.Width / 2 - btnOK.Width / 2 - 60, txtMsg.Location.Y + txtMsg.Height + 20);
btnCancel.Location = new Point(newForm.Width / 2 - btnOK.Width / 2 + 40, btnOK.Location.Y);
btnOK.DialogResult = DialogResult.OK;
btnCancel.DialogResult = DialogResult.Cancel;
newForm.StartPosition = FormStartPosition.CenterParent;
return newForm.ShowDialog();
} else
{
newForm.Text = title;
newForm.Controls.Add(txtMsg);
txtMsg.AutoSize = true;
txtMsg.Text = text;
newForm.Width = txtMsg.Width + 125;
newForm.Height = txtMsg.Height + 125;
newForm.MaximumSize = new Size(newForm.Width, newForm.Height);
newForm.MinimumSize = new Size(newForm.Width, newForm.Height);
txtMsg.Location = new Point(newForm.Width / 2 - txtMsg.Width / 2 - 10, newForm.Height / 2 - 40);
newForm.Controls.Add(btnOK);
newForm.Controls.Remove(btnCancel);
btnOK.Text = "OK";
btnOK.Location = new Point(newForm.Width / 2 - btnOK.Width / 2 -10, txtMsg.Location.Y + txtMsg.Height + 20);
btnOK.DialogResult = DialogResult.OK;
newForm.StartPosition = FormStartPosition.CenterParent;
return newForm.ShowDialog();
}
}
public DialogResult Text(string title, string text, MessageBoxButtons type)
{
return spawnForm(title, text, type);
}
}
}
Upvotes: 2
Reputation: 429
All you need to do is create your own custom form. All forms inherit for System.Windows.Forms.Form. When you want to show the form call ShowDialog() which returns a DialogResult when the form is closed. Depending on what you click on the message box you set the internal DialogResult property and call Close().
CustomMessageBox class
public partial class CustomMessageBox : Form
{
public CustomMessageBox()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
// Some other logic for OK button
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
// Some other logic for Cancel button
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
To use the message box it would just be
CustomMessageBox customMessage = new CustomMessageBox();
DialogResult result = customMessage.ShowDialog();
Of course you would have to add more to show an actual message and you could make a show method that is static like MessageBox.Show() but this is the basics for the DialogResult part.
Upvotes: 3