0

im trying to do a simple variable bind to a control text box but i cant for the life of me get this working. Here is the scenario two forms 1 with datagrid and and the other with two textboxes. When i click on the datagrid i would like to pass those variables to the textboxes in FORM1. This is what i have tried with no results. in my form1

public string myText
    {
        get { return tuidInput.Text; }
        set { tuidInput.Text = value; }
    } 

then i my form two when i try to set the value i try this.

private void selectUser(object sender, DataGridViewCellEventArgs e)
    {
        userPicked.tuid = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["spriden_id"].Value.ToString();
        userPicked.Name =  dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["spriden_last_name"].Value.ToString();

        Form1 form1 = new Form1();

        form1.myText = userPicked.tuid
     }

i got that example from here http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/7308639f-640b-48bf-8293-abcbfd2292d8/

however it does not update the textbox? what should i be doing differentely?

what im i doing wrong ? how can i do this correctly i have looked at several article on here but have not been successful at any of them. any help would be great appreciated.

4
  • Get and set should not reference the UI element. The UI element should bind to the property. Commented Apr 9, 2013 at 1:11
  • can you explain better i just dont follow? Commented Apr 9, 2013 at 1:15
  • 1
    I think the issue is that you are creating a new Form1(), i.e. you aren't setting the value in the current Form1 which is on your screen but some new instance of it....your Form2 needs a reference to the currently open instance of Form1 - do you have an example of how you're opening the forms (e.g. the code for opening Form2 from Form1 or vice versa) Commented Apr 9, 2013 at 1:58
  • Get references a UI element - tuidInput - that is not a good practice. UI element should bind to a public property. Commented Apr 9, 2013 at 3:17

3 Answers 3

1

I'm not sure if that's the case, but in:
form1.myText = userPicked.tuid
you're refering to a newly created Form1 object, initialized just 1 line above. If you'll check form1.myText property, you'll probably notice it has its value assigned, but it's not the form object you want to actually refer to. If your textboxes are in Form2, then create a Form2 reference in Form1. private Form2 form2 = null;
And create a property

public Form2 SecondForm { set { this.form2 = value; } } Assign it properly and then use form2.tuidInput.Text in your Form1 normally.

Sign up to request clarification or add additional context in comments.

5 Comments

i have both forms open and at the same time i want to update the textbox in form 1 from the form2. I'm no sure i follow what you are saying
I mean that you're not changing textbox in your already opened Form1 object, but in the new one that you've created in Form1 form1 = new Form1(); You've got 2 different objects of type Form1 created.
ohh i get it so how do i set the textbox of already open form. using your method?
You'd need to have a reference to Form1 in your Form2 class. So in your Form2 class add 2 fields: private Form1 form1 = null; and public Form1 textboxForm { set { this.form1 = value; } } Then you'd need to assign a proper reference to form2.textboxForm property, but we don't know how and where are you opening those forms - which one opens up which.
So after opening form2 object in form1 put fom2.textboxForm = form1; and you'll be able to see form1 object in your form2. I really don't know what's there you don't understand my friend.
0

none of the answers above answer the question i finally found an answer here http://social.msdn.microsoft.com/forums/en-us/csharpgeneral/thread/9339C805-58C6-4DC2-934F-2A4ADC67ED4D here is all i needed to do. I changed the textbox to public and then ran this from form2 and it worked like a charm.

Form1 form1 = (Form1)Application.OpenForms["Form1"];

Thanks everyone for your responses. Miguel

Comments

0

Have you tried:

Form1 form1 = new Form1();
form1.myText.Text = userPicked.tuid;
form1.myText.Refresh();
form1.Show();

You may have to redraw the text box after reloading it.

Also, you said you want to do this correctly. To do it "correctly" (I say it loosely because "correct" can be interpreted in sooo many ways by so many different people!) - I would like to suggest using an MVP, MVVM or MVC pattern to handle this instead of doing this business logic in the GUI.

For MVP example: http://haacked.com/archive/2006/08/09/ASP.NETSupervisingControllerModelViewPresenterFromSchematicToUnitTestsToCode.aspx

1 Comment

i have both forms open and at the same time i want to update the textbox in form 1 from the form2. when i do it this way i end up with an additional form opening up at the end? inst there a way to open an opened form from another open form?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.