Passing textbox value to another form

S

Saimvp

Good day.

I have a question. I want to Pass the value of my textbox1 from FORM1 to my
Form2 into a textbox1.

#####FORM2 CODE######

private static frmNEmp sForm = null;
public static frmNEmp Instance()
{
if (sForm == null) { sForm = new frmNEmp(); }

return sForm;
}

#####FORM1 CODE#####
private void BUTTON1_Click(object sender, EventArgs e)
{
Form sForm = frmNEmp.Instance();
sForm.MdiParent = this;
sForm.Show();
sForm.Activate();
}

############END##########################

IN VB6 CODE: FORM2.TEXT1.TEXT = FORM1.TEXT1.TEXT
HOW ABOUT IN C#? BECAUSE I'VE TRY THE SOME EXAMPLE ON THE NET, LIKE THIS :
http://codeincsharp.blogspot.com/2008/04/how-to-pass-values-to-another-form.html

When I create another project and follow that codes, its run like what I want.
But when it comes to my project like the first codes above theres no passing
data happen. Please help me.

Thanks.
 
M

Marc Gravell

Simply create a property on Form2 that makes this possible:

public string CaptionText {
get {return textbox1.Text;}
set {textbox1.Text = value;}
}

and assign it:

sForm.CaptionText = textbox1.Text;
sForm.Show();

For the record, I also don't recommend this Form2 Instance() approach;
I'd simply use a new frmNEmp();

A static form, in particular, has various issues with threading,
multiple parenting, etc...

Marc
 
I

Ignacio Machin ( .NET/ C# MVP )

Good day.

I have a question. I want to Pass the value of my textbox1 from FORM1 to my  
Form2 into a textbox1.

#####FORM2 CODE######

        private static frmNEmp sForm = null;
        public static frmNEmp Instance()
        {
            if (sForm == null) { sForm = new frmNEmp(); }

            return sForm;
        }

#####FORM1 CODE#####
        private void BUTTON1_Click(object sender, EventArgs e)
        {
            Form sForm = frmNEmp.Instance();
            sForm.MdiParent = this;
            sForm.Show();
            sForm.Activate();
        }

############END##########################

IN VB6 CODE: FORM2.TEXT1.TEXT = FORM1.TEXT1.TEXT
HOW ABOUT IN C#? BECAUSE I'VE TRY THE SOME EXAMPLE ON THE NET, LIKE THIS :http://codeincsharp.blogspot.com/2008/04/how-to-pass-values-to-anothe...

When I create another project and follow that codes, its run like what I want.
But when it comes to my project like the first codes above theres no passing
data happen. Please help me.

Thanks.

In addition to Marc's comment you can create a constructor of the form
that receive a string parameter, that will be assigned to your textbox
 
S

Saimvp

Hello Marc Gravell and Machin.
Good Day.

Why your not using an Instance in the form and yet your using a New Form?
Im using it because I want to open a single form only. If Im using a New
Form1, In my command button If I will click the button to show the Form1 it
will show the Form1 equal the click in the button.


What you suggestion to me. Im new in programming in C#.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top