0

I have a "simple" problem with assign variables from FORM1 (in my code Form1) and using those variables in FORM2 (in my code frLeczenie). So I started to create a public string variables:

        public string wynikImie;
        public string wynikUmaszczenie;
        public string wynikDataUrodzenia;
        public string wynikPlec;
        public string wynikZnakiSzczegolne;
        public string wynikCzyWykastrowane;

To those variables I'll assign data from SQL Database:

private void dgZwierze_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string zapytanie = "SELECT IMIE_ZWIERZECIA, DATA_URODZENIA, PLEC, ZNAKI_SZCZEGOLNE, UMASZCZENIE, CZY_WYKASTROWANE FROM tbZwierze WHERE tbZwierze.IMIE_ZWIERZECIA = '" + wynikImie + "' AND tbZwierze.UMASZCZENIE = '" + wynikUmaszczenie + "'";
            SqlCommand cmdZapytanie = new SqlCommand(zapytanie, cs);
            cs.Open();
            SqlDataReader reader = cmdZapytanie.ExecuteReader();

                if (reader.Read())
                {
                    wynikImie = reader.GetValue(0).ToString();
                    wynikDataUrodzenia = reader.GetValue(1).ToString();
                    wynikPlec = reader.GetValue(2).ToString();
                    wynikZnakiSzczegolne = reader.GetValue(3).ToString();
                    wynikUmaszczenie = reader.GetValue(4).ToString();
                    wynikCzyWykastrowane = reader.GetValue(5).ToString();
                }
            cs.Close();
        }

To this moment all is great, but problem occurs when I opened the FORM2:

private void btnLeczenie_Click(object sender, EventArgs e)
        {
            frLeczenie leczenie = new frLeczenie();
            leczenie.ShowDialog(); 
        }

Suddenly my all data assigned to public string variables is missing, and I can't using their in the FORM 2:

private void frLeczenie_Load(object sender, EventArgs e)
        {
            Form1 formaglowna = new Form1();
            textBox1.Text = formaglowna.wynikImie;
            textBox2.Text = formaglowna.wynikDataUrodzenia;
            textBox3.Text = formaglowna.wynikPlec;
            textBox4.Text = formaglowna.wynikZnakiSzczegolne;
            textBox5.Text = formaglowna.wynikUmaszczenie;
            textBox6.Text = formaglowna.wynikCzyWykastrowane;
        }

What I'm doing wrong? Maybe I missing something? Could you take a look on this?

Regards, Peter.

Foee
  • 77
  • 1
  • 5

3 Answers3

0

new Form1(); creates a new instance of your first form, but you want to use the already initialized form with it's variables instead. So you could pass the form instance via constructor to your second form and store it in a property:

in first form:

frLeczenie leczenie = new frLeczenie(this);

second form constructor:

public frLeczenie(Form1 formaglowna)
{
    InitializeComponent();

    this.Formaglowna = formaglowna;
    // ...
}

public Form1 Formaglowna{ get; set; }

Now you access them via property:

textBox1.Text = Formaglowna.wynikImie;
// ...
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thanks, I understand where I made a mistake. It works fine right now. Regards, Peter. – Foee Mar 13 '13 at 06:29
0

Note that you set the values in an instance of the form in it's dgZwierze_CellContentClick method. However in form 2 you are creating a new Form1 that has never been shown or had that method called. So the values are empty.

Form1 formaglowna = new Form1();
textBox1.Text = formaglowna.wynikImie;

What you need is to pass the real form1 into form2 and not create a new form 1

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
0

When you create the new form1 in the last part of your code, it is a new blank instance of the class. Each instance of a class or form will have its own variable values. If you really need these variables to be available and shared on all objects of a given class, make them static.

GHC
  • 2,658
  • 5
  • 30
  • 35