Click here to Skip to main content
15,923,120 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Quite simply I want to take the .text information from a text box in another form and store it in as a variable in a new one.

So far the code I have is:

string userName;

frm_LogonScreen Logon = new frm_LogonScreen();

userName = Logon.tbx_UserName.Text;


but it wont pick it up. Any help would be most appreciated :)

Thanks.
Posted

According to the code you've provided, you're not showing the dialog, thus no text is being put into the textbox. There's nothing to "get".
 
Share this answer
 
Comments
WurmInfinity 8-Feb-11 10:55am    
Could you please elaborate on what I need to do please?
#realJSOP 8-Feb-11 18:20pm    
You need to show the dialog.
Sergey Alexandrovich Kryukov 8-Feb-11 16:08pm    
Yes, indeed, ha-ha! My 5.
--SA
Well you have not even shown the child form yet. So it's unlikely that the textbox has been filled in (or if it has even been created yet).

Also, instead of accessing the child control in that form directly, expose a public property that returns the text from the textbox. After you show the form, access the form's public property to get the text you want and then assign it to the userName field.

In the child dialog, expose the text as follows:

C#
public string Username
{
    get
    {
        return textBoxUsername.Text;
    }
}


Now in the parent dialog do this:

C#
string userName;

var dialog = new ChildForm();
if (dialog.ShowDialog() == DialogResult.OK)
{
    userName = dialog.Username;
}
 
Share this answer
 
v3
Comments
WurmInfinity 8-Feb-11 10:55am    
Could you please elaborate on what I need to do please?
Nish Nishant 8-Feb-11 10:58am    
Check my updated answer.
Sergey Alexandrovich Kryukov 8-Feb-11 16:09pm    
My 5. May be my more compact and simple answer would help?
--SA
#realJSOP 8-Feb-11 16:15pm    
Maybe using pictures and audible grunts would be better than words...
Sergey Alexandrovich Kryukov 8-Feb-11 19:20pm    
Maybe. Does not matter too much; the subject is too trivial. (Frankly, main reason of my post was that I did not recognize at first that this one already does the trick :-)
Thanks for the note :-)
--SA
This won't work. What you are doing is creating a new instance of the form and trying to read the values from it which would be empty (if the default answer is empty).

Have a look here. This should help you.
 
Share this answer
 
v2
Probably what you need to do something like this:

C#
string GetPassword() {
    frm_LogonScreen dialog = new frm_LogonScreen();
    if (dialog.ShowDialog() == DialogResult.OK)
        return dialog.txt_UserName.Text;
    else
        return null;
}


You should use modal, dialog-like behavior. Obtain the result and put it whenever you need, null would mean failure to authenticate

—SA
 
Share this answer
 

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