Solved

Passing Values between Forms

Posted on 2004-04-20
9
309 Views
Last Modified: 2010-04-15
Hello

I have a form A which accepts 2 primary keys ID's id1 and id2. I want to pass these 2 Ids to 3/4 forms in the same project. I am presently using serialization to pass these values. It works fine..

Now i wanted to do updates of the existing customers.. So i am inheriting all the forms.. Now the problem is: when i load the inherited forms i am still seeing the old values from the original forms.. But i need the lastest values in the update forms. (not original form values)

So i was just wondering if there is any round about way of passing values between forms instead of using serilaization.. i would appreciate if u can help me providing me with an example.

thx

0
Comment
Question by:karra
9 Comments
 
LVL 22

Expert Comment

by:_TAD_
ID: 10870457


This is a case where a singleton might be very helpful to you.  

Create a user class that inherits from a singleton (which has a static instance function).
0
 
LVL 1

Expert Comment

by:lampy24
ID: 10870720
0
 

Author Comment

by:karra
ID: 10871562
Hey Lampy24,

As per ur's articles i wrote the following. Can u please look into it and let me know what am i doing wrong.

Aim: To pass RefNo and SSN to the child form.
------
main form:
-------------
public delegate void ValuePassEventHandler(int RefNo, int SSN);
public event ValuePassEventHandler ValuePass;

private void btnSubmit_Click(object sender, System.EventArgs e)
{

................
ValuePass(RefNum,SSNum);
}

Child Form:
---------------
public form1()
{
frmNewOffender newOff = new frmNewOffender();
newOff.ValuePass +=new Offender.frmNewOffender.ValuePassEventHandler(this.AcceptValue);
}

private void AcceptValue(int RefNo, int SSN)
{
             txtRefNo.Text = RefNo + "";
             txtSSN.Text = SSN + "";
}
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 1

Expert Comment

by:lampy24
ID: 10872721
Hey karra,

Is the frmNewOffender method .Show() called before the submit button?  I think you need to do this before you call the event.  Try this if not let me know, I am testing this to see if I can get it to work with invoking the Show method on the form.

Good Luck
0
 
LVL 1

Expert Comment

by:lampy24
ID: 10872765
What problems are you encountering with your code?
0
 

Author Comment

by:karra
ID: 10872870
I am not calling frmNewOffender.Show() anywhere..

All i wanted to do is: Pass the 2 Ids from the frmNewOffender() to form1..

When i complied the code it gave me no errors. But whn i ran the code it said: Object was not found as exception and then the 2 Id textboxes in the form1 were Null..
0
 

Author Comment

by:karra
ID: 10872879
I am calling form1( ) from NewOffender( )  by the click of a button.. (just in case u want to know more.. )
0
 
LVL 1

Accepted Solution

by:
lampy24 earned 50 total points
ID: 10879296
The problem you are having is that you need to initialize your child form: form1.   The error is occurring because there the object attached to the delegate(form1) is not initialized.  So some where in your main form you need to do this before the submit button is pressed:

Form1 form1 = new Form1();

Good Luck
0
 
LVL 2

Expert Comment

by:sholodak
ID: 10885068
You should make a variable for the 3 or 4 forms accessible as a private or protected variable from the main form.  I'm assuming that you are getting the ID's on the main form.  I would attach an event handler to the textbox/combobox/whatever to fire whenever an ID column changes.  In this event handler, simply set the value in each of the 3/4 forms.  You can do this with or without showing the forms.

In other words, in the constructor of the main form, just create the child forms.  You don't have to show them.  In each child form, either set up a public variable for the ID fields or set up a function to set those values.  The event handler will take care of keeping the child forms values up to date.  

Another way to handle this would be to make event handlers within the child forms and attach them to the textbox change event on the main form.  i.e., something along the lines of frmMain.txtID1.TextChanged += new EventHandler(frmChild1.ID_Changed); frmMain.txtID1.TextChanged += new EventHandler(frmChild2.ID_Changed);, etc., etc.  This way the private forms would be notified directly when the value changed.  It might be a little trickier to get at the current value this way, but it is doable.

Scott
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question