473,320 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Passing variables between forms.

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?
Nov 15 '05 #1
10 16170

"Steve" <ho******@cmmts.com> wrote in message
news:0b****************************@phx.gbl...
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
Nov 15 '05 #2
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
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);
...
}
--
WBR, Roman S. Golubin
ICQ UIN 63253392
go****************@arhcity.ru
Nov 15 '05 #4
Hans

that is interesting. When does the form get destroyed?
-----Original Message-----

"Steve" <ho******@cmmts.com> wrote in message
news:0b****************************@phx.gbl...
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 readproperties
of that form.

Hans Kesting
.

Nov 15 '05 #5
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" <ho******@cmmts.com> wrote in message
news:0b****************************@phx.gbl...
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?

Nov 15 '05 #6
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.CenterScree n;
myform.ShowDialog ();
}
if (myform.DialogResult == DialogResult.OK &&
myform.SomeLabel.ToString().Trim() != "" )
{
this.MainFormLabel.Text =
myform.SomeLabelPropertyWhichReadsThePopupDataToBe PassedBackToMainForm;
}
}

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.
--
Tommie Carter
tcarternyc(at)hotmail(dot)com
--
"Steve" <ho******@cmmts.com> wrote in message news:<0b****************************@phx.gbl>...
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?

Nov 15 '05 #7
Of course I was talking about HTML...... sorry if this answer was off topic.
"Scott Reynolds @eircom.net>" <scottandsuzanne<rem> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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" <ho******@cmmts.com> wrote in message
news:0b****************************@phx.gbl...
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?


Nov 15 '05 #8
Steve wrote:
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.
-----Original Message-----

"Steve" <ho******@cmmts.com> wrote in message
news:0b****************************@phx.gbl...
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
.


Nov 15 '05 #9
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-----
Steve wrote:
Hans

that is interesting. When does the form get destroyed?

The Windows resources for the dialog get destroyed

when 'Close' or 'Dispose' is called orwhen the dialog is closed.
The memory for the properties/internals of .Net object is freed automatically by garbage collectorwhen it is not needed.
So, you can still read properties of a 'Disposed/Destroyed' form. Though, it cannot be shown
twice.
-----Original Message-----

"Steve" <ho******@cmmts.com> wrote in message
news:0b****************************@phx.gbl.. .

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
.


.

Nov 15 '05 #10
Steve. wrote:
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]

.


Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jason Us | last post by:
Does anyone have experience with passing variables from an ASP page to a JSP page. The way it currently works in passing the SSN in the URL. This cannot be good. I thought that storing a...
2
by: Richard | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** HI, I am working on a project where I need to input data to a (local) HTML page using multiple form elements, such as text,...
5
by: Jack | last post by:
Hi, I need to pass multple variables in a link in order to go to a asp page with the two varables. The following are the values of the variables using response.write: <%'Response.Write Mypage...
9
by: Max | last post by:
I'm new with Javascript and can't seem to figure out what I'm doing wrong here as I'm not able to pass a simple variable to a function. In the head of doc I have: <script...
3
by: SV | last post by:
Dear all, In my application I have a lot of hidden fields. I want to make them invisible for the users though for debugging reasons I want to make them visible. So I want to add these objects to...
2
by: Michael C | last post by:
What's the best wat to pass data between two separate forms. For instance, Form1 invokes Form2 like this: Form z = new Form2(); z.Show(); How can I pass back strings and boolean values from...
11
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
7
by: The Doctor | last post by:
A rather elementary question, In VB5, how can I pass a variable from one form to another?
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.