Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,

How can i copy content of one textbox to other at runtime.
Posted
Comments
shefeekcm 26-Jul-11 6:47am    
did u get the answer,please see mine.

From comments I can see that the text boxes are in the different forms, so this is a popular question of form collaboration. The most robust way is implementing appropriate interface in the form class.

Please see my past answer to this question: How to copy all the items between listboxes in two forms[^], see other suggestions and the discussion.

Here is the same for WPF: Updating listviews on different windows[^].

—SA
 
Share this answer
 
You can actually select text and then use Ctrl+C on the first textbox, and then paste using Ctrl+V into the second box.
 
Share this answer
 
Comments
dhawaljoshi 26-Jul-11 5:36am    
it is not funny dude.........
walterhevedeich 26-Jul-11 5:41am    
I actually found it funny. But this can also mean that you need to be more specific on your question. Like, are you writing a ASP.Net or Winforms app? And what would be the trigger on when the value will be copied. Be a little more specific. :)
dhawaljoshi 26-Jul-11 5:44am    
ya em talking about winforms.....
Ankur\m/ 26-Jul-11 7:09am    
That should be reflected in your question.
walterhevedeich 26-Jul-11 5:39am    
5 for the humor. :)
So how about
public class Form1
{
    public string TextboxText
    {
        get
        {
            return( textbox1.Text );
        }
    }
}

public class Form2
{
    private void SomeMethod(Form1 source)
    {
        textbox1.Text = source.TextboxText;
    }
}
This assumes that the caller of Form2.SomeMethod() has a reference to your Form1 object available. If that is not the case, provide one. E.g. in Form2's constructor.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jul-11 15:13pm    
This is more to the point; I voted 4. More robust solution is with interface, please see mine.
--SA
Hi,
pass the value through a funtion and assign that value to the second text box.

for example

form2.function(arg);

in form 2 write the definition of the function and copy the value


if any doubt comment on it.

regards,
shefeek
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 26-Jul-11 15:11pm    
A bit better, my 4, but to be robust and safe, the interface should be used.
Please see my solution.
--SA
Try this :
private void Form1_Load(object sender, EventArgs e)
{
if(TextBox1.Text !="") //check if textbox1 contains string
{
TextBox2.Text = TextBox1.Text;
}
}
 
Share this answer
 
v2
Comments
dhawaljoshi 26-Jul-11 5:37am    
thnx praveen but i want to copy the content of the textbox from form1 to a textbox in form2.
Praveen Kullu 26-Jul-11 6:08am    
Store your text box text in some file by using streamwriter class. Then read the text from that file by using streamreader and show it in your other text box.
[no name] 26-Jul-11 6:20am    
Are you sure this is the best way to do what he is asking?
Praveen Kullu 26-Jul-11 6:40am    
There is another method which involves finding the text on textbox control and then storing that text in a global string variable. That variable can then be used on to show in another form.

But the above method works well when two forms are not related to each other.
Sergey Alexandrovich Kryukov 26-Jul-11 15:06pm    
1) you don't need the check for "", ever; 2) you should use string.Empty, not "".
--SA
As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA
 
Share this answer
 
Try This

In Form1
session["TxtFromForm1"]=textbox1.text;

InForm2

textbox1.text=session["TxtFromForm1"].ToString();
 
Share this answer
 
Comments
dhawaljoshi 26-Jul-11 5:59am    
sessions are used in web based programming.
dhawaljoshi 26-Jul-11 6:00am    
not in winforms
now you write this code like:-
//In form 1 button or your contorl which you want to call form2
Form2 f2 = new Form2();
            this.AddOwnedForm(f2);
            f2.Show();


//now write code in form2 in load event or when you need to necessary to display form1 textbox value:-
Textbox t= (TextBox)this.Owner.Controls["TextBox1"]);
textbox2.text=t.text;


in above code textbox1 is the name of text box of form1 textbox. and textbox2 is name of form2 textbox.
 
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