hello;
how to access to a control of a form from another form?

example datagridview

Recommended Answers

All 12 Replies

Normlly you would not want to do that. But you could just make the grid Public in the class header file.

commented: 555555 +0

thank you Ancient dragon but i think i have to, because i have datagridview in form3 and i a button in form1 which is responsible on the fillfull of this datagridview, i thought also on making it public but when i did it i get errors. Here is my code in the button in Form1

Form3 ^maForm2 = gcnew Form3();
             maForm2->dataGridView1->AutoResizeColumnHeadersHeight();

       Resize all the row heights to fit the contents of all non-header cells.
           maForm2->dataGridView1->AutoResizeRows(
            DataGridViewAutoSizeRowsMode::AllCellsExceptHeaders);
            // Set the column header names.
     maForm2->dataGridView1->Columns[ 0 ]->Name = "";
      maForm2->dataGridView1->Columns[ 1 ]->Name = "RAM";
      maForm2->dataGridView1->Columns[ 2 ]->Name = "CPU";
      maForm2->dataGridView1->Columns[ 3 ]->Name = "BAT";


             maForm2->Show();

the errors are :

1>------ Début de la génération : Projet : interfaceProject, Configuration : Debug Win32 ------
1>  Form3.cpp
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(481): warning C4018: '<' : incompatibilité signed/unsigned
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(483): warning C4018: '<' : incompatibilité signed/unsigned
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(490): warning C4018: '<' : incompatibilité signed/unsigned
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(523): error C2065: 'Form3' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(523): error C2065: 'maForm2' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(523): error C2061: erreur de syntaxe : identificateur 'Form3'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(524): error C2065: 'maForm2' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(524): error C2227: la partie gauche de '->dataGridView1' doit pointer vers un type class/struct/union/générique
1>          le type est ''unknown-type''
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(524): error C2227: la partie gauche de '->AutoResizeColumnHeadersHeight' doit pointer vers un type class/struct/union/générique
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'all'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C3288: 'System::EventHandler ^' : déréférencement non conforme d'un type de handle
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'all' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'the'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'the' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'row'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'row' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'heights'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'heights' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'to'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'to' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'fit'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'fit' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'the'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'the' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'contents'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'contents' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'of'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'of' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'all'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'all' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'non'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'non' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'header' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'cells'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'cells' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(536): error C2228: la partie gauche de '.maForm2' doit avoir un class/struct/union
1>          le type est ''unknown-type''
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(536): error C2227: la partie gauche de '->Show' doit pointer vers un type class/struct/union/générique
1>  interfaceProject.cpp
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(481): warning C4018: '<' : incompatibilité signed/unsigned
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(483): warning C4018: '<' : incompatibilité signed/unsigned
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(490): warning C4018: '<' : incompatibilité signed/unsigned
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(524): error C2248: 'interfaceProject::Form3::dataGridView1' : impossible d'accéder à private membre déclaré(e) dans la classe 'interfaceProject::Form3'
1>          c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form3.h(37) : voir la déclaration de 'interfaceProject::Form3::dataGridView1'
1>          c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form3.h(15) : voir la déclaration de 'interfaceProject::Form3'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'all'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C3288: 'System::EventHandler ^' : déréférencement non conforme d'un type de handle
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'all' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'the'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'the' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'row'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'row' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'heights'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'heights' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'to'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'to' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'fit'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'fit' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'the'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'the' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'contents'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'contents' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'of'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'of' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'all'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'all' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'non'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'non' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'header' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2146: erreur de syntaxe : absence de ';' avant l'identificateur 'cells'
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(526): error C2065: 'cells' : identificateur non déclaré
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(536): error C2228: la partie gauche de '.maForm2' doit avoir un class/struct/union
1>          le type est ''unknown-type''
1>c:\visual studio 2010\projects\interfaceproject\interfaceproject\Form1.h(536): error C2227: la partie gauche de '->Show' doit pointer vers un type class/struct/union/générique
1>  Génération de code en cours...
========== Génération : 0 a réussi, 1 a échoué, 1 mis à jour, 0 a été ignoré ==========

looks like you did not include form3.h in form1.h

no i did , -_-

Without seeing all the code it's nearly impossible to tell what your problem is. Delete all compiler-generated files, zip it up, and attach it to your post.

the problem is in the Form1 and Form3 , thank you very much
Click Here

your link doesn't work. Just attach it to your post using the Files tab in the purple ribbon above the editor.

This compiles without errors/warnings. This code is in the button click event handler. Note the placement of Form2.h include below. My Form2 is the same as your Form3 -- I just created a project with VC++ 2010 Express that had two forms, not three as in your project. I think one of your problems was on line 11 below -- gcnew Form2(), not gcnew Form().

If you moved all that initialization into Form3 constructor you wouldn't need to make the grid public.

    #pragma once
    #include "Form2.h"

    namespace WinForms1 {
    using namespace System;
...
...
...

        private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                Form2 ^maForm2 = gcnew Form2();
                 maForm2->dataGridView1->AutoResizeColumnHeadersHeight();

    //       Resize all the row heights to fit the contents of all non-header cells.
               maForm2->dataGridView1->AutoResizeRows(
                DataGridViewAutoSizeRowsMode::AllCellsExceptHeaders);
                // Set the column header names.
         maForm2->dataGridView1->Columns[ 0 ]->Name = "";
          maForm2->dataGridView1->Columns[ 1 ]->Name = "RAM";
          maForm2->dataGridView1->Columns[ 2 ]->Name = "CPU";
          maForm2->dataGridView1->Columns[ 3 ]->Name = "BAT";


                 maForm2->Show();



                 }

thanks very much but it didn't work for me; to i will post the code of the 2 Forms : that is Form2

#pragma once

namespace interfaceProject {

    using namespace System;




        }
#pragma endregion
    public: System::Void dataGridView1_CellContentClick(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
                 dataGridView1->AutoResizeColumnHeadersHeight();

             }
    };
}

and this is Form1:

#pragma once

#include "Form2.h"
using namespace std;
namespace interfaceProject {

    using namespace System;




    private: System::Void B_ShowResults_Click(System::Object^  sender, System::EventArgs^  e) {
            //this->Hide();

             Form2 ^maForm2 = gcnew Form2();
             maForm2->dataGridView1->AutoResizeColumnHeadersHeight();

      // Resize all the row heights to fit the contents of all non-header cells.
      /*      maForm2->dataGridView1->AutoResizeRows(
            DataGridViewAutoSizeRowsMode::AllCellsExceptHeaders);
            // Set the column header names.
     maForm2->dataGridView1->Columns[ 0 ]->Name = "";
      maForm2->dataGridView1->Columns[ 1 ]->Name = "RAM";
      maForm2->dataGridView1->Columns[ 2 ]->Name = "CPU";
      maForm2->dataGridView1->Columns[ 3 ]->Name = "BAT";
            // maForm2->Void dataGridView1->Rows->Add*/
            // maForm2->Owner = this;
            maForm2->Show();

         }

and this the error i got :

'interfaceProject::Form2::dataGridView1'::can not access private member declared (e) in the class
'interfaceProject::Form2'

sorry for benning so bothering but it wouldn't work for me

THank you very much for your help

:can not access private member declared (e) in the class

which line does that error appear on? Looks like you didn't make the datagrid public.

honnestly it begin with the line 16 and it repeated for ths hole code

:

    maForm2->dataGridView1->AutoResizeRows(
                DataGridViewAutoSizeRowsMode::AllCellsExceptHeaders);

          maForm2->dataGridView1->Columns[ 0 ]->Name = "";
          maForm2->dataGridView1->Columns[ 1 ]->Name = "RAM";
          maForm2->dataGridView1->Columns[ 2 ]->Name = "CPU";
          maForm2->dataGridView1->Columns[ 3 ]->Name = "BAT";




and i did make the datagrid public look in the line 12 of the code of the Form2 

Line 12 of Form2 does not make the datagrid public -- it makes a function public. You need to find the line in Form2.h that declares datagridview1 and change it from Private to Public. It should be a line that looks like this:

public: System::Windows::Forms::DataGridView^ dataGridView1;

commented: For the effort :) +14
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.