Custom MessageBox UserControl

Hi,

I want to make a usercontrol acting as a messagebox for my smarphone project.

The idea is that when myMsgBox.Show("Are Your Sure ") is called, a small rectangle box appears on the screen with the specified text, and the menuItems on the form turn to "Ok" and "Cancel".

The calling code should block (like showDialog does on forms) and continue when the user presses ok or cancel. The myMsgBox.Sho() method should also return the boolean true or false, depending on the users choice.

Psudo-Code:

MyFunction()
{
....

bool result = myMsgBox.Show("Are you sure ");


// THIS CODE IS NOW BLOCKED AND THE CUSTOM MSGBOX IS ACTIVE
// THE MENUITEMS ON THE FORM HAS CHANGED TO "OK" AND "CANCEL"
// WHEN THE USER PRESSES ON A MENUITEM, THIS CODE WILL CONTINUE
// AND THE MESSAGEBOX WILL DISSAPPEAR

if(result)
{
myLabel.Text = "User pressed Ok";
}
else
{
myLabel.Text = "User pressed Cancel";
}
}

Hope you understand my question.

Regards, Nima



Answer this question

Custom MessageBox UserControl

  • saffy_in_aus

    Nima,

    You have to do a couple of things. First, create a second form with the OK and Cancel buttons on it. Then, right click on one of the buttons in design view and click on "Properties". Under the behavior > property click "Dialog Result" and change it to the name of the button (ok or cancel). Do it for each button.

    Then add this in your main form (assuming your custom message box class is called "CustomMessageBox)

    private void btnGo_Click(object sender, EventArgs e)

    {

    //Declare the class

    CustomMessageBox MyMessageBox = new CustomMessageBox();

    //Pop up the dialog

    DialogResult MyDialog = (MyMessageBox.ShowDialog());

    //Make a decision

    if (MyDialog == DialogResult.Cancel)

    {

    MessageBox.Show("Cancel was pressed");

    }

    else

    {

    MessageBox.Show("Ok was pressed");

    }

    }

    ///Best ofluck, Ryan


  • Custom MessageBox UserControl