Hello there!

Well you see, I have got this little problem and I usually dont post on forums unless I am really stuck at something :s

So I am trying to accomplish something that more or less comes down to this:

I have several forms with several textboxes and by pressing the next button you go to the next form, etc.

But in the end I want to collect all the data/variables into another form or console application that uses my code to write all that data to certain cells in an excel file.

I have basicly tried everything I found on the internet but those things were usually about changing the text in a second form to a variable that was declared in the first form, or I would create a new class and define a variable there but that gave me the problem I couldnt set the variable to the string value of my textbox :@

So in a simplified way, what I am trying to accomplish is getting a string variable from one textbox and then use that variable in another form. (but not something like changing label1/textbox1.text into the value of that variable)

Sorry if this sounds complicated but I have been looking for this (maybe obvious) answer for days :?:

Hi there,

You could create a public property in the form where the textbox is:

public string GetTextBoxText
        {
            get { return textBox1.Text; }
        }

And when you create this certain form in the other form, you can call the property like:

FormWithThatTextbox form = new FormWithThatTextbox();
string variable = form.GetTextBoxText;
form.Show();

so the variable will have the textbox's text. I'm not sure if this is what you needed, but this is what I understood :)
I hope it helps!

Hey, thanks for replying so fast :)

I really thought I had it for a second xS I'll show you the code and maybe you can see what beginner's mistake I'm making? :P (I just tried this with a very small test setup

form 1 code(textbox and button):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string GetTextBoxText
        {
            get { return textBox1.Text; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 myNewForm = new Form2();

            myNewForm.ShowDialog();
        }
    }
}

form 2 code(textbox that displays the text from form1):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            test();  
        }
        private void test()
        {
            Form1 form = new Form1();
            string variable = form.GetTextBoxText;
            textBox1.Text = variable;
        }
    }
}

I'm sorry if I'm overlooking something really obvious but I had a really tiring day :yawn: :zzz: ;)

Hi there,

You could create a public property in the form where the textbox is:

public string GetTextBoxText
        {
            get { return textBox1.Text; }
        }

And when you create this certain form in the other form, you can call the property like:

FormWithThatTextbox form = new FormWithThatTextbox();
string variable = form.GetTextBoxText;
form.Show();

so the variable will have the textbox's text. I'm not sure if this is what you needed, but this is what I understood :)
I hope it helps!

Hey, thanks for replying so fast :)

I really thought I had it for a second xS I'll show you the code and maybe you can see what beginner's mistake I'm making? :P (I just tried this with a very small test setup

form 1 code(textbox and button):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string GetTextBoxText
        {
            get { return textBox1.Text; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 myNewForm = new Form2();

            myNewForm.ShowDialog();
        }
    }
}

form 2 code(textbox that displays the text from form1):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            test();  
        }
        private void test()
        {
            Form1 form = new Form1();
            string variable = form.GetTextBoxText;
            textBox1.Text = variable;
        }
    }
}

I'm sorry if I'm overlooking something really obvious but I had a really tiring day :yawn: :zzz: ;)

(Sorry for double post xS)

Ok try this:

Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.Text = "safasdfads";
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 myNewForm = new Form2();
            myNewForm.GetTextBoxText = textBox1.Text;
            myNewForm.ShowDialog();
        }
    }
}

Form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public string GetTextBoxText
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            test();
        }

        private void test()
        {
            string variable = GetTextBoxText;
            textBox1.Text = variable;
        }
    }
}

First it creates the form, and set it's property, then show. In the second form the property will be used to set the textbox :) that's it, if you have any questions please provide a code snippet aswell :) I hope it helped

Tamas

Ok try this:

Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.Text = "safasdfads";
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 myNewForm = new Form2();
            myNewForm.GetTextBoxText = textBox1.Text;
            myNewForm.ShowDialog();
        }
    }
}

Form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public string GetTextBoxText
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            test();
        }

        private void test()
        {
            string variable = GetTextBoxText;
            textBox1.Text = variable;
        }
    }
}

First it creates the form, and set it's property, then show. In the second form the property will be used to set the textbox :) that's it, if you have any questions please provide a code snippet aswell :) I hope it helped

Tamas

I have experimented with it several times, and namen the textbox in my Form2 textbox 2 to avoid misunderstandings :S
But still no luck, I add a button to go to the second form and my textBox is still empty :(

I'll provide the code I have at the moment:

form 1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.Text = "safasdfads";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 myNewForm = new Form2();
            myNewForm.GetTextBoxText = textBox1.Text;
            myNewForm.ShowDialog();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Form2 myNewForm = new Form2();
            myNewForm.ShowDialog();
        }
    }
}

form 2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public string GetTextBoxText
        {
            get { return textBox2.Text; }
            set { textBox2.Text = value; }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            test();
        }

        private void test()
        {
            string variable = GetTextBoxText;
            textBox2.Text = variable;
        }
    }
}

I really hope I can work this out somehow :?:

Thanks for the replies

I don't why you try such a complicated procedure all though I am not an expert but I give you what you want as I understand your problem is sending text box text from form1 to form2 simply create a public variable on form2 of string and example

//form2
public string v;

and access this variable from form1 like this

Form2 myNewForm = new Form2();
myNewForm.v = textBox1.Text;

May it will help You If work on that

Best Of Luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.