在托管C ++中使用自定义按钮文本创建MessageBox的简单方法是什么?

时间:2023-01-19 16:20:55

I would like to keep the overhead at a minimum. Right now I have:

我想将开销保持在最低限度。现在我有:

// Launch a Message Box with advice to the user
DialogResult result = MessageBox::Show("This may take awhile, do you wish to continue?", "Warning", MessageBoxButtons::YesNo, MessageBoxIcon::Exclamation);

// The test will only be launched if the user has selected Yes on the Message Box
if(result == DialogResult::Yes)
{
    // Execute code
}

Unfortunately my client would prefer "Continue" and "Cancel" in place of the default "Yes" and "No" button text. It seems like there should be an easy way to do this.

不幸的是,我的客户更喜欢“继续”和“取消”来代替默认的“是”和“否”按钮文本。似乎应该有一个简单的方法来做到这一点。

3 个解决方案

#1


2  

You can use "OK" and "Cancel"

你可以使用“确定”和“取消”

By substituting MessageBoxButtons::YesNo with MessageBoxButtons::OKCancel

用MessageBoxButtons :: OKCancel替换MessageBoxButtons :: YesNo

MessageBoxButtons Enum

Short of that you would have to create a new form, as I don't believe the Enum can be extended.

除此之外,您必须创建一个新表单,因为我不相信Enum可以扩展。

#2


0  

Change the message as below. This may be the simplest way I think.

更改消息如下。这可能是我认为最简单的方法。

DialogResult result = MessageBox::Show(
       "This may take awhile, do you wish to continue?**\nClick Yes to continue.\nClick No to cancel.**",
       "Warning",
       MessageBoxButtons::YesNo,
       MessageBoxIcon::Exclamation
);

#3


0  

From everything I can find it looks like Corin is right. Here is the code that I used to accomplish the original goal. I am not usually a Managed C++ programmer, so any editing would be appreciated.

从我能发现的一切看起来,科林是正确的。这是我用来完成原始目标的代码。我通常不是一个托管C ++程序员,所以任何编辑都将不胜感激。

CustomMessageBox.h:

using namespace System::Windows::Forms;

/// <summary>
/// A message box for the test. Used to ensure user wishes to continue before starting the test.
/// </summary>
public ref class CustomMessageBox : Form
{
private:
    /// Used to determine which button is pressed, default action is Cancel
    static String^ Button_ID_ = "Cancel";

    // GUI Elements
    Label^ warningLabel_;
    Button^ continueButton_;
    Button^ cancelButton_;

    // Button Events
    void CustomMessageBox::btnContinue_Click(System::Object^ sender, EventArgs^ e);
    void CustomMessageBox::btnCancel_Click(System::Object^ sender, EventArgs^ e);

    // Constructor is private. CustomMessageBox should be accessed through the public ShowBox() method
    CustomMessageBox();

public:
    /// <summary>
    /// Displays the CustomMessageBox and returns a string value of "Continue" or "Cancel"
    /// </summary>
    static String^ ShowBox();
};

CustomMessageBox.cpp:

#include "StdAfx.h"
#include "CustomMessageBox.h"

using namespace System::Windows::Forms;
using namespace System::Drawing;

CustomMessageBox::CustomMessageBox()
{
    this->Size = System::Drawing::Size(420, 150);
    this->Text="Warning";
    this->AcceptButton=continueButton_;
    this->CancelButton=cancelButton_;
    this->FormBorderStyle= ::FormBorderStyle::FixedDialog;
    this->StartPosition= FormStartPosition::CenterScreen;
    this->MaximizeBox=false;
    this->MinimizeBox=false;
    this->ShowInTaskbar=false;

    // Warning Label
    warningLabel_ = gcnew Label();
    warningLabel_->Text="This may take awhile, do you wish to continue?";
    warningLabel_->Location=Point(5,5);
    warningLabel_->Size=System::Drawing::Size(400, 78);
    Controls->Add(warningLabel_);

    // Continue Button
    continueButton_ = gcnew Button();
    continueButton_->Text="Continue";
    continueButton_->Location=Point(105,87);
    continueButton_->Size=System::Drawing::Size(70,22);
    continueButton_->Click += gcnew System::EventHandler(this, &CustomMessageBox::btnContinue_Click);
    Controls->Add(continueButton_);

    // Cancel Button
    cancelButton_ = gcnew Button();
    cancelButton_->Text="Cancel";
    cancelButton_->Location=Point(237,87);
    cancelButton_->Size=System::Drawing::Size(70,22);
    cancelButton_->Click += gcnew System::EventHandler(this, &CustomMessageBox::btnCancel_Click);
    Controls->Add(cancelButton_);
}

/// <summary>
/// Displays the CustomMessageBox and returns a string value of "Continue" or "Cancel", depending on the button
/// clicked.
/// </summary>
String^ CustomMessageBox::ShowBox()
{
    CustomMessageBox^ box = gcnew CustomMessageBox();
    box->ShowDialog();

    return Button_ID_;
}

/// <summary>
/// Event handler: When the Continue button is clicked, set the Button_ID_ value and close the CustomMessageBox.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
void CustomMessageBox::btnContinue_Click(System::Object^ sender, EventArgs^ e)
{
    Button_ID_ = "Continue";
    this->Close();
}

/// <summary>
/// Event handler: When the Cancel button is clicked, set the Button_ID_ value and close the CustomMessageBox.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
void CustomMessageBox::btnCancel_Click(System::Object^ sender, EventArgs^ e)
{
    Button_ID_ = "Cancel";
    this->Close();
}

And then finally the modification to the original code:

然后最后修改原始代码:

// Launch a Message Box with advice to the user
String^ result = CustomMessageBox::ShowBox();

// The test will only be launched if the user has selected Continue on the Message Box
if(result == "Continue")
{
    // Execute Code
}

#1


2  

You can use "OK" and "Cancel"

你可以使用“确定”和“取消”

By substituting MessageBoxButtons::YesNo with MessageBoxButtons::OKCancel

用MessageBoxButtons :: OKCancel替换MessageBoxButtons :: YesNo

MessageBoxButtons Enum

Short of that you would have to create a new form, as I don't believe the Enum can be extended.

除此之外,您必须创建一个新表单,因为我不相信Enum可以扩展。

#2


0  

Change the message as below. This may be the simplest way I think.

更改消息如下。这可能是我认为最简单的方法。

DialogResult result = MessageBox::Show(
       "This may take awhile, do you wish to continue?**\nClick Yes to continue.\nClick No to cancel.**",
       "Warning",
       MessageBoxButtons::YesNo,
       MessageBoxIcon::Exclamation
);

#3


0  

From everything I can find it looks like Corin is right. Here is the code that I used to accomplish the original goal. I am not usually a Managed C++ programmer, so any editing would be appreciated.

从我能发现的一切看起来,科林是正确的。这是我用来完成原始目标的代码。我通常不是一个托管C ++程序员,所以任何编辑都将不胜感激。

CustomMessageBox.h:

using namespace System::Windows::Forms;

/// <summary>
/// A message box for the test. Used to ensure user wishes to continue before starting the test.
/// </summary>
public ref class CustomMessageBox : Form
{
private:
    /// Used to determine which button is pressed, default action is Cancel
    static String^ Button_ID_ = "Cancel";

    // GUI Elements
    Label^ warningLabel_;
    Button^ continueButton_;
    Button^ cancelButton_;

    // Button Events
    void CustomMessageBox::btnContinue_Click(System::Object^ sender, EventArgs^ e);
    void CustomMessageBox::btnCancel_Click(System::Object^ sender, EventArgs^ e);

    // Constructor is private. CustomMessageBox should be accessed through the public ShowBox() method
    CustomMessageBox();

public:
    /// <summary>
    /// Displays the CustomMessageBox and returns a string value of "Continue" or "Cancel"
    /// </summary>
    static String^ ShowBox();
};

CustomMessageBox.cpp:

#include "StdAfx.h"
#include "CustomMessageBox.h"

using namespace System::Windows::Forms;
using namespace System::Drawing;

CustomMessageBox::CustomMessageBox()
{
    this->Size = System::Drawing::Size(420, 150);
    this->Text="Warning";
    this->AcceptButton=continueButton_;
    this->CancelButton=cancelButton_;
    this->FormBorderStyle= ::FormBorderStyle::FixedDialog;
    this->StartPosition= FormStartPosition::CenterScreen;
    this->MaximizeBox=false;
    this->MinimizeBox=false;
    this->ShowInTaskbar=false;

    // Warning Label
    warningLabel_ = gcnew Label();
    warningLabel_->Text="This may take awhile, do you wish to continue?";
    warningLabel_->Location=Point(5,5);
    warningLabel_->Size=System::Drawing::Size(400, 78);
    Controls->Add(warningLabel_);

    // Continue Button
    continueButton_ = gcnew Button();
    continueButton_->Text="Continue";
    continueButton_->Location=Point(105,87);
    continueButton_->Size=System::Drawing::Size(70,22);
    continueButton_->Click += gcnew System::EventHandler(this, &CustomMessageBox::btnContinue_Click);
    Controls->Add(continueButton_);

    // Cancel Button
    cancelButton_ = gcnew Button();
    cancelButton_->Text="Cancel";
    cancelButton_->Location=Point(237,87);
    cancelButton_->Size=System::Drawing::Size(70,22);
    cancelButton_->Click += gcnew System::EventHandler(this, &CustomMessageBox::btnCancel_Click);
    Controls->Add(cancelButton_);
}

/// <summary>
/// Displays the CustomMessageBox and returns a string value of "Continue" or "Cancel", depending on the button
/// clicked.
/// </summary>
String^ CustomMessageBox::ShowBox()
{
    CustomMessageBox^ box = gcnew CustomMessageBox();
    box->ShowDialog();

    return Button_ID_;
}

/// <summary>
/// Event handler: When the Continue button is clicked, set the Button_ID_ value and close the CustomMessageBox.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
void CustomMessageBox::btnContinue_Click(System::Object^ sender, EventArgs^ e)
{
    Button_ID_ = "Continue";
    this->Close();
}

/// <summary>
/// Event handler: When the Cancel button is clicked, set the Button_ID_ value and close the CustomMessageBox.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
void CustomMessageBox::btnCancel_Click(System::Object^ sender, EventArgs^ e)
{
    Button_ID_ = "Cancel";
    this->Close();
}

And then finally the modification to the original code:

然后最后修改原始代码:

// Launch a Message Box with advice to the user
String^ result = CustomMessageBox::ShowBox();

// The test will only be launched if the user has selected Continue on the Message Box
if(result == "Continue")
{
    // Execute Code
}