473,385 Members | 1,727 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,385 software developers and data experts.

Best Way To Return Contents Of A Form

Hi All

Beginner question - please be patient with me :-) I am new to c#.

If one creates a child modal (and also non-modal) form what is the
best way of returning the collected data back to the calling form?

For example, say off of my main form I launch a modal dialog box that
gets the username and password what is the best way of passing that
data back? The info has to go into an object eventually so I can
easily stuff the data into an object in the child form. But how does
the object then get back - would I pass a reference back in an event
for this?

Is it better to pre-create the object in the main form, pass it in a
reference to the form handler before the Show() or ShowDialog call. I
suppose in a non-modal form you would have to send an event of some
sort to tell the main form to process the object.

Just not sure of the typical way this is handled - all advice
appreciated.

Thanks
-R

Jul 21 '05 #1
3 1684
You can easily add public properties to the child form and have other object
access it this way DialgForm.UserName/Password. Example:

pulic class LogInForm: Form
{
public LogInForm()
{
//Add two text boxes for user to input UserName and Password
//And set OK button's DialogResult property to OK
//set Cancel button's DialogResult property to Cancel
}

public string UserName
{
get { return txtUserName.Text; }
}

public string Password
{
get { return txtPassword.Text; }
}

//other code for this dialog form
}
In your app,

LogInForm dlg=new LogInForm()
if (dlg.ShowDialog()==DialogResult.OK)
{
string user=dlg.UserName;
string pswd=dlg.Password;
//do something
}
dlg.Dispose()
"RubiconXing" <No****@NoSpam.com> wrote in message
news:2f********************************@4ax.com...
Hi All

Beginner question - please be patient with me :-) I am new to c#.

If one creates a child modal (and also non-modal) form what is the
best way of returning the collected data back to the calling form?

For example, say off of my main form I launch a modal dialog box that
gets the username and password what is the best way of passing that
data back? The info has to go into an object eventually so I can
easily stuff the data into an object in the child form. But how does
the object then get back - would I pass a reference back in an event
for this?

Is it better to pre-create the object in the main form, pass it in a
reference to the form handler before the Show() or ShowDialog call. I
suppose in a non-modal form you would have to send an event of some
sort to tell the main form to process the object.

Just not sure of the typical way this is handled - all advice
appreciated.

Thanks
-R

Jul 21 '05 #2
Hi Norman

Thanks very much. I see this now - the form is an object and it has
properties. It can be its own container. Its just a matter of looking
at things in the right way.

This brings up another problem though - how can the form be a form and
also inherit functionality from another object. I've posted a new
question about this.

Thanks for your help.

-R

On Fri, 13 May 2005 12:56:39 -0700, "Norman Yuan"
<No*****@NotReal.not> wrote:
You can easily add public properties to the child form and have other object
access it this way DialgForm.UserName/Password. Example:

pulic class LogInForm: Form
{
public LogInForm()
{
//Add two text boxes for user to input UserName and Password
//And set OK button's DialogResult property to OK
//set Cancel button's DialogResult property to Cancel
}

public string UserName
{
get { return txtUserName.Text; }
}

public string Password
{
get { return txtPassword.Text; }
}

//other code for this dialog form
}
In your app,

LogInForm dlg=new LogInForm()
if (dlg.ShowDialog()==DialogResult.OK)
{
string user=dlg.UserName;
string pswd=dlg.Password;
//do something
}
dlg.Dispose()
"RubiconXing" <No****@NoSpam.com> wrote in message
news:2f********************************@4ax.com.. .
Hi All

Beginner question - please be patient with me :-) I am new to c#.

If one creates a child modal (and also non-modal) form what is the
best way of returning the collected data back to the calling form?

For example, say off of my main form I launch a modal dialog box that
gets the username and password what is the best way of passing that
data back? The info has to go into an object eventually so I can
easily stuff the data into an object in the child form. But how does
the object then get back - would I pass a reference back in an event
for this?

Is it better to pre-create the object in the main form, pass it in a
reference to the form handler before the Show() or ShowDialog call. I
suppose in a non-modal form you would have to send an event of some
sort to tell the main form to process the object.

Just not sure of the typical way this is handled - all advice
appreciated.

Thanks
-R


Jul 21 '05 #3
Here is a Dialog form that will provide data to all callers via a public property. An implied class, CustomData will store the
data. btnOK is the "OK" button on the form. I've implied that the onclick event for the button is handled in "btnOK_OnClick". A
button named "btnCancel" should also be created on this form. In the properties pane for ModalDiag, the AcceptButton should be set
to "btnOK" and the CancelButton should be set to "btnCancel". Setting the CancelButton will also return DialogResult.Cancel
automattically, when the button is pressed. AcceptButton does not return a DialogResult so I've added code in the OnClick event
handler to do so:

public class ModalDiag : Form
{
public CustomData Data { get { return data; } }

private CustomData data;

private void btnOK_OnClick(object sender, EventArgs e)
{
data = new CustomData(txtName.Text);
this.DialogResult = DialogResult.Ok;
}
}

Consume the dialog:

public CustomData GetDataFromUser()
{
using (ModalDiag diag = new ModalDiag())
{
if (diag.ShowDialog(this) == DialogResult.Ok)
return diag.Data;
else
// When the user cancels the dialog, a null object will be returned:
return null;
}
}

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"RubiconXing" <No****@NoSpam.com> wrote in message news:2f********************************@4ax.com...
Hi All

Beginner question - please be patient with me :-) I am new to c#.

If one creates a child modal (and also non-modal) form what is the
best way of returning the collected data back to the calling form?

For example, say off of my main form I launch a modal dialog box that
gets the username and password what is the best way of passing that
data back? The info has to go into an object eventually so I can
easily stuff the data into an object in the child form. But how does
the object then get back - would I pass a reference back in an event
for this?

Is it better to pre-create the object in the main form, pass it in a
reference to the form handler before the Show() or ShowDialog call. I
suppose in a non-modal form you would have to send an event of some
sort to tell the main form to process the object.

Just not sure of the typical way this is handled - all advice
appreciated.

Thanks
-R

Jul 21 '05 #4

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

Similar topics

6
by: Robin Siebler | last post by:
I have two directory trees that I want to compare and I'm trying to figure out what the best way of doing this would be. I am using walk to get a list of all of the files in each directory. I...
1
by: Don Grover | last post by:
Does any one have an asp page they can share that returns current session details, cookies etc. I can't get session arrays to return in a page.
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
3
by: RubiconXing | last post by:
Hi All Beginner question - please be patient with me :-) I am new to c#. If one creates a child modal (and also non-modal) form what is the best way of returning the collected data back to the...
4
by: Tarun Mistry | last post by:
Hi all, I have posted this in both the c# and asp.net groups as it applies to both (apologies if it breaks some group rules). I am making a web app in asp.net using c#. This is the first fully OO...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
13
by: G | last post by:
Hello, Looking for opinions on a fairly simple task, new to ASP.net (C#) and want to make sure I do this as efficiently as possible. I have a web based form, and I need to run some SQL before...
2
by: Ashley | last post by:
hey, what's up............................. "Neil" <nospam@nospam.netwrote in message news:8YFwj.10509$0o7.1113@newssvr13.news.prodigy.net...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.