473,785 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16211

"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.ShowDialo g();
SomeVariable = oForm.StringDat a;

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.T ext;
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 returnSoeIdValu e(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(o Obj){
sSOEID = window.showModa lDialog("soeidS earch.asp",oObj .id,"dialogHeig ht:
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=retriev eSoeid(this)>
<input type=button value="Click me for a modal box"
onClick=retriev eSoeid(theTextB ox) 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 MainFormReferen ce

public void PopupForm(MainF orm ParentRefToMain Form):this()
{
this.MyParentFo rm = ParentRefToMain Form;
}

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.WindowSt ate=FormWindowS tate.Normal;
myform.StartPos ition =
System.Windows. Forms.FormStart Position.Center Screen;
myform.ShowDial og ();
}
if (myform.DialogR esult == DialogResult.OK &&
myform.SomeLabe l.ToString().Tr im() != "" )
{
this.MainFormLa bel.Text =
myform.SomeLabe lPropertyWhichR eadsThePopupDat aToBePassedBack ToMainForm;
}
}

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)h otmail(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>" <scottandsuzann e<rem> wrote in message
news:%2******** ********@TK2MSF TNGP11.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 returnSoeIdValu e(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(o Obj){
sSOEID = window.showModa lDialog("soeidS earch.asp",oObj .id,"dialogHeig ht:
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=retriev eSoeid(this)>
<input type=button value="Click me for a modal box"
onClick=retriev eSoeid(theTextB ox) 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.gb l...

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

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

Similar topics

4
8537
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 unique session ID in a cookie and referencing the SSN from the Session ID would be the correct way to do this. But since we have two separate servers, IIS and Websphere, how do we coordinate the session ID?
2
2548
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, radio, checkbox, and dropdown. When the form Submit button is clicked, I then need the input data either written to another location on the same page, or written to another page (a different frame would be fine)
5
6701
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 & "<br>"%> Exp <%'Response.Write GrantID & "<br>"%>
9
2171
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 type="text/javascript"> function radioenable(value) { document.forms.search.elements.value.disabled=false;
3
5562
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 an array-variable and pass this variable to a subroutine in which I make all stored objects in the array-variable invisible. Can somebody explain me how to declare the correct variables, how to pass these to a sub routine and how to make these...
2
3684
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 Form2 to Form1? Thanks,
11
3483
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 click a button to determine whether the zip code is unique. If the zip code is not unique, another form/dialog is displayed (fclsLookup) - lookup form/dialog. The zip code and zipid are both passed to the lookup form/dialog by reference. I...
8
4416
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 click a button to determine whether the zip code is unique. If the zip code is not unique, another form/dialog is displayed (fclsLookup) - lookup form/dialog. The zip code is passed to the lookup form/dialog by reference. I then load a...
6
3259
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 query-string flag is used to indicate to the page whether it should instantiate a new instance of the object or access an existing instance from the calling page. On the both pages I have a property of the page which is an instance of this custom...
7
8190
by: The Doctor | last post by:
A rather elementary question, In VB5, how can I pass a variable from one form to another?
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.