Hi, I am now doing a window form application which are contains several window form interfaces My question is that :
Firstly I enter texts in a text box of interface A (login page) , then I clicks a button to hide A and go to interface B.
At B, I click a button again to hide B and go to interface C. C contains another text box and here I wants to insert or copy the text from textbox at A to text box C. By the way, the texts in text box A seems like been cleared after I write the code "this.Hide".
How to solve this problem? Anyone have idea?
Below is the event flow:

A.hide,B.Show>> B.hide,C.show>> Copy textbox content at A to textbox C

my codes:

C.Text=A.Text; //It doesnt insert anyting after i execute

Hope someone can help, thanks in advance.

Recommended Answers

All 10 Replies

I usually do this, in say, form1. What happens when I click a button, form1 passes two values to hostGameForm(), which is name, and colo.

frmGameServe hostGameForm = new frmGameServe(name, colo);
hostGameForm.ShowDialog();

Now, for this to work, frmGameServe must be modified to accept two values. So I put in string name, and string colo where the code begins, under public partial class.

public frmGameServe(string name, string colo)
        {
            InitializeComponent();
            lblName.Text = name;
            lblColor.Text = colo;
        }

I then capture name and colo in label boxes. This is to pass data from Form1, to frmGameServe. Since you have a third form, you can repeat the process to pass it from frmGameServe to Form3

1st of all, there is no word about Interfaces - there is a completely different expression. You wanna talk aobut Forms. Ok? So, please in the future use this term - Form.
What you can do, is to create a seperate class, with property (property is a "field" which uses get set accessors). And mark the property as static (with static modifier).

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

        private void button1_Click(object sender, EventArgs e)
        {
            //set the static property with the text you want:
            MyClass.MyText = textBox1.Text;
            Form2 f2 = new Form2();
            f2.Show();
            this.Hide();
        }
    }

    static class MyClass
    {
        public static string MyText { get; set; }
    }

    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
            //get data from static class from where ever you want:
            //form2 was in between, but form2 has nothing to do with passing data
            //so we can call the GET accessor from where every want - from form3:
            textBox1.Text = MyClass.MyText;
        }
    }

hi Mitja, firstly thanks for the remind of the word 'form', I will remind it.
The class is created below the Form1 or I needs to add it from the Project >> Add Class ?

As I understand Your, you like to send value of text to textbox of other form.

go to the designer of the destination form and find the code for text change the access specifier from private to public
Now

//create object of destination
form2 f =new form2;
f.textbox.text="Your text";
f.show();

As I understand Your, you like to send value of text to textbox of other form.

go to the designer of the destination form and find the code for text change the access specifier from private to public
Now

//create object of destination
form2 f =new form2;
f.textbox.text="Your text";
f.show();

Bad, bad example.
What you did is to set control and public (textBox), and this is sometihng the worst you can do.
And besides, he said he wasnt to pass data over 3 forms, not only form 1st to 2nd, but from 1st, over 2nd, to 3rd form. And this is not gonna work.

Take a look at mine Simple and working example.

Hi! use global variable..
for example in the login form, you must have a button which on clicks hides the login form and opens form B. so put this part in the code behind for the button.

formB Form1 = new formB(textbox.Text);
                            Form1.Show();
                            this.Hide();

Now in FormB, put a hidden textbox or a label which will contain the data, i've used a textbox in this case.

public FormB(string sTEXT)
        {
            InitializeComponent();
            textbox.Text = sTEXT;
        }

Put this code in the code which will hide formB and open formC.

formC Form1 = new formC(textbox.Text);
                            Form1.Show();
                            this.Hide();

And finally put this code in FormC and dont forget to put a textbox in the formC!

public FormC(string sTEXT)
        {
            InitializeComponent();
            textbox.Text = sTEXT;
        }

I hope its helps!

hi Mitja, firstly thanks for the remind of the word 'form', I will remind it.
The class is created below the Form1 or I needs to add it from the Project >> Add Class ?

No, not need it. Ok, you can do that, but you can write this kind of class on the Form1, take a look at this example:

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

namespace Jun28Test1
{
    public partial class Form1 : Form
    {
        DataTable table;
        public Form1()
        {
            InitializeComponent();
        }

        //rest of the code on Form1
    }

    static class MyClass
    {
        public static string MyText { get; set; }
    }

This is all on one *.cs file - like you see

So no need to create a brand new *.cs file. But if you want, you can do it.

You should use here object to pass any details to one form to other form.On sender side you make object of receiver and pass value in parameters nad on receiving side through constructor receive the value afterwords you can use it.

It works!!! Thanks Mitja and everyone, appreciated the helps :)

you should use static variables for this. for example:

public class A
{
static string str;
str=textBox1.Text;
}
public class B
{
label1.text=a.str;
}

this code may be suggest you how to hold textbox string to another form.

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.