Best Way To Return Contents Of A Form

R

RubiconXing

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
 
N

Norman Yuan

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()
 
R

RubiconXing

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
 
D

Dave

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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top