Can't add action to buttons in Visual Studio C++ using Windows Forms

This is a discussion on Can't add action to buttons in Visual Studio C++ using Windows Forms within the C++ Programming forums, part of the General Programming Boards category; Hello Dear Community of cprogramming.com! I have a following problem - I wrote a simple add_record function using structures (here ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    39

    Question Can't add action to buttons in Visual Studio C++ using Windows Forms

    Hello Dear Community of cprogramming.com!
    I have a following problem - I wrote a simple add_record function using structures (here is my main file):

    Code:
    // Exercise1.cpp : main project file.
        
        #include "stdafx.h"
        #include "Form1.h"
        
        using namespace Exercise1;
        
        typedef struct student {
                char *name;
                int index;
                double avg;
                student *next;
                student *prev;
        } stud;
        
        student *first = 0;
        
        [STAThreadAttribute]
        
        
        void add_record(student **first, char *name, int index, double avg){
                        student *new_stud = new student;
                        if (*first!=0) (*first)->prev = new_stud;
                        new_stud->name = name;
                        new_stud->avg = avg;
                        new_stud->index = index;
                        new_stud->next = *first;
                        new_stud->prev = 0;
                        *first = new_stud;
        }
    However, I can't put this add record function into button action (with predefined data, just for testing purposes)
    Code for `Form1.h`:

    Code:
        #pragma once
        
        namespace Exercise1 {
        
            using namespace System;
            using namespace System::ComponentModel;
            using namespace System::Collections;
            using namespace System::Windows::Forms;
            using namespace System::Data;
            using namespace System::Drawing;
        
            /// <summary>
            /// Summary for Form1
            /// </summary>
            public ref class Form1 : public System::Windows::Forms::Form
            {
            public:
                Form1(void)
                {
                    
                    InitializeComponent();
                    
                    //
                    //TODO: Add the constructor code here
                    //
                }
        
            protected:
                /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                ~Form1()
                {
                    if (components)
                    {
                        delete components;
                    }
                }
            private: System::Windows::Forms::Button^  button1;
            protected: 
            private: System::Windows::Forms::Button^  button2;
            //all remaining buttons here - irrelevant
        
            private:
                /// <summary>
                /// Required designer variable.
                /// </summary>
                System::ComponentModel::Container ^components;
        
        #pragma region Windows Form Designer generated code
                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                void InitializeComponent(void)
                {
        }
         #pragma endregion
            private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                     }
            private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                         String ^ i = "working";
                         textBox1->Text = i;
                         
                     }
        private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
                     String ^ g = "test";
                     if(add_record(student **first, testname, 23, 3.5))
                         textBox1->Text = g;
                 }
       
        };
        }
    How to fix that part with record adding?
    I get a following error:

    Code:
    1>d:\visual studio 2010 express\projects\exercise1\exercise1\Form1.h(176): error C2065: 'student' : undeclared identifier
        1>d:\visual studio 2010 express\projects\exercise1\exercise1\Form1.h(176): error C2065: 'first' : undeclared identifier
        1>d:\visual studio 2010 express\projects\exercise1\exercise1\Form1.h(176): error C2065: 'testname' : undeclared identifier
        1>d:\visual studio 2010 express\projects\exercise1\exercise1\Form1.h(176): error C3861: 'add_record': identifier not found
    I guess it's something connected with variable declaration but I don't know where to put that in order for it to work..

    Thanks in advance!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    The whole problem stems from that you have insufficient knowledge of C++.
    student is a Exercise1.cpp, and not in the Form1.h file. The compiler thus cannot find the declaration and you get an error.
    For that matter, you seem to not understand how to make a proper function call. Leave out the type of the argument you want to pass.
    Furthermore, first and testname do not exist. add_record is the same story as for student.

    I really, really, really recommend that you get a beginner's book and learn C++ from the ground. I recommend this book.
    You seem to not understand scope rules, and rely on pointers, new, char* and typedef where unnecessary.
    Also, for Windows Forms problems, I think TechTalk is more appropriate. This forum is for native C++ only.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Please note that you are using .NET in a C++ program. Although not evil by itself, you are making this a lot harder than it has to be. C++/CLI (using .NET from C++) was made for exceptional cases where you require exactly this functionality and are willing to take all it's drawbacks. If you need a UI for simple learning programs, the console if absolutely sufficient and if it's not, there are a lot of native GUI libraries (MFC comes with VS for example). Using C++/CLI while you are still learning C++ is going to be extremly difficult without providing any additional benefit.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change Power Button Action in Windows XP
    By linucksrox in forum Windows Programming
    Replies: 3
    Last Post: 04-21-2010, 08:38 PM
  2. writing windows dlls using visual studio 2008 express
    By xixpsychoxix in forum Windows Programming
    Replies: 17
    Last Post: 01-22-2009, 02:53 AM
  3. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 01:13 AM
  4. Buttons in visual studio .net
    By sql.scripter in forum Windows Programming
    Replies: 1
    Last Post: 05-07-2005, 05:31 AM
  5. Visual Studio Net on Windows 98
    By bvn in forum Tech Board
    Replies: 2
    Last Post: 12-02-2002, 09:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21