String transfer

I know that many have asked this question, but as i searched i just couldn't get answer to this so i'm asking it in my own thread.

I have two forms, in form1 i have a textbox where certain person should enter his/her name, what i want is that the name that certain person entered is transfered into form2 so i could use that name in form2 to be shown in the MessageBox.

One other thing, how can i transfer integer from form to form, so if i have int named Number and i do calculations in form1 and the Number = 10, how can i transfer that int Number so i can use it in form2 and that it has same value that it has in form1 after the calculations.

Thank you in advance



Answer this question

String transfer

  • mPaskov

    Nope, form2 doesn't need to modify the name. Could you please explain a bit more how to pass variable into the constructor
  • davidbellot

    Does form2 need to modify the name that was specified by form1 If not... why not pass the variable (or any others needed) into the constructor of form2

    If it does need to modify such variables you might want to look into a public property on either form1 or form2 so that the launcher of the other form can set the value prior to displaying and read it back afterwards if needed.



  • techie_ns

    Thank you , that really helped alot. But got another question. How to make the same thing but instead, i want string 'name' to transfer data from form1 to form5, but the thing is that i have forms from 1-5 and they open i order from 1 to 5.

    Form2 form2 = new Form2("Bob Vela");

    form2.ShowDialog();

    So this code doesn't work since it opens form2 and sends data into it, when i want it to be sent into form 5.


  • vailyve

    One other thing, how can i transfer integer from form to form, so if i have int named Number and i do calculations in form1 and the Number = 10, how can i transfer that int Number so i can use it in form2 and that it has same value that it has in form1 after the calculations.
  • Arvan

    c the below code and try it for rest of the forms.

    int temp;

    public Form3(int Tocno)

    {

    InitializeComponent();

    temp = Tocno;

    }

    private void button1_Click(object sender, EventArgs e)

    {

    {

    Form4 form4 = new Form4(temp);

    form4.ShowDialog();

    Hide();

    }

    }

    thank u



  • Finch82

    If we looked at the code file for your form2 you’d probably see something like this:

        public partial class Form2 : Form

        {

            public Form2()

            {

                InitializeComponent();

            }

        }

    And to use a form2 somewhere in form1 you’d have:

                Form2 form2 = new Form2();

                form2.ShowDialog();

    In order to pass a value in we’d change both to be as such:

        public partial class Form2 : Form

        {

            public Form2(string name)

            {

                InitializeComponent();

                MessageBox.Show("The user's name is: " + name);

            }

        }

    And to use it:

                Form2 form2 = new Form2("Bob Vela");

                form2.ShowDialog();

    The function Form2 we changed is the constructor of the class and is called when creating it which occurs when you use new Form2(). In this case we are passing in the string “Bob Vela” which is then available to the constructor just as any function has access to the arguments passed into it.



  • feby

    here is what is troubling me...

    private void button2_Click(object sender, EventArgs e)

    {

    {

    int Tocno = 0;

    Tocno++;

    Form3 form3 = new Form3(Tocno);

    form3.Show();

    Hide();

    }

    }

    ^this is my code in form2, after the button is pressed int 'Tocno' is incresed for 1 and sent to form3

    public Form3(int Tocno)

    {

    InitializeComponent();

    }

    ^and this is code in form3, the int 'Tocno' has been sent to form3, but the problem is that i don't know how to use that in here:

    private void button1_Click(object sender, EventArgs e)

    {

    {

    Form4 form4 = new Form4();

    form4.ShowDialog();

    Hide();

    }

    }

    ^that is code part in form3, i want to be able to use that int 'Tocno' when the button is clicked, but don't know how.


  • HarisRashid

    If you need name going to all 5 you can simply tweak each of the forms to accept the same input parameter so you’re end result would look something like:

    Form2 form2 = new Form2("Bob Vela");

    form2.ShowDialog();

    Form2 form3 = new Form3("Bob Vela");

    form2.ShowDialog();

    Form2 form4 = new Form4("Bob Vela");

    form2.ShowDialog();

    Form2 form5 = new Form5("Bob Vela");

    form2.ShowDialog();

    As I said previously, the method you use to transfer a number (or any value) depends on how you want it to be used... if you only want to use it within the object you want to send it to and never get it back (or modifications), passing it in as an argument is the easiest way... otherwise using a public property is your best bet where your caller would set the value on the form, display it, let it do the work and then read it back later.



  • Knubbi

    hi, sephiroth

    first of all make each thread for single topic if you want to ask about different topics start new thread

    second mark the answer for your question as reply

    third nothing better than a good book or decent tutorial http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=212562&SiteID=1

    this code will not increase tocno by one it will just make tocno = 1 each time you click button2

    private void button2_Click(object sender, EventArgs e)

    {

    int Tocno = 0;

    Tocno++;

    because every time you click this button you declair tocno = 0 then increase it =1 with each click the same senario will happend, you need to change the variable scope

    int Tocno = 0;

    private void button2_Click(object sender, EventArgs e)

    {

    Tocno++;

    like that the variable will hold its value as long as you use the same class without creating a new instance

    in form3 you recieved the variable but you did nothing with it, you have to save it in a class member to be able to use it with your other methods

    int myvar;

    public Form3(int Tocno)

    {

    InitializeComponent();

    myvar = Tocno

    }

    private void button1_Click(object sender, EventArgs e)

    {

    Form4 form4 = new Form4(myvar);

    form4.ShowDialog();

    Hide();

    }

    more over i don't know why you use a new block inside the method {{}} really you don't need that

    hope this helps



  • String transfer