Passing variables between forms.

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

Can anyone recommend the best way to pass a string back to
the calling class (windows form) from a dialog that was
shown modally using ShowDialog?
 
Steve said:
Can anyone recommend the best way to pass a string back to
the calling class (windows form) from a dialog that was
shown modally using ShowDialog?

When your (modal) for closes, it is not destroyed. You can still read
properties
of that form.

Hans Kesting
 
Hello Steve!
Can anyone recommend the best way to pass a string back to
the calling class (windows form) from a dialog that was
shown modally using ShowDialog?

Try to overload the ShowDialog() method as ShowDialog(out string outstring)
for shown modally form and fill variable outstring before them return:

public class MyDialog : Form
{
public DialogResult ShowDialog(out string outstring)
{
DialogResult ret;
ret = this.ShowDialog();
outstring= this.TextBox1.Text;
return ret;
}
}


public class CallerForm : Form
{
...
string outstring;
MyDialog dlg = new MyDialog();
dlg.ShowDialog(out outstring);
MessageBox.Show(outstring);
...
}
 
Hi,

You can make the string a public property of the class implementing the
second form, then before calling ShowDialog() you can assign a value the
the property and on return from ShowDialog() you can read the value from
the property.

MyForm oForm = new MyForm();

oForm.ShowDialog();
SomeVariable = oForm.StringData;

Hope this helps

Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
 
From the Modal Window use the return value ==== Look at msdn but this can be
any value or object such as an array of values. Don't set the document
opener to return the value such as opener.document.forms[0].value =
stringToReturn because then you tie yourself to using the popup form value.

function returnSoeIdValue(sSOEID){
returnValue = sSOEID;
window.close();
}

In the calling / opener page use code such as the following. I usually
passs the object which I want to set the value of into a function such as
the following. therefore I can use the modal page on more than one page on
my site. Don't forget to check the value before assigning it.

function retrieveSoeid(oObj){
sSOEID = window.showModalDialog("soeidSearch.asp",oObj.id,"dialogHeight:
400px; dialogWidth: 600px; center: Yes;help: No; resizable: No; status:
No;");
if(sSOEID){
oObj.value =sSOEID;
}else{
oObj.value ='';
}
}


and finally a little HTML code to start page: (you can use readonly so the
text can't be edited on the page.)

<input type=text id=theTextBox readonly onclick=retrieveSoeid(this)>
<input type=button value="Click me for a modal box"
onClick=retrieveSoeid(theTextBox) id=theButton>

I hope that amswers your question. Aim to reuse as much code as possible on
other pages. None of the modal stuff will work on any browser other than IE
5.5 and above. Netscape doesnot support the modal.
 
Steve,

There are at least two ways to address this --

Either will work just as well.

For this example lets say we have MainForm and PopupForm. In case 1
you may add a constructor to PopupForm (it should inherit the this()
constructor so that it will override the main constructor properly)
that accepts a reference to the MainForm and then simply reference
conrtols on the MainForm using that passed reference (of the MainForm)
-- You can also setup a reference to the PopupForm in the MainForm and
use that to extract information.

i.e.
class PopupForm
{
....

private MainForm MyParentForm; //Field to hold MainFormReference

public void PopupForm(MainForm ParentRefToMainForm):this()
{
this.MyParentForm = ParentRefToMainForm;
}

next when you call this PopupForm in the MainForm class do something
like this:
class MainForm
{
....
function PopupFormButton_OnClick()
{
PopupForm myform = new PopupForm(this);
myform.WindowState=FormWindowState.Normal;
myform.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;
myform.ShowDialog ();
}
if (myform.DialogResult == DialogResult.OK &&
myform.SomeLabel.ToString().Trim() != "" )
{
this.MainFormLabel.Text =
myform.SomeLabelPropertyWhichReadsThePopupDataToBePassedBackToMainForm;
}
}

Case # 2 -

Setup properties on either form and reference the field information
you want to pass using the properties (can be static or instanced
values). You should setup fields in both forms to hold the data you
need to pass.

I would suggest (if you need to pass datasets that you do so as
reference values to ensure that updates are posted to the form quickly
and without consuming too many resources.
 
Of course I was talking about HTML...... sorry if this answer was off topic.


Scott Reynolds @eircom.net> said:
From the Modal Window use the return value ==== Look at msdn but this can be
any value or object such as an array of values. Don't set the document
opener to return the value such as opener.document.forms[0].value =
stringToReturn because then you tie yourself to using the popup form value.

function returnSoeIdValue(sSOEID){
returnValue = sSOEID;
window.close();
}

In the calling / opener page use code such as the following. I usually
passs the object which I want to set the value of into a function such as
the following. therefore I can use the modal page on more than one page on
my site. Don't forget to check the value before assigning it.

function retrieveSoeid(oObj){
sSOEID = window.showModalDialog("soeidSearch.asp",oObj.id,"dialogHeight:
400px; dialogWidth: 600px; center: Yes;help: No; resizable: No; status:
No;");
if(sSOEID){
oObj.value =sSOEID;
}else{
oObj.value ='';
}
}


and finally a little HTML code to start page: (you can use readonly so the
text can't be edited on the page.)

<input type=text id=theTextBox readonly onclick=retrieveSoeid(this)>
<input type=button value="Click me for a modal box"
onClick=retrieveSoeid(theTextBox) id=theButton>

I hope that amswers your question. Aim to reuse as much code as possible on
other pages. None of the modal stuff will work on any browser other than IE
5.5 and above. Netscape doesnot support the modal.

Steve said:
Can anyone recommend the best way to pass a string back to
the calling class (windows form) from a dialog that was
shown modally using ShowDialog?
 
Steve said:
Hans

that is interesting. When does the form get destroyed?

The Windows resources for the dialog get destroyed when 'Close' or 'Dispose' is called or
when the dialog is closed.
The memory for the properties/internals of .Net object is freed automatically by garbage collector
when it is not needed.
So, you can still read properties of a 'Disposed/Destroyed' form. Though, it cannot be shown twice.
 
Hans,

So what you're saying is that if I show a dialog from a
function I have access to any variables contained in the
dialog until the function exits (presuming that all
references to the dialog where private within the function
and not public in the class). After the function exits the
references to the dialog are destroyed and the garbage
collector is free to destroy the dialog variables at any
time. Is this correct?
-----Original Message-----


The Windows resources for the dialog get destroyed
when 'Close' or 'Dispose' is called or
when the dialog is closed.
The memory for the properties/internals of .Net object is
freed automatically by garbage collector
when it is not needed.
So, you can still read properties of
a 'Disposed/Destroyed' form. Though, it cannot be shown
twice.
 
Steve. said:
Hans,

So what you're saying is that if I show a dialog from a
function I have access to any variables contained in the
dialog until the function exits (presuming that all
references to the dialog where private within the function
and not public in the class). After the function exits the
references to the dialog are destroyed and the garbage
collector is free to destroy the dialog variables at any
time. Is this correct?
Exactly. Even More, you can forward properties of the dialog to properties of the controls and
use them directly from the calling function. consider the following example:

using System;
using System.Drawing;
using System.Windows.Forms;


public class Dialog : Form {
private TextBox edit = new TextBox();

public Dialog() {
Size = new Size(400,300);
edit.Bounds = new Rectangle(10, 10, 100, 30);
edit.Show();

Controls.Add(edit);
}

public string EditText {
get {
return edit.Text;
}
set {
edit.Text = value;
}
}
}

public class Application
{
public static void Main()
{
Dialog dlg = new Dialog();
dlg.EditText = "Hello";
dlg.ShowDialog();
MessageBox.Show(string.Format("Text was {0}", dlg.EditText));
}
}
-----Original Message-----
Steve wrote:
[Skipped]
 
Back
Top