BlackStone Posted June 15, 2004 Posted June 15, 2004 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. Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
*Experts* mutant Posted June 16, 2004 *Experts* Posted June 16, 2004 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. Quote
BlackStone Posted June 16, 2004 Author Posted June 16, 2004 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). Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
*Experts* Nerseus Posted June 16, 2004 *Experts* Posted June 16, 2004 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 Quote "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
Arch4ngel Posted June 16, 2004 Posted June 16, 2004 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 Quote "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* Nerseus Posted June 17, 2004 *Experts* Posted June 17, 2004 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 Quote "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
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.