By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,079 Members | 1,747 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,079 IT Pros & Developers. It's quick & easy.

Best Way To Return Contents Of A Form

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.