Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
I want to update a texbox of form2 from form1.
In form2 i have a textbox which show result form mysql database i need to set value from form1.
What is the method. Please help.
Thank You
Posted
Comments
[no name] 7-Sep-13 7:33am    
There are many ways. It would really help us help you if you told us why the answers to the exact same question others asked and you are asking now did not help you. So we do not end up repeating ourselves with solutions that you have tried already and they did not work for you.
BillWoodruff 8-Sep-13 2:32am    
This is one of the most commonly asked types of questions asked, and answered on CP Q&A.

Very important information is missing here: how are the instances of Form1 and Form2 created ?

If Form1 creates the instance of Form2, that is a different "reality" than if Form2, and Form2, are created by some other Form, or if Form2 creates Form1. If an instance of Form2 is created once, and is "always there" during the "life" of the application: that's one scenario, if an instance of Form2 is created "on-demand," and then disposed of when not needed, that's another scenario.

There are several valid/useful/practical ways for Form1 to "get a reference" to Form2's TextBox. The selection of the technique you use should be based on the overall interaction, and relationship, of Form1 with Form2.

There are other techniques which are not mentioned by any of the "solutions" here.

By adding more details to your question, I think you'll get more focused, and helpful, responses.

It depends on the relationship between the forms: which is the parent of the other and so forth.

One of these should cover it:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
 
Share this answer
 
Comments
Thomas Daniels 7-Sep-13 7:48am    
My 5!
Let see an example, I assume your value of form 1 is string then, make a constructor for form2 that accept that string value and in calling new form2 pass that value to constructor then set it to form2.frm2Textbox.text like :
C#
Form2 form = new Form2(valueOfForm1);

In form2 constructor:
C#
public class Form2 : Form
{
    public Form2(string text)
    {
        frm2Textbox.Text = text; 
    }
}

You can pass any type of value like above example.Still confused? See it:
Passing Data Between two forms (Windows forms using c#) [^]
 
Share this answer
 
v3
Comments
Thomas Daniels 7-Sep-13 7:48am    
My 5!
ridoy 7-Sep-13 7:50am    
Thank you,:)
BillWoodruff 8-Sep-13 2:39am    
In this answer you assume that by "update" the OP means a one-time only, or "static," operation. I would not make that assumption, or any other, until the OP tells us more about what they are doing. Please see my comment to the OP.
ridoy 8-Sep-13 2:45am    
Yes Bill i agree with you,as i said in my answer i assume the OP's concept as he didn't clear those things.
hello!
If you want this in Window forms refer this link

http://stackoverflow.com/questions/11165537/passing-textboxs-text-to-another-form-in-c[^]

Ravi Halvadiya
Software Engineer
Email:ravi@insightsoftech.com
 
Share this answer
 
v3
Comments
[no name] 7-Sep-13 10:37am    
How, exactly, would you recommend that the OP use a query string in a Winforms application?
OriginalGriff 7-Sep-13 17:25pm    
Reason for my vote of one: please read the question before trying to answer. Even if this was web based, which this isn't, a query string is probably not a good solution in this case anyway...
 
Share this answer
 
Hi,
You can use public variable,
I example, form1 has a text box named 'textbox1', form2 has a text box named 'textbox2', in the form1 has a button named 'button1' used to call form2,

THE WAY TO DO :
1/Create public string variable named 'str' in form2,
2/In pressed event for button1 in the form1 you need to write code :
form2 f=new form2;//create new instant for class form
form2.str=this.textbox1.Text;
f.ShowDialog();

3/In loading event of the form2 you write code as belowing :
textbox2.Text=str,

Hope this will be helpfull for you.
Thank
 
Share this answer
 
Comments
OriginalGriff 7-Sep-13 17:23pm    
Reason for my vote of one: this is a very poor answer.
You need y to brush up on your good programming practices, and not recommend the use of public variables - they are not a good idea, you should use properties instead.
Oh, and look at overloading constructors as well...

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