Passing Values among Winforms in C#.net - c#

I am trying to obtain a value back to the parent form and below is the code that I was using for this and it was working fine until started loading the child-form in a panel control to avoid popup windows.
Code in the mainform which contains the Panel
MainMovement child = new MainMovement(new_dat, required_time, number);
child.TopLevel = false;
this.pnlmain.Controls.Add(child);
child.Show();
child.BringToFront();
///Obtaining value back from the child form
string updatingc = child.updatestatus; //This is not working, I am proceeding with some more functions depending on this value, but code does not work here after
Child form has a public value as updatestatus and it sets the value before closing the child form.
Please advise how to obtain this value. I believe it is something to do with changing child.ShowDialog() to child.Show(). (In order to load the form into a panel I had to change this, before that this was working fine).

You can pass the object of your main form into child form via constructor. If you pass your object, you will have access to all the methods of parent form in your child. You can call any public method of main class to update your value.
MainMovement child = new MainMovement(this,new_dat, required_time, number);
child.TopLevel = false;
this.pnlmain.Controls.Add(child);
child.ShowDialog();
child.BringToFront();
Put one public method in your main form,
Public void UpdateValue(String pString)
{
// Update your value
}
In your child form, you have to catch "this" with global object.
private oMainForm as MainForm
public void MainMovement(MainForm pObject,String new_dat, String required_time, Int number)
{
oMainForm = pObject;
// Your Code
}
Now you can simply call your 'UpdateValue' method from child form.
oMainForm.UpdateValue("Updated String");

The problem is .ShowDialog() waits for a DialogResult before continuing, whereas Show() just shows the form and continues. It is hard to say without knowing how your child form is working, but my guess is whatever updates or sets updatestatus in your child form doesn't update before your code reaches that line.
One possible solution involves a major refactoring of your code. You can add an event to your MainMovement form that is triggered when updatestatus is changed.
Note that I changed your updatestatus to UpdateStatus and turned it into a property
public MainMovement : Form
{
public event EventHandler Updated;
private void OnUpdateStatus()
{
if (Updated != null)
{
Updated(this, new EventArgs());
}
}
private String updatestatus;
public String UpdateStatus
{
get { return updatestatus; }
private set
{
updatestatus = value;
OnUpdateStatus();
}
}
// rest of your child form code
}
public ParentForm : Form
{
public void MethodInYourExample()
{
// other code?
MainMovement child = new MainMovement(new_dat, required_time, number);
child.Updated += ChildUpdated;
child.TopLevel = false;
this.pnlmain.Controls.Add(child);
child.Show();
child.BringToFront();
}
void ChildUpdated(object sender, EventArgs e)
{
var child = sender as MainMovement;
string updatingc = child.UpdateStatus;
//rest of your code
}
}

Related

How do I retrieve a custom list from another form in a using block? [duplicate]

I have a main form (let's call it frmHireQuote) that is a child of a main MDI form (frmMainMDI), that shows another form (frmImportContact) via ShowDialog() when a button is clicked.
When the user clicks the 'OK' on frmImportContact, I want to pass a few string variables back to some text boxes on frmHireQuote.
Note that there could be multiple instances of frmHireQuote, it's obviously important that I get back to the instance that called this instance of frmImportContact.
What's the best method of doing this?
Create some public Properties on your sub-form like so
public string ReturnValue1 {get;set;}
public string ReturnValue2 {get;set;}
then set this inside your sub-form ok button click handler
private void btnOk_Click(object sender,EventArgs e)
{
this.ReturnValue1 = "Something";
this.ReturnValue2 = DateTime.Now.ToString(); //example
this.DialogResult = DialogResult.OK;
this.Close();
}
Then in your frmHireQuote form, when you open the sub-form
using (var form = new frmImportContact())
{
var result = form.ShowDialog();
if (result == DialogResult.OK)
{
string val = form.ReturnValue1; //values preserved after close
string dateString = form.ReturnValue2;
//Do something here with these values
//for example
this.txtSomething.Text = val;
}
}
Additionaly if you wish to cancel out of the sub-form you can just add a button to the form and set its DialogResult to Cancel and you can also set the CancelButton property of the form to said button - this will enable the escape key to cancel out of the form.
I normally create a static method on form/dialog, that I can call. This returns the success (OK-button) or failure, along with the values that needs to be filled in.
public class ResultFromFrmMain {
public DialogResult Result { get; set; }
public string Field1 { get; set; }
}
And on the form:
public static ResultFromFrmMain Execute() {
using (var f = new frmMain()) {
var result = new ResultFromFrmMain();
result.Result = f.ShowDialog();
if (result.Result == DialogResult.OK) {
// fill other values
}
return result;
}
}
To call your form;
public void MyEventToCallForm() {
var result = frmMain.Execute();
if (result.Result == DialogResult.OK) {
myTextBox.Text = result.Field1; // or something like that
}
}
Found another small problem with this code... or at least it was problematic when I tried to implement it.
The buttons in frmMain do not return a compatible value, using VS2010 I added the following and everything started working fine.
public static ResultFromFrmMain Execute() {
using (var f = new frmMain()) {
f.buttonOK.DialogResult = DialogResult.OK;
f.buttonCancel.DialogResult = DialogResult.Cancel;
var result = new ResultFromFrmMain();
result.Result = f.ShowDialog();
if (result.Result == DialogResult.OK) {
// fill other values
}
return result;
}
}
After adding the two button values, the dialog worked great!
Thanks for the example, it really helped.
delegates are the best option for sending data from one form to another.
public partial class frmImportContact : Form
{
public delegate void callback_data(string someData);
public event callback_data getData_CallBack;
private void button_Click(object sender, EventArgs e)
{
string myData = "Top Secret Data To Share";
getData_CallBack(myData);
}
}
public partial class frmHireQuote : Form
{
private void Button_Click(object sender, EventArgs e)
{
frmImportContact obj = new frmImportContact();
obj.getData_CallBack += getData;
}
private void getData(string someData)
{
MessageBox.Show("someData");
}
}
I just put into constructor something by reference, so the subform can change its value and main form can get new or modified object from subform.
If you want to pass data to form2 from form1 without passing like new form(sting "data");
Do like that
in form 1
using (Form2 form2= new Form2())
{
form2.ReturnValue1 = "lalala";
form2.ShowDialog();
}
in form 2 add
public string ReturnValue1 { get; set; }
private void form2_Load(object sender, EventArgs e)
{
MessageBox.Show(ReturnValue1);
}
Also you can use value in form1 like this if you want to swap something in form1
just in form1
textbox.Text =form2.ReturnValue1
I use MDI quite a lot, I like it much more (where it can be used) than multiple floating forms.
But to get the best from it you need to get to grips with your own events. It makes life so much easier for you.
A skeletal example.
Have your own interupt types,
//Clock, Stock and Accoubts represent the actual forms in
//the MDI application. When I have multiple copies of a form
//I also give them an ID, at the time they are created, then
//include that ID in the Args class.
public enum InteruptSource
{
IS_CLOCK = 0, IS_STOCKS, IS_ACCOUNTS
}
//This particular event type is time based,
//but you can add others to it, such as document
//based.
public enum EVInterupts
{
CI_NEWDAY = 0, CI_NEWMONTH, CI_NEWYEAR, CI_PAYDAY, CI_STOCKPAYOUT,
CI_STOCKIN, DO_NEWEMAIL, DO_SAVETOARCHIVE
}
Then your own Args type
public class ControlArgs
{
//MDI form source
public InteruptSource source { get; set; }
//Interrupt type
public EVInterupts clockInt { get; set; }
//in this case only a date is needed
//but normally I include optional data (as if a C UNION type)
//the form that responds to the event decides if
//the data is for it.
public DateTime date { get; set; }
//CI_STOCKIN
public StockClass inStock { get; set; }
}
Then use the delegate within your namespace, but outside of a class
namespace MyApplication
{
public delegate void StoreHandler(object sender, ControlArgs e);
public partial class Form1 : Form
{
//your main form
}
Now either manually or using the GUI, have the MDIparent respond to the events of the child forms.
But with your owr Args, you can reduce this to a single function. and you can have provision to interupt the interupts, good for debugging, but can be usefull in other ways too.
Just have al of your mdiparent event codes point to the one function,
calendar.Friday += new StoreHandler(MyEvents);
calendar.Saturday += new StoreHandler(MyEvents);
calendar.Sunday += new StoreHandler(MyEvents);
calendar.PayDay += new StoreHandler(MyEvents);
calendar.NewYear += new StoreHandler(MyEvents);
A simple switch mechanism is usually enough to pass events on to appropriate forms.
First you have to define attribute in form2(child) you will update this attribute in form2 and also from form1(parent) :
public string Response { get; set; }
private void OkButton_Click(object sender, EventArgs e)
{
Response = "ok";
}
private void CancelButton_Click(object sender, EventArgs e)
{
Response = "Cancel";
}
Calling of form2(child) from form1(parent):
using (Form2 formObject= new Form2() )
{
formObject.ShowDialog();
string result = formObject.Response;
//to update response of form2 after saving in result
formObject.Response="";
// do what ever with result...
MessageBox.Show("Response from form2: "+result);
}
I raise an event in the the form setting the value and subscribe to that event in the form(s) that need to deal with the value change.

Call parent from custom control

I have a form, on this form is a flowlayoutpanel with multiple custom made TextBoxes
The form overrides the base methode Refresh(), to do some other things also.
Now I'm digging into the parent to eventueally come on the form and do the refresh
this.Parent.Parent.Parent.Refresh();
I want to re-use the control on other forms, so is there another way to do this?
And I know a While(true) is possible:
Boolean diggToParent = true;
var parent = this.Parent;
while (diggToParent)
{
if (parent.Parent != null)
{
parent = parent.Parent;
}
else
break;
}
parent.Refresh();
But is there a cleaner way to do this?
You can solve this by creating and raising an event that is handled by the parent form:
public class MyUserControl : UserControl
{
// ...
public event EventHandler RequestRefresh;
// Call this method whenever you want the parent to refresh
private void OnRequestRefresh()
{
if (RequestRefresh != null)
RequestRefresh(this, EventArgs.Empty);
}
}
In the parent form (or the container that should be refreshed), you add an event handler, e.g.
public class MyParentForm : Form
{
public MyParentForm()
{
InitializeComponent();
userCtrl.RequestRefresh += userCtrl_RequestRefresh;
}
// Do whatever the parent thinks is necessary to refresh.
public void userCtrl_RequestRefresh(object sender, EventArgs e)
{
Refresh();
}
// ...
}
This way the parent form can decide what to do when the user control requests a refresh. For details on events, see this link.

The concept of callbacks (C#)

Hi I have a form (Form A) which has a button "Add" which pops up a text-entering form (Form B). After you've entered the text in Form B, you press "OK" (btnOK). At this point I want Form A to get the text and do something with it.
From what I see I have two options:
1) Form A passes a function (delegate) to Form B which executes it in the btnOK_Click function.
2) I somehow register a method in Form A with the btnOK.Click event.
Am I on the correct track and can you think of any solutions to a similar problem that I can read because I get in trouble with static methods and events...
If Form B is a modal window, you could add a public string property to Form B.
When the user is closing Form B you could set the property to the text that was entered by the user. Then, back in Form A you could read the public property of Form B to get the text that the user entered.
Add a property to form B that is something like
public class FormB { ...
public string Text
{
get { return textBox.Text; }
}
...
}
If you show form b like follows:
FormB b = new FormB();
WPF: if (b.ShowDialog() != true) return;
Winforms: if (b.ShowDialog(this) != DialogResult.Ok) return;
string txt = b.Text;
So basically what is happening is you are showing the modal form b, where the user can enter text. Once the form is closed, b.ShowDialog will return from blocking. At that point, you can ask for b's Text property and retrieve the string. The Text property should be bound to b's textbox using some method.
If you don't want to show b modally, you can do this:
FormB b = new FormB();
b.Closed += new EventHandler(FormB_Closed);
b.Show();
private void FormB_Closed(object sender, EventArgs e)
{
string text = ((FormB)sender).Text;
... do something
}
A better solution would be to put a public property on Form B containing the text you need. Then, just access that property from Form A after Form B has exited.
I prefer #1 - passing a delegate is clean and simple
class BInputValues {
public String Field1 { get; set; }
//...
};
partial class FormB {
readonly Action<BInputValues> callback;
public FormB(Action<BInputValues> callback) {
this.callback = callback;
}
protected override void btnOK_Click(object sender, EventArgs e) {
callback(new BInputValues {
Field1 = Field1.Text,
//...
});
}
}
override void btnAdd_click() {
var formb = new FormB(args => {
// do something with args
});
formb.ShowModal();
}
You could also create a Subclass of Form...
public class FormWithResult : Form
{
protected object FormResult { get; set; }
public DialogResult ShowDialog(out object result)
{
DialogResult dr = ShowDialog();
result = FormResult;
return dr;
}
public DialogResult ShowDialog(out object result, IWin32Window win)
{
DialogResult dr = ShowDialog(win);
result = FormResult;
return dr;
}
public void Return(object result)
{
FormResult = result;
Close();
}
}
Then you can write this to Call a modal form and retrieve a result
popup p = new popup();
object result;
p.ShowDialog(out result);
MessageBox.Show((string)result);
And in your popup form you can either do:
FormResult = textBox1.Text;
Close();
OR
Return(textBox1.Text);
To Close the form and return the value.
Subclassing Form has drawbacks as well of course, but I'll throw this out as another solution.
As a side not, a generic version of this where you could Strongly type the return value would be much better if it weren't for this limitation at design time: Visual Studio 2008 Winform designer fails to load Form which inherits from generic class
If you wanted Asynchronous results obviously this would have to be tweaked. I assume you are using Modal popups.

C# Passing values from one form to another open form

I am learning the C# programming language and am making a Payroll program addon for SAP Business One. I have two forms and I want to pass a value from PayrollFormulaBuilder.cs to EarnDeductSetup.cs.
The PayrollFormulaBuilder is used by a user to generate a formula and is saved to a string. A user clicks on a calculator button on the EarnDeductSetup form in order to open up the PayrollFormulaBuilder form. The EarnDeductSetup form is still open but in the background. I want the generated formula to show on my EarnDeductSetup form (I have a textbox, txt_formula_template.Text) as soon as a user clicks on an 'Apply button on the PayrollFormulaBuilder form. i would also like for this PayrollFormulaBuilder form to close as soon as the apply button is pressed.
Right now, I am unable to show the generated formula on my EarnDeductSetup form
My Code: (EarnDeductSetupForm)
namespace EIM_Payroll_Application
{
public partial class EarnDeductSetupForm : Form
{
private SAPbobsCOM.Company company;
public string SAPCodePD { get; set; }
public EarnDeductSetupForm(SAPbobsCOM.Company co)
{
InitializeComponent();
this.company = co;
}
...
private void btn_calculator_Click(object sender, EventArgs e)
{
PayrollFormulaBuilder PC = new PayrollFormulaBuilder();
PC.ShowDialog();
}
...
if (rb_calculated_amt.Checked == true)
{
txt_formula_template.Text = SAPUtility._formulaVariable;
formulaTemplate = txt_formula_template.Text;
}
SAPUtility.cs
namespace Payroll.Util.Helpers
{
public static class SAPUtility
{
public static string _formulaVariable = String.Empty;
public static string variable
{
get { return _formulaVariable; }
set { _formulaVariable = value; }
}
...
PayrollFormulaBuilder.cs
private void btnApply_Click(object sender, EventArgs e)
{
SAPUtility._formulaVariable = formula_display.Text;
this.Close();
EarnDeductSetupForm.ActiveForm.ShowDialog();
}
My question is, how do I get this formula to show on my txt_formula_template.Text textbox on my EarnDeductSetupForm as soon as a user presses apply on the PayrollFormulaBuilder form?
Set the button in the parent form to internal, then use the parent or parentform property in the dialog to access it, by casting it to the parent for type (this should expose the internal button with intellisense). Call ShowDialog with "this" as parameter (no quotes).
Subscribe to events on the internal button the parent form, in the child dialog. And do your thing in the event handler.

How to return a value from a Form in C#?

I have a main form (let's call it frmHireQuote) that is a child of a main MDI form (frmMainMDI), that shows another form (frmImportContact) via ShowDialog() when a button is clicked.
When the user clicks the 'OK' on frmImportContact, I want to pass a few string variables back to some text boxes on frmHireQuote.
Note that there could be multiple instances of frmHireQuote, it's obviously important that I get back to the instance that called this instance of frmImportContact.
What's the best method of doing this?
Create some public Properties on your sub-form like so
public string ReturnValue1 {get;set;}
public string ReturnValue2 {get;set;}
then set this inside your sub-form ok button click handler
private void btnOk_Click(object sender,EventArgs e)
{
this.ReturnValue1 = "Something";
this.ReturnValue2 = DateTime.Now.ToString(); //example
this.DialogResult = DialogResult.OK;
this.Close();
}
Then in your frmHireQuote form, when you open the sub-form
using (var form = new frmImportContact())
{
var result = form.ShowDialog();
if (result == DialogResult.OK)
{
string val = form.ReturnValue1; //values preserved after close
string dateString = form.ReturnValue2;
//Do something here with these values
//for example
this.txtSomething.Text = val;
}
}
Additionaly if you wish to cancel out of the sub-form you can just add a button to the form and set its DialogResult to Cancel and you can also set the CancelButton property of the form to said button - this will enable the escape key to cancel out of the form.
I normally create a static method on form/dialog, that I can call. This returns the success (OK-button) or failure, along with the values that needs to be filled in.
public class ResultFromFrmMain {
public DialogResult Result { get; set; }
public string Field1 { get; set; }
}
And on the form:
public static ResultFromFrmMain Execute() {
using (var f = new frmMain()) {
var result = new ResultFromFrmMain();
result.Result = f.ShowDialog();
if (result.Result == DialogResult.OK) {
// fill other values
}
return result;
}
}
To call your form;
public void MyEventToCallForm() {
var result = frmMain.Execute();
if (result.Result == DialogResult.OK) {
myTextBox.Text = result.Field1; // or something like that
}
}
Found another small problem with this code... or at least it was problematic when I tried to implement it.
The buttons in frmMain do not return a compatible value, using VS2010 I added the following and everything started working fine.
public static ResultFromFrmMain Execute() {
using (var f = new frmMain()) {
f.buttonOK.DialogResult = DialogResult.OK;
f.buttonCancel.DialogResult = DialogResult.Cancel;
var result = new ResultFromFrmMain();
result.Result = f.ShowDialog();
if (result.Result == DialogResult.OK) {
// fill other values
}
return result;
}
}
After adding the two button values, the dialog worked great!
Thanks for the example, it really helped.
delegates are the best option for sending data from one form to another.
public partial class frmImportContact : Form
{
public delegate void callback_data(string someData);
public event callback_data getData_CallBack;
private void button_Click(object sender, EventArgs e)
{
string myData = "Top Secret Data To Share";
getData_CallBack(myData);
}
}
public partial class frmHireQuote : Form
{
private void Button_Click(object sender, EventArgs e)
{
frmImportContact obj = new frmImportContact();
obj.getData_CallBack += getData;
}
private void getData(string someData)
{
MessageBox.Show("someData");
}
}
I just put into constructor something by reference, so the subform can change its value and main form can get new or modified object from subform.
If you want to pass data to form2 from form1 without passing like new form(sting "data");
Do like that
in form 1
using (Form2 form2= new Form2())
{
form2.ReturnValue1 = "lalala";
form2.ShowDialog();
}
in form 2 add
public string ReturnValue1 { get; set; }
private void form2_Load(object sender, EventArgs e)
{
MessageBox.Show(ReturnValue1);
}
Also you can use value in form1 like this if you want to swap something in form1
just in form1
textbox.Text =form2.ReturnValue1
I use MDI quite a lot, I like it much more (where it can be used) than multiple floating forms.
But to get the best from it you need to get to grips with your own events. It makes life so much easier for you.
A skeletal example.
Have your own interupt types,
//Clock, Stock and Accoubts represent the actual forms in
//the MDI application. When I have multiple copies of a form
//I also give them an ID, at the time they are created, then
//include that ID in the Args class.
public enum InteruptSource
{
IS_CLOCK = 0, IS_STOCKS, IS_ACCOUNTS
}
//This particular event type is time based,
//but you can add others to it, such as document
//based.
public enum EVInterupts
{
CI_NEWDAY = 0, CI_NEWMONTH, CI_NEWYEAR, CI_PAYDAY, CI_STOCKPAYOUT,
CI_STOCKIN, DO_NEWEMAIL, DO_SAVETOARCHIVE
}
Then your own Args type
public class ControlArgs
{
//MDI form source
public InteruptSource source { get; set; }
//Interrupt type
public EVInterupts clockInt { get; set; }
//in this case only a date is needed
//but normally I include optional data (as if a C UNION type)
//the form that responds to the event decides if
//the data is for it.
public DateTime date { get; set; }
//CI_STOCKIN
public StockClass inStock { get; set; }
}
Then use the delegate within your namespace, but outside of a class
namespace MyApplication
{
public delegate void StoreHandler(object sender, ControlArgs e);
public partial class Form1 : Form
{
//your main form
}
Now either manually or using the GUI, have the MDIparent respond to the events of the child forms.
But with your owr Args, you can reduce this to a single function. and you can have provision to interupt the interupts, good for debugging, but can be usefull in other ways too.
Just have al of your mdiparent event codes point to the one function,
calendar.Friday += new StoreHandler(MyEvents);
calendar.Saturday += new StoreHandler(MyEvents);
calendar.Sunday += new StoreHandler(MyEvents);
calendar.PayDay += new StoreHandler(MyEvents);
calendar.NewYear += new StoreHandler(MyEvents);
A simple switch mechanism is usually enough to pass events on to appropriate forms.
First you have to define attribute in form2(child) you will update this attribute in form2 and also from form1(parent) :
public string Response { get; set; }
private void OkButton_Click(object sender, EventArgs e)
{
Response = "ok";
}
private void CancelButton_Click(object sender, EventArgs e)
{
Response = "Cancel";
}
Calling of form2(child) from form1(parent):
using (Form2 formObject= new Form2() )
{
formObject.ShowDialog();
string result = formObject.Response;
//to update response of form2 after saving in result
formObject.Response="";
// do what ever with result...
MessageBox.Show("Response from form2: "+result);
}
I raise an event in the the form setting the value and subscribe to that event in the form(s) that need to deal with the value change.

Categories

Resources