I'm using code from this example: Messagebox with input field
Once the user clicks the button on the form, nothing happens. The form remains. Do I need to wireup something for the modal to go away so I can get the textbox result?
public void ShowMyDialogBox()
{
Form2 testDialog = new Form2();
// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialog's TextBox.
this.txtResult.Text = testDialog.TextBox1.Text;
}
else
{
this.txtResult.Text = "Cancelled";
}
testDialog.Dispose();
}
To get the form to close, you'll need to call Close(); within the button clicked event on Form2.
Related
In my project, i have used to show the Form as dialog by using "ShowDialog()" method. But it throws below exception
"Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog."
Form frm = new Form();
frm.ShowDialog();
Please let me know the reason of this exception. Is it possible?
Thanks.
If you are using MDI and Form is a child form then error is telling you that the child form is not top-level form which needs to interact with the parent form. "A modal form means it must be closed or hidden before you can continue working with the rest of application". This is why using ShowDialog() will have this error and where #S.Petrosov's answer comes in and where you should use Show() instead.
You need to pass owner Form to Form.ShowDialog. As shown below:
public void ShowMyDialogBox()
{
Form2 testDialog = new Form2();
// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialog's TextBox.
this.txtResult.Text = testDialog.TextBox1.Text;
}
else
{
this.txtResult.Text = "Cancelled";
}
testDialog.Dispose();
}
I have a (login) form that use it as modal like this (parent form code):
using (var login = new Login())
{
login.ShowDialog();
}
I do some checks on opened modal dialog and I want that it not close on pressing OK button if user name and password was wrong.
My login:
private void goSignIn_Click(object sender, EventArgs e)
{
var loggedInCustomer =LoginController.signIn(usernameBox.Text, passwordBox.Text);
if (loggedInCustomer == null)
{
MessageBox.Show("Wrong username or password! :( ", "Wrong!");
}
else
Close();
}
Check the button (goSignIn) DialogResult property. If it's set, it automatically close the form.
If the DialogResult for this property is set to anything other than None, and if the parent form was displayed through the ShowDialog method, clicking the button closes the parent form without your having to hook up any events. The form's DialogResult property is then set to the DialogResult of the button when the button is clicked.
MSDN Button.DialogResult Property
I guess you've set the DialogResult of goSignIn button to some value(probably DialogResult.OK), remove that line, everything should work fine as expected.
Don't call close then
private void goSignIn_Click(object sender, EventArgs e)
{
var loggedInCustomer =LoginController.signIn(usernameBox.Text, passwordBox.Text);
if (loggedInCustomer == null)
{
MessageBox.Show("Wrong username or password! :( ", "Wrong!");
}
else
{
Close();
}
}
I have a "main" form (form1).
Within that form I successfully open another form (form2) - but - when that form is open, you can still control the underlying form (form1).
I don't want the user to be able to do so, and whenever he tries to click on it or something, the popped-up form (form2) should gain focus/flash a bit, with some default Windows sound.
I've just described how it acts if the popup window is a FileSave/OpenDialog. Those dialogs work exactly as I want them to work.
I've tried setting form2.Owner = form1; but that did not result in the desired effect.
So my question basically is: how can I get the same effect of focus/etc. on a form - just how it is on a Save/OpenDialog?
Thanks,
~ Tgys
To open the form2 use form2.ShowDialog()
In form1 class
form2 form2Object = new form2();
form2.ShowDialog(this);
This way form1 will remain in the background and un-clickable until form2 is shown. You can also return the dialog result from form2 if you want. Code from MSDN.
Form2 testDialog = new Form2();
// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialog's TextBox.
this.txtResult.Text = testDialog.TextBox1.Text;
}
else
{
this.txtResult.Text = "Cancelled";
}
I have a form. In that I got to show a dialog (on some circumstances) with Text and a Cancel button. I want to catch the event of that button in my form Or know if that cancel button was clicked.
How can this be done ? I believe this should be possible but can't make out how ?
From my mainForm I have BackgroundWorker. When the backgroundWorker is started I open a childForm (with a Label and a button) and when the background task is over, I close the childForm. What I want more is : when the button of childForm is clicked the ongoing task of backgroundWorker should be cancelled.
SOLUTION
In my childForm I have set CancelButton property as cancelBtn for the form. The othe code is :
private bool cancel;
public bool Cancel
{
get { return cancel; }
set { cancel = value; }
}
// Set the flag as true to indicate that Cancel button was actually pressed
private void cancelBtn_Click(object sender, EventArgs e)
{
Cancel = true;
}
In mainForm :
childDlg = new ChildDialog();
// wHILE cALLING
backgroundWorker1.RunWorkerAsync();
msg = "Connecting...";
childDlg .set(msg, "");
if (!childDlg .IsAccessible)
{
// This is caught even when the dialog is closed
if (childDlg .ShowDialog() == DialogResult.Cancel) {
if (childDlg.Cancel == true) { // Was really cancelBtn pressed
// NOW ONLY do my stuff
}
}
}
I had tried using #DeveloperX technique i.e. EventHandler in parent class, but the parent class method was nver being called. Tried a lot but couldn't success. Then tried of #RobinJ's technique and it worked. I just had to add flag to identify was really cancel button pressed or jjst windw was closed normally.
Thanks to all of you for tryig to help me out. I really appreciate your help.
Thanks
Set DialogResult property to either DialogResult.Ok or DialogResult.Cancel
Then in the parent form:
Form form = new Form();
DialogResult results = form.DialogResult;
if(results == DialogResult.Ok)
{
... make magic
}
else
{
...
}
Put this in the form that should catch the event:
frmDialog.ShowDialog();
And this in the btnCancel_Click event of the dialog:
return DialogResult.Cancel();
Sorry if I'm confusing VB and C# here, but it's pretty much the same.
Simply you can create an event for the form that shows the dialog
and handle this event in parent form
in case the user clicks on ok fire event with specefic parameter and for cancel another parameter (such dialogresult.cancel)
an pseudo implementation can be like this
public class FormChild : System.Windows.Forms.Form
{
public event EventHandler DialogCanceled;
public event EventHandler DialogConfirmed;
public void ShowDialog()
{
using (var dialogForm = new FormDialog())
{
if (dialogForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (DialogConfirmed != null)
DialogConfirmed(this,new EventArgs());
}
else
{
if (DialogCanceled != null)
DialogCanceled(this,new EventArgs());
}
}
}
}
public class ParentForm : System.Windows.Forms.Form
{
public void callChild()
{
using (var f = new FormChild())
{
f.DialogCanceled += new EventHandler(f_DialogCanceled);
f.DialogConfirmed += new EventHandler(f_DialogConfirmed);
f.ShowDialog();
}
}
void f_DialogConfirmed(object sender, EventArgs e)
{
throw new NotImplementedException();
}
void f_DialogCanceled(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
You should be using the ShowDialog method on the form you need to show as a dialog and then use the DialogResult property to communicate to the parent form the result of the dialog operation.
This way you handle the button click on the form that owns the button but set the DialogResult to DialogResult.Cancel to specify that the user pressed the cancel button.
A dialog is usually a blocking event, where eventhandling by the parent form would make no sense at all.
If it isn't a modal dialog, you can always create one or several public events in the popup form, that are triggered when the buttons are clicked. These events can then be caught by the parent form.
Don't expose your buttons to the parent form, it would be terrible oo-programming.
Use the following:
Form form = new Form();
switch (form.ShowDialog())
{
case DialogResult.Ok:
{
....
Break;
}
case DialogResult.Cancel:
{
....
Break;
}
}
Set the Form.AcceptButton and Form.CancelButton properties to the appropriate buttons.
Refer to the following:
Form.ShowDialog Method
DialogResult Enumeration
Form.AcceptButton Property
Form.CancelButton Property
I have some custom made dialog that have on it Set Button , I want when i exit from newBlockForm.ShowDialog(this); to get the dialog result if the user pressed on that button or not .
Like i would do in winforms dialog
if(MessageBox.Show("Exit?", "Close UP",
MessageBoxButtons.YesNo)== DialogResult.Yes)
Any idea how i do so ?
You can use the DialogResult Property of the Button on your Dialog form and set it to DialogResult Enumeration like:
//in your dialog form
button1.DialogResult = DialogResult.OK;
then in your main form :
//Create an instance of your dialog form
Form2 testDialog = new Form2();
// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
//do processing
}
else
{
//do processing
}
Map the AcceptButton property on the Form to Set button in the designer.
Or in the Set button click handler you could set some value.
private void HandleOnSetButtonClick(object sender, EventArgs e)
{
this.IsSetClicked = true;
this.Close();
//or
this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
public Boolean IsSetClicked
{
get;
private set;
}
ShowDialog method returns a DialogResult by itself which you can set automatically by setting the DialogResult of the buttons in question in your form designer.