Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing details between forms - again! 1

Status
Not open for further replies.

aprobe

Programmer
Dec 4, 2003
36
NZ
I'm having trouble passing data between forms. Before you shout, I know there are many threads about this topic already but none seem to work for me.

basically form 1 is like this:-

Form abc = new PasswordResetFrm();
abc.userName = "Fred";
abc.ShowDialog(this);

in form 2 (PasswordResetFrm) I have a property as followe:-

public string userName
{
get
{
return txtUser.Text;
}
set
{
userName = txtUser.Text;
}
}

The problem is this, I cannot see the property userName in form1. In fact I cannot see anything I add as public in the 2nd form. I can't even see a textbox on the 2nd form if I change 'modifiers' property to public.

The form displays correctly. I can change main properties of the 2nd form before showing i.e. the displayed form name (text property). I just can't see anything I've added.

Both forms are in the same project and namespace.

It's as if it's missing some references.

If I try to compile the program I get the message 'System.Windows.Forms.Form' does not contain a definition for 'userName'.

By the way, I haven't tried passing the details in the constructor. I want to try and crack it the proper way and I also want to be able to get a result back from the 2nd form.

Can anyone help please.

Brian
 
There are many ways to communicate between two forms.
Use an intermediate class where encapsulate the shared data/methods:
Code:
// intermediate class
public class I
{
    public I()
    {
        Index =0;

    }
    public int Index;
}

// class A (Form1)

public class A
{
    public A()
    {

    }
    
    public I shared   
    {
        get {    return _s;    }
        set { _s=value;}
    }
    private I _s;




}
// class B (Form2)

public class B
{
    public B()
    {
    }
    private I _s;
    public I shared   
    {
        get {    return _s;    }
        set { _s=value;}
    }
     private I _s;
}

How to use:
I s = new I();
s.Index++;
A objA = new A();
B objB = new B();
objA.shared = s;
objB.shared = s;
//Access and modify shared data
objA.shared.Index;
objB.shared.Index;
-obislavu-
 
Thanks for that information, I'll bear it in mind.

However, what I'm trying to do is something which is fundamental. I can't see new properties that I create on the 2nd form and defined as public. Why ?

 
Are the namespaces the same for both forms?

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Yes both are in the same project and Namespace.

Brian
 
Go to your \bin\debug directory and delete your executable (*.dll, *.exe), and rebuild. See what happens.

Also, shouldn't this:
Code:
userName = txtUser.Text;
be this:
Code:
txtUser.Text = value;
Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
I deleted all .dll and .exe but this did not resolve the problem.

Regarding the incorrect property settings, you may be correct. That part was added in a hurry, but that should not stop me seeing the property from the other form.

Brian
 
Found it. The problem is in this line:
Code:
Form abc = new PasswordResetFrm();
You are declaring abc as a Form, and not a PasswordResetFrm.

Naturally, a Form doesn't know anything about the properties of a PasswordResetFrm.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Brilliant!!

It was staring me in the face but I couldn't see it!

I've spent hours thinking it must be something more complicated.

Thanks a lot !!

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top