Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
How do you let a form that is acting like a dialog box recive input like a messagebox and give a result like an input box? I need it to be able to receive a filename, text, and an image, and return text back.
"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
  • *Experts*
Posted
By "like messagebox" you mean returing a dialog result? Set the button that you want to return a given result, and modify its DialogResult property. To return something, create a property that will return a value of an internal variable which you set when you have the value ready. Then after you close the form, you can access the property since dialogs are not disposed right after they are closed.
Posted
I did that to a string I called Entry. However, it always was set to "" when it returned(I checked it at debug time, and the internal variable(mEntry) was accessible).
"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
  • *Experts*
Posted

Suppose you have a form named MyDialog with a public string Entry. Make sure the "Ok" button (btnOk below) has it's DialogResult property set to "Ok" or set it manually in code, in the click event. Should be no need to set the Close button's DialogResult property since the form will use a default of either "None" or "Cancelled" or something like that. If you only care about "Ok", then that's all you need to set.

public class MyDialog : System.Windows.Form
{
   public string Entry = string.Empty;

   public MyDialog(string myFilename, string someText, Image myImage)
   {
       // Use myFilename, someText, and myImage for whatever...
   }

   private void btnOk_Click(object sender, System.EventArgs e)
   {
       // Set the public string Entry to the value of a textbox
       Entry = textBox1.Text;
       this.Close();
   }
}

 

A "close" or "cancel" button would just call this.Close. The default value of Entry would always be string.Empty unless btnOk is clicked.

 

In the calling form:

public class MyForm : System.Windows.Form
{
   private void btnOpenDialog_Click(object sender, System.EventArgs e)
   {
       Image i; // Define or get some image to pass to the popup
       MyDialog frm = new MyDialog("c:\\filename", "Hello World", i);
       DialogResult result = frm.ShowDialog();

       // Ok should only be returned if the Ok button was pressed.
       if(result==DialogResult.Ok)
       {
           // This should work...
           string myEntry = frm.Entry;
       }

       // Manually dispose all forms that use ShowDialog
       frm.Dispose();
   }
}

 

You can get some "weird" behavior from setting a button's DialogResult property. For example, if you had set btnOk's DialogResult to "Ok" above, and then had code in the Ok button's click that exited early because of non-valid data, the popup would STILL close! You'd have to remember to set the DialogResult back to None (or something) or else the form would automatically close. In fact, I don't think you even need "this.Close();" in btnOk_Click IF you have its DialogResult set.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Nerseus is totally right.

 

I would only add a comment.

 

Make Entry a read-only property instead of a direct accessed variable. Since it will never be set you won't have to permit writing.

 

Otherwise... code is perfect as usual Nerseus. :p

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

  • *Experts*
Posted

I don't know about perfect. When I post now, it's from home and I don't currently have VS to test with so things may not compile as-is. I should put that in my signature... ;)

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...