A question about ShowDialog() form

T

Tony Johansson

Hello!

I have a modal dialog lets call it TestDialog that is shown by using method
showDialog().

This TestDialog has three controls it's one richtextbox and two buttons.
The buttons is one Ok button and one Cancel button.

When this TestDialog is shown the user has two alternatives either write
something in the richtext box and then click the Ok button or click the
Cancel button ignoring whatever was written in the richtextbox field.

The user is not allowed to click the Ok button without writing anything in
the richtextbox. If he does some
information must be displayed telling him that some text must be written
when clicking the Ok button.

Assume the following sequence:
1. The user click the Ok button without writing anything in the richtextbox.
2. When the Ok button is clicked the TestDialog is closed.
3. I check if the Ok button is clicked and if the richtextbox is empy then I
use the MessageBox to give some information. When this information is given
from the MessageBox there is no visible TestDialog. When the user click Ok
in the MessageBox I display the TestDialog again. This works but I'm not
fully satisfied with the solution.
4. When this MessageBox is displayed the user can't do anything else because
this testDialog is modal but the user might be confused when the MessageBox
is saying something like enter text in the TestDialog when no TestDialog is
visible.

What I do want instead is if it would be possible to keep the TestDialog
visible if the user click the Ok button without writing anything in the
richtextbox. Then this MessageBox with some information is displayed above
this TestDialog given a better solution.

//Tony
 
P

Paul E Collins

Tony said:
The user is not allowed to click the Ok button
without writing anything in the richtextbox. If he
does some information must be displayed [...]

No, you should simply disable the OK button until text has been typed.

Start with it disabled, and whenever you get a TextChanged event from
the textbox, change the state of the OK button depending on whether
the box is now empty.

Eq.
 
G

Guest

Tony,

Why go through all that? Why not just disable the OK button until something
is in the RichTextBox? That way the user can't click the OK button at all,
and you have a LOT less coding to worry about, and you are already sure that
there is something in the textbox.

Just a thought.

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
 
T

Tom Spink

Tony said:
Hello!

I have a modal dialog lets call it TestDialog that is shown by using
method showDialog().

This TestDialog has three controls it's one richtextbox and two buttons.
The buttons is one Ok button and one Cancel button.

When this TestDialog is shown the user has two alternatives either write
something in the richtext box and then click the Ok button or click the
Cancel button ignoring whatever was written in the richtextbox field.

The user is not allowed to click the Ok button without writing anything in
the richtextbox. If he does some
information must be displayed telling him that some text must be written
when clicking the Ok button.

Assume the following sequence:
1. The user click the Ok button without writing anything in the
richtextbox. 2. When the Ok button is clicked the TestDialog is closed.
3. I check if the Ok button is clicked and if the richtextbox is empy then
I use the MessageBox to give some information. When this information is
given from the MessageBox there is no visible TestDialog. When the user
click Ok in the MessageBox I display the TestDialog again. This works but
I'm not fully satisfied with the solution.
4. When this MessageBox is displayed the user can't do anything else
because this testDialog is modal but the user might be confused when the
MessageBox is saying something like enter text in the TestDialog when no
TestDialog is visible.

What I do want instead is if it would be possible to keep the TestDialog
visible if the user click the Ok button without writing anything in the
richtextbox. Then this MessageBox with some information is displayed above
this TestDialog given a better solution.

//Tony

Hi Tony,

You can check the contents of the RichTextBox in the click event of your Ok
button, and then either:

1) Display your error message, and return out of the method

2) Set the DialogResult property of your form, and call the 'Close' method
on your form.

If you are using the 'DialogResult' property of the button itself to cause
automatic closing of the form, you'll need to take that off of your Ok
button.

Here's a short sample:

///
private void OnOkClicked ( object s, EventArgs e )
{
if ( rtb.Text.Length == 0 )
{
MessageBox.Show( "Please enter some text" );
return;
}

this.DialogResult = DialogResult.Ok;
this.Close();
}
///

(This was typed straight into my news reader... I haven't tested it <g>)
 

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