How could I control the DialogResult of a button? - c#

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;
}

Related

How to do something after clicking on MessageBox button, but before this MessageBox closes?

I have a list of "projects" with some informations in textBoxs for each project. The user can select a project then modify the informations and click on save button after that.
If I changes selected project without save the modifications, a Yes/No MessageBox appear:
DialogResult dialogResult = MessageBox.Show(
"Do you want to save changes ?",
"Title",
MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
//Click Yes
}
else
{
//Click No
}
I would to refresh all the project list (with my own Refresh() methode) after clicking on Yes/No button, but staying on the MessageBox until the refresh is done.
Is it possible?
The built in MessageBox class does not allow such complicated behaviour.
One way to do this is to create your own message box. Create a subclass of Form, add some labels and buttons. Expose some events like YesClicked and NoClicked.
In your main form, create an instance of your custom message box, subscribe to the events, and call ShowDialog on it.
After the refresh is done, you can call Close or Dispose on your custom message box to close it.
Try creating a custom message box. I commented the code, let me know if you need clarification
public static class MessageBoxResult
{
public static int dialogResult; // <== i use this value to determine what button was pressed
}
// your custom message box form code
public partial class CustomMsgBox : Form
{
public CustomMsgBox()
{
InitializeComponent();
}
public void show(string pos0, string pos1, string pos2, string message) //<=== initializing the message box with the values from your main code
{
button1.Text = pos0;
button2.Text = pos1;
button3.Text = pos2;
label1.Text = message;
}
// message box events to set the static field incase a button on the custom form was changed
private void button1_Click(object sender, EventArgs e)
{
MessageBoxResult.dialogResult = 0;
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBoxResult.dialogResult = 1;
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
MessageBoxResult.dialogResult = 2;
this.Close();
}
}
//usage
{
MessageBoxResult.dialogResult = -1; // <== setting the static field to -1 to mean nothing was pressed
CustomMsgBox cMsgBox = new CustomMsgBox();
cMsgBox.show("your message");
cMsgBox.ShowDialog();
}
You can change what happends, based on what button is clicked, with the DialogResult
DialogResult dialogResult = MessageBox.Show(#"Some body", #"Title", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
// do stuff
}
No, a message box cannot do this. It's not meant to do this. It's meant to just display a message... in a box :)
What you can always do is create your own window that looks like a message box and behaves like a message box, but actually does things when you click the buttons.

How to connect pop up to a button?

How to connect a pup up to a button ?
I want when i click a button pup up window to appear.
I have already made the pop up with some elements child-ed in the grid in the pop up.
I've tried this but it doesn't work
private void Task_click(object sender, RoutedEventArgs e)
{
if (PopUp.Visibility == Visibility.Collapsed)
PopUp.Visibility = Visibility.Visible;
}
I think maybe other parameter has to be changed for the pup up to appear.
Thanks for the help :D
From what I understood, you are doing Windows Form programing and the popup you are talking about must be a Custom dialog box (Simpler Version's Tutorial). Assuming you have set the properties of your PopUp suitable for a dialog box, you can use following :
using (PopUp pp = new PopUp())
{
DialogResult result = pp.ShowDialog();
if (result == DialogResult.OK)
{
// do your stuff
}
}

Why the new Form is close on the second time i click OK and the textBox is empty?

private void button2_Click(object sender, EventArgs e)
{
cl = new ChangeLink();
cl.StartPosition = FormStartPosition.CenterParent;
DialogResult dr = cl.ShowDialog(this);
if (dr == DialogResult.Ok)
{
if (cl.getText() == "")
{
MessageBox.Show("The TextBox Cannot Be Empty");
cl.ShowDialog(this);
return;
}
else
{
label4.Text = cl.getText();
cl.Close();
}
}
else if (dr == DialogResult.Cancel)
{
cl.Close();
}
cl is a new Form i mgetting the text from.
Now im check that if cl.getText() is empty "" its throwing a message to the user and when the user click ok on the messagebox i want it to return and show the new Form dialog again.
When i click once on the OK button of the new Form and it show the messaeBox message and then show me again the dialogresult box of the new Form but then when i click on OK again and the textBox is still empty it doesnt show the messageBox again just close the new Form and set label4 text to be empty.
I want that each time the user click OK and the textBox is empty it will keep show the new Form textBox dialog untill the user click Cancel or put something in the textBox and then click OK.
It wiil be a lot cleaner if you do the checking in your second Form. You will need to add code to your Form2's OK Button's Click Event. Make sure you remove the default DialogResult from the OK Button Property's.
private void button2_Click(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(textBox1.Text))
DialogResult = DialogResult.OK;
}

Windows phone7 custom messagebox with a popup

In my application I have created a custom messagebox(with a different backgroud color) using a Popup. There it has a button and I have implemented the button click event.
With in the button click, it has a message box and according to "Ok" or "Cancel" it performs the operation from there onwards.
The problem is after I click the button in the popup(custom messagebox), the message box loads, but still the popup(custom messegebox) appear in the background. I need the custom popup to close entirely and to perform operations. Therefore I have used popup.IsOpen = false; as the first statement within button click, but still it appears in the background untill it finish the entire button click event. I searched for other properties, but didn't find any working.
following is the code
Popup popup = new Popup();
popup.Height = 300;
popup.Width = 480;
popup.VerticalOffset = 100;
CustomMessageBox control = new CustomMessageBox();
popup.Child = control;
popup.IsOpen = true;
this.IsEnabled = false;
control.OK_BTN.Click += (s, args) =>
{
popup.IsOpen = false;
this.IsEnabled = true;
MessageBoxResult result = MessageBox.Show("Do you want to reset the settings ?", "Settings", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
changeSettings();
}
};
Any suggestion to achieve this is highly appreciated. Thanks...!!!
The reason this happens is because all of this is happening on the UI thread which is getting blocked until you return. Until your button handler returns, no updates to the UI will happen.
If you really want the popup to disappear first, you would need to do something like
control.OK_BTN.Click += (s, args) =>
{
popup.IsOpen = false;
this.IsEnabled = true;
Dispatcher.BeginInvoke(() =>
{
MessageBoxResult result = MessageBox.Show("Do you want to reset the settings ?", "Settings", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
changeSettings();
}
});
};
That would force the MessageBox to be opened after your button handler returns. But are you sure you want the popup to disappear first? What if the user clicks cancel on the message box? Typically the message box appears directly on top of whatever invoked it and doesn't replace it.
I was just typing what Negative Eddy wrote.
Using Dispatcher.BeginInvoke will work! I tested it, works perfectly. I don't have enough rep to comment on his apparently.

MessageBox close another form

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
{
}

Categories