Visual C ++ 2005: How do I access Win32 form control values ​​in a .cpp file?

I recently started to get jittery in Visual Studio 2005 and I am in Visual C ++. When I double click on the control in the designer it opens the .h file which I figured is for inverse declarations and if I put all the guts in functions there I can get my program to work but I don't like having the code in the .h file.

However, when I put things in a .cpp file, I cannot access the controls at all. I'm getting a lot of compiler errors and the like, and I'm wondering what VC ++ expects as I'm more used to the GCC / MinGW environment.

The errors I get the most are:

error C2228: left of '.trackBar1/.value/.etc' must have class/struct/union


error C2227: left of '->trackBar1/->Value/->etc' must point to class/struct/union/generic type

I tried the following to access the control:

Junk::Form1::trackBar1->value
Junk::Form1::trackBar1.value
Junk::Form1->trackBar1
Junk->Form1->trackBar1
this->trackBar1->value //This is legal in the .h file, and how I can get it to work there
trackBar1->value

      

And a few others who are just trying to get rid of despair. I tried to specify the same namespace and everything in the .h file and I still can't access the control.

I am using Visual Studio 2005 with Visual C ++ CLR Win32 application. The code Visual Studio uses to instantiate the controls:

this->trackBar1 = (gcnew System::Windows::Forms::TrackBar());  

      

In Dev-C ++ or code :: blocks I use to place class declarations in a .h file and then specifying functions in a .cpp file by executing class :: function, but in Visual Studio, I just can't figure out why I can't do the same, or what I'm doing horribly wrong and stupid.

Thank.

0


source to share


2 answers


Thanks for clarifying the question, here are some ideas

  • Make sure the .h file is included in the appropriate cpp, for example make sure that if you define

foo.h

class
{
...
};

      

make sure foo.cpp contains foo.h like:

#include "foo.h"

... definitions ...

      



  • It looks like in the context of your class the reference to trackBar1 should be valid, this is your last example above. If you are outside of your class, you will need to declare an instance of the incoming class and reference its trackBar instance.

Also, as I said,

You need to know if your "Form1" is a value or an instance of an object or class or namespace. If it's a namespace / class less than using :: to qualify the namespace is appropriate. Due to a bug, it looks more like not a namespace / class / structure. You seem to be trying to treat it as a pointer as well. Is this a pointer? How did it come about?

I would simplify the experiment with an extremely simple C ++ header / cpp example. Please confirm it works. Slowly copy pasting into your functionality and see at what stage it breaks. Make the world hello world VS 2005 program and add to your code.

When you find where it breaks and is still broken, edit your question with a simplified set of example code and we can better help you debug.

0


source


Perhaps this was due to a change in the controls (i.e. double click). Go back to the control (in view mode) and change the name. Then change it. Then recompile.



Hooray!

0


source







All Articles