Click here to Skip to main content
15,888,139 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have passes some values though a constructor from one from to another. the values are successfully transferred in another form through constructor but when i display them in a textbox the values become null and the textbox remains empty. i am showing the code of the constructor for both forms.

this form calls the form1

C#
private void lbl_imgpass_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
   {
       string img = img_name.Text;
       Setimgpass st = new Setimgpass(img);
       st.Show();
   }


form1(from where the values are transferred to another form)
else
        {
             first = textBox2.Text;
             second = textBox3.Text;
             third = textBox4.Text;
             Adduser add = new Adduser(x1low, x1up, x2low, x2up, x3low, x3up, y1low, y1up, y2low, y2up, y3low, y3up, first, second, third);
          this.Hide();

        }


second form(where the values are received)

C#
public Adduser(string x1low, string x1up, string x2low, string x2up, string x3low, string x3up, string y1low, string y1up, string y2low, string y2up, string y3low, string y3up,string first, string second, string third)

      {
        InitializeComponent();
        frst = first;
        secnd = second;
        thrd = third;
        lowx1 = x1low;
        upx1 = x1up;
        lowx2 = x2low;
        upx2 = x2up;
        lowx3 = x3low;
        upx3 = x3up;
        lowy1 = y1low;
        upy1 = y1up;
        lowy2 = y2low;
        upy2 = y2up;
        lowy3 = y3low;
        upy3 = y3up;
        txt_imgpass.Text = frst + " " + secnd + " " + thrd;

    }


txt_imgpass is the textbox in which i want values.

What I have tried:

i have tried displaying values in a textbox on activated event of a form but then also while debugging the values of these variables are null.
Posted
Updated 26-Mar-16 19:13pm
v2
Comments
Sergey Alexandrovich Kryukov 26-Mar-16 20:09pm    
Method arguments are passed into a constructor in exact same way as in any other method. You just need to run it under the debugger and see what's going on, nothing else.

I would not use so long argument list; it's too easy to mess it up. And it's hard to help figuring out the bugs made by a person starting code samples with the word "else"... :-)

—SA
Member 11958493 26-Mar-16 20:31pm    
the order of the arguments are absolutely correct and i have done debugging also and the problem is that after the constructor has called and on the execution of this.hide() the values become null
ZurdoDev 26-Mar-16 20:29pm    
This is very, very easy to figure out. But you will have to do it. Just debug the code and watch what is happening line by line.
Sascha Lefèvre 26-Mar-16 20:36pm    
As you never call add.Show() I can only assume the TextBox that remains empty isn't the TextBox that you think it is.
Member 11958493 26-Mar-16 20:39pm    
i have tried add.show() but what it does is that

let me show you the control flow its user register -> form2 -> user register so according to you when i use add.show() the values which were already present on user register previously are gone and only the current value in the textbox remains i hope now you have understood what i wanted to say?

1 solution

When you declare and create and instance of the Form, Setimgpass, named'st in the call to the EventHandler lbl_imgpass_LinkClicked:

1. That Form instance exists only in the scope of the EventHandler: when the EventHandler finishes, you have no reference to the instance 'st.

So, unless you have done something (not shown in your code posted here) in the instance 'st to send/transfer data to the Form that created and showed it, there is no data available.

Even if you created Public Fields/Properties in the instance of Setimgpass, and then declared it at Form scope, and kept a reference to it, when the user Closes the Form ... rather than hides it ... the Form is disposed, and any data is ... gone.

There are several well-known ways to pass data to/from Form to Form:

a. creating a custom DialogResult to pass back to the base Form ... when the Form that contains data you want to transfer is shown modally.

b.0. inserting a reference to the base Form into the shown Form, so the shown Form can set properties, or call Event Handlers, in the base Form: this is a very bad idea and leads to coding horrors !

b.1. exposing objects (Controls, UserControls, etc.) on the shown Form by Properties declared public. So, the base Form can directly use those objects, assign EventHandlers to the objects, etc. This is also a bad way to enable Form to Form communication, and violates the principle of encapsulation essential to good code structure.

b.2. creating some form of Delegate (Events, Delegate, Action, Func) (declared 'public) in the shown Form, which the base Form can subscribe to. Custom EventArgs can be used, also (imho, they are rather a mess, and there are better ways to do that in modern .NET).

c. defining public Delegates (I like using the 'Action, and 'Func "shorthand" that makes using delegates so much easier) in the shown Form, and then having the base Form inject code into those Delegates.

d. using some "global" object (perhaps a 'static class) as a "hub," or "broker," to manage form to form communication. This is generally not a good idea ... unless you are implementing MVC, MVP, MVVM, etc.

e. as you have, obviously, found out, you can pass data in the constructor of a Form (or any other object), but, clearly, you need something more than that to get data back to the base Form.

I prefer method 'c. because it reduces the dependency of the shown Form on the base Form: the shown Form does not know which (one, or many) external consumers are using the Form; it's "loosely coupled" to its clients.

When, and how, to pass the data is a strategic concern:

a. is having complete data required ?

a.1. if yes, how do you handle the user closing the Form before all the whatever are filled-out, or set ?

a.2. if no, how do you know ... back in the base Form ... which data is valid, which was not entered ?

a.3. in either case: How does the base Form know which Fields/Properties were set, or not set ?

If you must have complete data, use of 'ShowDialog is an obvious choice; or, handling the Form Closing Event, and preventing the Form being closed before data is complete.

If you don't have to have complete data, you can muck around with using nullable variables, or classes, or structs, but this can be a lot of work ... testing them all for 'null.

In the case you have a lot of data to transfer (which appears to me to be your case, here), consider creating a custom class or struct to hold the data.

One strategy I have used (for massive data transfer, when partial data is okay) is to use a custom class: one instance of the Class is passed to the shown Form in the constructor, and the values in it may be used to seed the UI objects with values The shown Form then creates a second instance of the class which it passes back to the base Form. In the base Form a comparison of the first, and second instances, can be done quickly (using a comparison facility you have baked into the structure of the class used for data exchange) to determine if the data is complete.

If you clarify your question here, to include information about whether you require complete data, I'll add a short code example to this post.
 
Share this answer
 
v2
Comments
Member 11958493 27-Mar-16 10:38am    
ya i like to transfer all data to another form

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900