MessageBox close another form - c#

Hello Guys I have msg box when i press on yes its close that form which calls msg box
how can i do when msg box dialogresult = ok close only himself

Set the DialogResault property to None for the button which its event handler open the MessageBox.
Good luck!

DialogResult result = MessageBox.Show("Click yes to close, otherwise click no.", "Message Box Test", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
Application.Exit();
}

Maybe you're assigning the result to the DialogResult property of the parent form, (see http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult.aspx) and in particular from the remark section :
" If the form is displayed as a dialog
box, setting this property with a
value from the DialogResult
enumeration sets the value of the
dialog box result for the form, hides
the modal dialog box, and returns
control to the calling form."
Use:
if (MessageBox.Show(...) == DialogResult.Yes)
{
}
else
{
}

Related

How to show a message dialog before can bring other usercontrol to front

I am developing a WinForms Application , I design a Hamburger Menu app, Lets say I have a "Button1" & "Button2" on the left panel of my app and I have 2 UserControl(userControl1, userControl2) , then when I click the button1, I call userControl1.BringToFront();
this is my exact code:
if (!panelUSerControl.Controls.Contains(DashboardView.Instance)) {
panelUSerControl.Controls.Add(DashboardView.Instance);
DashboardView.Instance.Dock = DockStyle.Fill;
DashboardView.Instance.BringToFront();
} else {
DashboardView.Instance.BringToFront();
}
So userControl1 is now displayed on the screen , my question is when I click the button2(this will show userControl2), how can I display a confirmation message with option(Yes/No) to inform the user that he/she will leave the userControl1? , and if the user selects No, the app will stays in the userControl1 , and if yes , will show userControl2
Thanks in advance,
NicoTing
You should do this in your button2 click event, i.e. display a confirmation message there and then proceed to show the second control or not:
private void button2_click(object sender, System.EventArgs e)
{
if (MessageBox.Show("Show userControl2?", "Confirmation",
MessageBoxButtons.YesNo) == DialogResult.Yes))
{
userControl2.BringToFront();
}
}
There is a "leave" event on each control but this fires when the user has already left the control, so you cannot use it to confirm before leaving the control.
this is what I did:
if (panelUSerControl.Controls.Contains(userControl1.Instance) && panelUSerControl.Controls.GetChildIndex(userControl1.Instance) == 0) {
if (MessageBox.Show("Are you sure you want to cancel your delivery?", "Confirmation", MessageBoxButtons.YesNo) == DialogResult.No) {
return;
}
}

Message box closes form regardless of result

i am new to message box buttons and it seems to close the form regardless.
private void btnFechar_Click(object sender, EventArgs e)
{
DialogResult = MessageBox.Show("Desjea Sair?", "Aviso", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (DialogResult == DialogResult.Yes)
{
this.Close();
}
}
thanks in advance
You're setting the DialogResult of the form (this looks like WinForms), which always closes it.
Create a local variable inside your button click event:
private void btnFechar_Click(object sender, EventArgs e)
{
var dialogResult = MessageBox.Show("Desjea Sair?", "Aviso", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.Yes)
{
this.Close();
}
}
More on Form.DialogResult from MSDN:
If the form is displayed as a dialog box, setting this property with a value from the DialogResult enumeration sets the value of the dialog box result for the form, hides the modal dialog box, and returns control to the calling form.
So if you're displaying the form using ShowDialog() as most of us do, then setting the form's DialogResult property causes it to close.

How could I control the DialogResult of a button?

I want to create a dialog requesting user information, and I set the Ok button's DialogResult to OK. In the OK button's Click event, the program checks for empty textboxes, and if there are empty textboxes, the program shows a message box and stops the dialog box from closing. But because I set the DialogResult to OK, there are no way of stopping the box from closing. Could you give me some solutions? Thanks.
Here's my code:
outcomeName = outcomeNameTxtBx.Text;
outcomeDetails = outcomeDetailsTxtBx.Text;
addTargetedClasses();
finishDate = finishDatePicker.Value;
beginWorkingDate = beginWorkingDatePicker.Value;
if (!isAllInfoEntered())
{
//Show the message box
return;
}
Just set it back if you are not happy:
if (!isAllInfoEntered())
{
this.DialogResult = DialogResult.None;
//Show the message box
return;
}
Don't make return:
if (!isAllInfoEntered())
{
this.DialogResult = DialogResult.None;
//Show the message box
}
else
{
return;
}

Keeping the parent DialogButton open after the child DialogButton is closed

I'm having a problem in keeping the Parent Form to be opened till I close after using ShowDialog() .
I've been trying regarding this but couldn't get. I think there is something simple that I might have been missing. Can you please help me reg this?
The problem is,
I have Form 1, on pressing one button, Form 2 opens.
I do some validations in Form 2, and check for the validations. If the validation doesn't pass, I open a DialogBox form, with Retry and Cancel.
If I press Retry, The control should go back to the Form 2 and form 2 should not close.
If the press Cancel, both the DialogBox form and the Form 2 should close. Right now, regardless of what I press, both the forms close.
I have looked online and couldn't find any solution. Went through this solution, but both the forms are still closing for me.
Why does closing a nested child dialog also close the parent dialog?
My code:(Sample example scenario)
Form 1:
private void button1_Click(object sender, EventArgs e)
{
Form2 testForm = new Form2();
DialogResult dialogResult = new DialogResult();
dialogResult = testForm.ShowDialog(this);
if(dialogResult == DialogResult.OK)
{
//Do something
}
}
Form 2:
private void button1_Click(object sender, EventArgs e)
{
DialogResult validDataResult = MessageBox.Show("Invalid Data Entered. Please provide the correct data."
, "Data Management"
, MessageBoxButtons.RetryCancel);
if (validDataResult == DialogResult.Cancel)
{
this.Close();
}
}
in Form2.cs do your validation and then
(assuming validationOK is the true/false result of your checks)
if(validationOK == false)
{
// Ask retry or cancel to the user
if(DialogResult.Cancel == MessageBox.Show("Validation Fail", "Validation failed, press retry to do it againg", MessageBoxButtons.RetryCancel))
this.DialogResult.Cancel; // Set the dialog result on form2. This will close the form.
// if you have the validation done in a button_click event and that button has its
// property DialogResult set to something different than DialogResult.None, we need
// to block the form2 from closing itself.
// uncomment this code if the above comment is true
// else
// this.DialogResult = DialogResult.None;
}
You have to set the DialogResult of Form2 before you can call this.Close(). Otherwise, it remains the default. (The below code is only a guess of the actual double close logic since you did not specify that)
Form2.cs:
if (validDataResult == DialogResult.Cancel)
DialogResult = DialogResult.Cancel;
else
DialogResult = DialogResult.OK;
Close();
Form1.cs:
if(dialogResult == DialogResult.OK)
{
Close();
}
else
{
//Do something
}

Windows mobile message box this.close()?

On Windows mobile 6.1 prof.
I have a messagebox with a yes/no button on it. When I click 'No' option in the messagebox, my whole application shutsdown, how can I simply close the messagebox?
string message = "Application will perform a data download agree?";
string caption = "";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result = MessageBox.Show(message, caption, buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (result == DialogResult.Yes)
{
navigateForward(WEB_PAGE_NAVIGATE);
}
else
{
this.Close();
}
You do not need to close the message box. It's a DialogWindow and will close itself when you click any of the options:
DialogResult result = MessageBox.Show();
if (result == DialogResult.Yes)
{
navigateForward(WEB_PAGE_NAVIGATE);
}
else
{
// No need to do anything here as the MessageBox is closed automatically.
}
The reason your whole application shuts down is because this relates to the class you're currently in. I'm guessing that class is your main Form and when your main form is closed, the application shuts down.

Categories