Custom dialog window.. Hmm

  • Thread starter =?ISO-8859-1?Q?=D8yvind_J=E6gtnes?=
  • Start date
?

=?ISO-8859-1?Q?=D8yvind_J=E6gtnes?=

I have a main form and a second form where i have set a ok button to
AcceptButton and a cancel button to CancelButton in the properties view.

I launch it with:

TheDialog dlg = new TheDialog();
dlg.ShowDialog();

Now it shows. I can click the cancel button or press escape and the form
closes with DialogResult.Cancel as result.

But if i click the Ok button or press enter, nothing happens :p

In the code generated by designer it sais:
this.AcceptButton = this.btnOk;
this.CancelButton = this.btnCancel;

Am i doing something wrong or does this look like a bug?

Im using Visual C# express 2005 and .net 2.0 beta things.
 
E

Eric Renken

Double click on the OK button to create the click event for the button.
Then in this method type:

this.DialogResult = DialogResult.OK;
 
K

Knut Vonheim

I had the same issue in .Net 1.1 SP1 with an inherited button assigned as
the Accept button. I, too, had to set the dialog result in the clicked event
of the button.

Does anyone have an explanation for this?

TYIA,
Knut
 
?

=?ISO-8859-1?Q?=D8yvind_J=E6gtnes?=

Eric said:
Double click on the OK button to create the click event for the button.
Then in this method type:

this.DialogResult = DialogResult.OK;

Yeah, that works. But seems strange that the cancel button is working
and ok button is not working ;)

I opened a bug at msdn pages.
http://tinyurl.com/54goh
It needs more votes to be looked at by Microsoft.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi,

You don't have to create event handler if you don't do anything special, but
close the form.

Buttons have one property DialogResult. You can set that property and if
it's different then DialogResult.None when you press the button it will
close the form with that value set.

So why does it work for Cancel, but not for Ok button?
That's because when you assign a button to the CancelButton the button gets
its DialogResult set to DialogResult.Cancel value. It doesn't happen for
AcceptButton though.
The reason behind this I guess is because in most of the cases the Cancel
button does actually close the form. Accept button (default button) on the
other hand may not close the form. It might be an 'Apply' button for
example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top